View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.genericattributes.service.entrytype;
35  
36  import java.util.Locale;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  import org.apache.commons.fileupload.FileItem;
41  import org.apache.commons.lang3.StringUtils;
42  
43  import fr.paris.lutece.plugins.genericattributes.business.Entry;
44  import fr.paris.lutece.plugins.genericattributes.business.Field;
45  import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
46  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
47  import fr.paris.lutece.portal.business.file.File;
48  import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
49  import fr.paris.lutece.portal.service.file.FileService;
50  import fr.paris.lutece.portal.service.file.IFileStoreServiceProvider;
51  import fr.paris.lutece.portal.service.i18n.I18nService;
52  import fr.paris.lutece.portal.service.message.AdminMessage;
53  import fr.paris.lutece.portal.service.message.AdminMessageService;
54  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
55  
56  /**
57   * Abstract entry type for comments
58   */
59  public abstract class AbstractEntryTypeComment extends EntryTypeService
60  {
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
66      {
67          initCommonRequestData( entry, request );
68          String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
69          String strComment = request.getParameter( PARAMETER_COMMENT );
70          String strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
71          String strIndexed = request.getParameter( PARAMETER_INDEXED );
72          String strDisplayBo = request.getParameter( PARAMETER_DISPLAY_BO );
73          String strFieldError = StringUtils.EMPTY;
74  
75          if ( StringUtils.isBlank( strComment ) )
76          {
77              strFieldError = FIELD_COMMENT;
78          }
79  
80          if ( StringUtils.isNotBlank( strFieldError ) )
81          {
82              Object [ ] tabRequiredFields = {
83                      I18nService.getLocalizedString( strFieldError, locale )
84              };
85  
86              return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
87          }
88  
89          entry.setCode( strCode );
90          entry.setComment( strComment );
91          entry.setCSSClass( strCSSClass );
92          entry.setIndexed( strIndexed != null );
93  
94          GenericAttributesUtils.createOrUpdateField( entry, FIELD_DISPLAY_BO, null, String.valueOf( strDisplayBo != null ) );
95  
96          if ( request instanceof MultipartHttpServletRequest )
97          {
98              MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
99              FileItem fileItem = multipartRequest.getFile( PARAMETER_FILE );
100 
101             if ( fileItem != null && fileItem.get( ) != null )
102             {
103                 removeOldFiles( entry );
104 
105                 PhysicalFile physicalFile = new PhysicalFile( );
106                 physicalFile.setValue( fileItem.get( ) );
107 
108                 File file = new File( );
109                 file.setTitle( fileItem.getName( ) );
110                 file.setMimeType( fileItem.getContentType( ) );
111                 file.setSize( physicalFile.getValue( ).length );
112                 file.setPhysicalFile( physicalFile );
113 
114                 String idFile = getFileStoreServiceProvider( ).storeFile( file );
115                 GenericAttributesUtils.createOrUpdateField( entry, FIELD_DOWNLOADABLE_FILE, file.getTitle( ), idFile );
116             }
117         }
118         return null;
119     }
120 
121     private void removeOldFiles( Entry entry )
122     {
123         Field oldFile = entry.getFieldByCode( FIELD_DOWNLOADABLE_FILE );
124         if ( oldFile != null )
125         {
126             getFileStoreServiceProvider( ).delete( oldFile.getValue( ) );
127             FieldHome.remove( oldFile.getIdField( ) );
128         }
129     }
130 
131     protected IFileStoreServiceProvider getFileStoreServiceProvider( )
132     {
133         return FileService.getInstance( ).getFileStoreServiceProvider( "defaultDatabaseFileStoreProvider" );
134     }
135 }