View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.announce.service.entrytype;
35  
36  import fr.paris.lutece.plugins.announce.service.upload.AnnounceAsynchronousUploadHandler;
37  import fr.paris.lutece.plugins.genericattributes.business.Entry;
38  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
39  import fr.paris.lutece.plugins.genericattributes.business.MandatoryError;
40  import fr.paris.lutece.plugins.genericattributes.business.Response;
41  import fr.paris.lutece.plugins.genericattributes.service.entrytype.AbstractEntryTypeFile;
42  import fr.paris.lutece.plugins.genericattributes.service.upload.AbstractGenAttUploadHandler;
43  import fr.paris.lutece.portal.business.file.File;
44  import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
45  import fr.paris.lutece.portal.service.fileupload.FileUploadService;
46  import fr.paris.lutece.portal.service.i18n.I18nService;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
49  import fr.paris.lutece.util.filesystem.FileSystemUtil;
50  
51  import org.apache.commons.fileupload.FileItem;
52  import org.apache.commons.lang3.StringUtils;
53  
54  import java.awt.image.BufferedImage;
55  
56  import java.io.ByteArrayInputStream;
57  import java.io.IOException;
58  
59  import java.util.List;
60  import java.util.Locale;
61  
62  import javax.imageio.ImageIO;
63  
64  import javax.servlet.http.HttpServletRequest;
65  
66  /**
67   *
68   * class EntryTypeImage
69   *
70   */
71  public class EntryTypeImage extends AbstractEntryTypeFile
72  {
73      /**
74       * Name of the bean of this service
75       */
76      public static final String BEAN_NAME = "announce.entryTypeImage";
77      private static final String MESSAGE_ERROR_NOT_AN_IMAGE = "announce.message.notAnImage";
78      private static final String TEMPLATE_CREATE = "admin/plugins/announce/entries/create_entry_type_image.html";
79      private static final String TEMPLATE_MODIFY = "admin/plugins/announce/entries/modify_entry_type_image.html";
80      private static final String TEMPLATE_HTML_CODE = "skin/plugins/announce/entries/html_code_entry_type_image.html";
81      private static final String TEMPLATE_HTML_CODE_ADMIN = "admin/plugins/announce/entries/html_code_entry_type_image.html";
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public String getTemplateHtmlForm( Entry entry, boolean bDisplayFront )
88      {
89          return bDisplayFront ? TEMPLATE_HTML_CODE : TEMPLATE_HTML_CODE_ADMIN;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public String getTemplateCreate( Entry entry, boolean bDisplayFront )
97      {
98          return TEMPLATE_CREATE;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public String getTemplateModify( Entry entry, boolean bDisplayFront )
106     {
107         return TEMPLATE_MODIFY;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
115     {
116         List<FileItem> listFilesSource = null;
117 
118         if ( request instanceof MultipartHttpServletRequest )
119         {
120 
121             String strIdEntry = Integer.toString( entry.getIdEntry( ) );
122             String strAttributePrefix = "attribute" + strIdEntry;
123 
124             List<FileItem> asynchronousFileItem = getFileSources( request, strAttributePrefix );
125 
126             if ( asynchronousFileItem != null )
127             {
128                 listFilesSource = asynchronousFileItem;
129             }
130 
131             GenericAttributeError formError = null;
132 
133             if ( ( listFilesSource != null ) && !listFilesSource.isEmpty( ) )
134             {
135                 formError = checkResponseData( entry, listFilesSource, locale, request );
136 
137                 if ( formError != null )
138                 {
139                     // Add the response to the list in order to have the error message in the page
140                     Response response = new Response( );
141                     response.setEntry( entry );
142                     listResponse.add( response );
143                 }
144 
145                 for ( FileItem fileItem : listFilesSource )
146                 {
147                     String strFilename = ( fileItem != null ) ? FileUploadService.getFileNameOnly( fileItem ) : StringUtils.EMPTY;
148 
149                     // Add the image to the response list
150                     Response response = new Response( );
151                     response.setEntry( entry );
152 
153                     if ( ( fileItem != null ) && ( fileItem.getSize( ) < Integer.MAX_VALUE ) )
154                     {
155                         PhysicalFile physicalFile = new PhysicalFile( );
156                         physicalFile.setValue( fileItem.get( ) );
157 
158                         File file = new File( );
159                         file.setPhysicalFile( physicalFile );
160                         file.setTitle( strFilename );
161                         file.setSize( (int) fileItem.getSize( ) );
162                         file.setMimeType( FileSystemUtil.getMIMEType( strFilename ) );
163 
164                         response.setFile( file );
165                     }
166 
167                     listResponse.add( response );
168 
169                     BufferedImage image = null;
170 
171                     try
172                     {
173                         if ( ( fileItem != null ) && ( fileItem.get( ) != null ) )
174                         {
175                             image = ImageIO.read( new ByteArrayInputStream( fileItem.get( ) ) );
176                         }
177                     }
178                     catch( IOException e )
179                     {
180                         AppLogService.error( e );
181                     }
182 
183                     if ( ( image == null ) && StringUtils.isNotBlank( strFilename ) )
184                     {
185                         formError = new GenericAttributeError( );
186                         formError.setMandatoryError( false );
187 
188                         Object [ ] args = {
189                                 ( fileItem != null ) ? fileItem.getName( ) : StringUtils.EMPTY
190                         };
191                         formError.setErrorMessage( I18nService.getLocalizedString( MESSAGE_ERROR_NOT_AN_IMAGE, args, request.getLocale( ) ) );
192                         formError.setTitleQuestion( entry.getTitle( ) );
193                     }
194                 }
195 
196                 return formError;
197             }
198 
199             if ( entry.isMandatory( ) && ( ( listFilesSource == null ) || listFilesSource.isEmpty( ) ) )
200             {
201                 formError = new MandatoryError( entry, locale );
202 
203                 Response response = new Response( );
204                 response.setEntry( entry );
205                 listResponse.add( response );
206             }
207 
208             return formError;
209         }
210 
211         return new MandatoryError( entry, locale );
212     }
213 
214     /**
215      * {@inheritDoc}
216      */
217     @Override
218     public AbstractGenAttUploadHandler getAsynchronousUploadHandler( )
219     {
220         return AnnounceAsynchronousUploadHandler.getHandler( );
221     }
222 
223     /**
224      * {@inheritDoc}
225      */
226     @Override
227     public String getUrlDownloadFile( int nResponseId, String strBaseUrl )
228     {
229         return getUrlDownloadImage( nResponseId, strBaseUrl );
230     }
231 
232     @Override
233     protected boolean checkForImages( )
234     {
235         return true;
236     }
237 }