View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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.directory.business;
35  
36  import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
37  import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
38  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
39  import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
40  import fr.paris.lutece.portal.service.fileupload.FileUploadService;
41  import fr.paris.lutece.portal.service.i18n.I18nService;
42  import fr.paris.lutece.portal.service.message.AdminMessage;
43  import fr.paris.lutece.portal.service.message.AdminMessageService;
44  import fr.paris.lutece.portal.service.plugin.Plugin;
45  import fr.paris.lutece.portal.service.plugin.PluginService;
46  import fr.paris.lutece.portal.service.regularexpression.RegularExpressionService;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
49  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
50  import fr.paris.lutece.util.ReferenceList;
51  import fr.paris.lutece.util.filesystem.FileSystemUtil;
52  import fr.paris.lutece.util.html.Paginator;
53  import fr.paris.lutece.util.image.ImageUtil;
54  
55  import org.apache.commons.collections.CollectionUtils;
56  import org.apache.commons.fileupload.FileItem;
57  import org.apache.commons.lang.StringUtils;
58  
59  import java.awt.image.BufferedImage;
60  
61  import java.io.ByteArrayInputStream;
62  import java.io.IOException;
63  
64  import java.util.ArrayList;
65  import java.util.List;
66  import java.util.Locale;
67  
68  import javax.imageio.ImageIO;
69  
70  import javax.servlet.http.HttpServletRequest;
71  
72  /**
73   *
74   * class EntryTypeImg
75   *
76   */
77  public class EntryTypeImg extends AbstractEntryTypeUpload
78  {
79      protected static final String FIELD_THUMBNAIL_WIDTH = "directory.create_entry.label_width";
80      protected static final String FIELD_THUMBNAIL_HEIGHT = "directory.create_entry.label_width";
81      protected static final String FIELD_BIG_THUMBNAIL_WIDTH = "directory.create_entry.label_width";
82      protected static final String FIELD_BIG_THUMBNAIL_HEIGHT = "directory.create_entry.label_width";
83      protected static final String ERROR_FIELD_THUMBNAIL = "directory.create_entry.label_error_thumbnail";
84      protected static final String ERROR_FIELD_BIG_THUMBNAIL = "directory.create_entry.label_error_big_thumbnail";
85      private static final String PARAMETER_THUMBNAIL_HEIGHT = "thumbnail_height";
86      private static final String PARAMETER_THUMBNAIL_WIDTH = "thumbnail_width";
87      private static final String PARAMETER_BIG_THUMBNAIL_WIDTH = "thumbnail_big_width";
88      private static final String PARAMETER_BIG_THUMBNAIL_HEIGHT = "thumbnail_big_height";
89      private static final String PARAMETER_IMAGE_SHOWN_IN_RESULT_LIST = "image_shown_in_result_list";
90      private static final String PARAMETER_IMAGE_SHOWN_IN_RESULT_RECORD = "image_shown_in_result_record";
91      private static final String PARAMETER_THUMBNAIL_SHOWN_IN_RESULT_LIST = "thbnail_shown_in_result_list";
92      private static final String PARAMETER_THUMBNAIL_SHOWN_IN_RESULT_RECORD = "thbnail_shown_in_result_record";
93      private static final String PARAMETER_BIG_THUMBNAIL_SHOWN_IN_RESULT_LIST = "big_thbnail_shown_in_result_list";
94      private static final String PARAMETER_BIG_THUMBNAIL_SHOWN_IN_RESULT_RECORD = "big_thbnail_shown_in_result_record";
95      private static final String PARAMETER_CREATE_THUMBNAIL = "create_thumbnail";
96      private static final String PARAMETER_CREATE_BIG_THUMBNAIL = "create_big_thumbnail";
97      private static final String MESSAGE_ERROR_NOT_AN_IMAGE = "directory.message.error.notAnImage";
98      private static final String FIELD_IMAGE = "image_full_size";
99      private static final String FIELD_THUMBNAIL = "little_thumbnail";
100     private static final String FIELD_BIG_THUMBNAIL = "big_thumbnail";
101     private final String PREFIX_ENTRY_ID = "directory_";
102     private static final int INTEGER_QUALITY_MAXIMUM = 1;
103     private final String _template_create = "admin/plugins/directory/entrytypeimg/create_entry_type_img.html";
104     private final String _template_modify = "admin/plugins/directory/entrytypeimg/modify_entry_type_img.html";
105     private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypeimg/html_code_form_entry_type_img.html";
106     private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypeimg/html_code_entry_value_type_img.html";
107     private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypeimg/html_code_form_entry_type_img.html";
108     private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypeimg/html_code_entry_value_type_img.html";
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public String getTemplateHtmlFormEntry( boolean isDisplayFront )
115     {
116         if ( isDisplayFront )
117         {
118             return _template_html_front_code_form_entry;
119         }
120 
121         return _template_html_code_form_entry;
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
129     {
130         if ( isDisplayFront )
131         {
132             return _template_html_front_code_entry_value;
133         }
134 
135         return _template_html_code_entry_value;
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public String getEntryData( HttpServletRequest request, Locale locale )
143     {
144         String strTitle = request.getParameter( PARAMETER_TITLE );
145         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
146         String strComment = request.getParameter( PARAMETER_COMMENT );
147         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
148         String strIndexed = request.getParameter( PARAMETER_INDEXED );
149         String strIndexedAsTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
150         String strIndexedAsSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
151 
152         String strDisplayWidth = request.getParameter( PARAMETER_DISPLAY_WIDTH );
153         String strDisplayHeight = request.getParameter( PARAMETER_DISPLAY_HEIGHT );
154 
155         // used for display image
156         int nDisplayWidth = DirectoryUtils.convertStringToInt( strDisplayWidth );
157         int nDisplayHeight = DirectoryUtils.convertStringToInt( strDisplayHeight );
158 
159         String strShowInFormMainSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
160         String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
161         String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
162         String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
163         String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
164 
165         String strError = this.checkEntryData( request, locale );
166 
167         if ( StringUtils.isNotBlank( strError ) )
168         {
169             return strError;
170         }
171 
172         this.setTitle( strTitle );
173         this.setHelpMessage( strHelpMessage );
174         this.setComment( strComment );
175 
176         this.setFields( request );
177 
178         this.setDisplayWidth( nDisplayWidth );
179         this.setDisplayHeight( nDisplayHeight );
180 
181         this.setMandatory( strMandatory != null );
182         this.setIndexed( strIndexed != null );
183         this.setIndexedAsTitle( strIndexedAsTitle != null );
184         this.setIndexedAsSummary( strIndexedAsSummary != null );
185         this.setShownInAdvancedSearch( strShowInFormMainSearch != null );
186         this.setShownInResultList( strShowInResultList != null );
187         this.setShownInResultRecord( strShowInResultRecord != null );
188         this.setShownInHistory( strShowInHistory != null );
189         this.setShownInCompleteness( strShowInCompleteness != null );
190         // image can't be exported in csv
191         this.setShownInExport( false );
192 
193         return null;
194     }
195 
196     /**
197      * {@inheritDoc}
198      */
199     @Override
200     public String getTemplateCreate( )
201     {
202         return _template_create;
203     }
204 
205     /**
206      * {@inheritDoc}
207      */
208     @Override
209     public String getTemplateModify( )
210     {
211         return _template_modify;
212     }
213 
214     /**
215      * {@inheritDoc}
216      */
217     @Override
218     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
219             Locale locale ) throws DirectoryErrorException
220     {
221         // add Empty recordField(Use for data import)
222         RecordField recordField = new RecordField( );
223         recordField.setEntry( this );
224         listRecordField.add( recordField );
225     }
226 
227     /**
228      * {@inheritDoc}
229      */
230     @Override
231     public void getImportRecordFieldData( Record record, String strImportValue, boolean bTestDirectoryError, List<RecordField> listRecordField, Locale locale )
232             throws DirectoryErrorException
233     {
234         // add Empty recordField
235         RecordField recordField = new RecordField( );
236         recordField.setEntry( this );
237         listRecordField.add( recordField );
238     }
239 
240     /**
241      * {@inheritDoc}
242      */
243     @Override
244     public void getRecordFieldData( Record record, HttpServletRequest request, boolean bTestDirectoryError, boolean bAddNewValue,
245             List<RecordField> listRecordField, Locale locale ) throws DirectoryErrorException
246     {
247         if ( request instanceof MultipartHttpServletRequest )
248         {
249             List<FileItem> fileItems = getFileSources( request );
250 
251             // if asynchronous file items is empty get the file in the multipart request
252             if ( CollectionUtils.isEmpty( fileItems ) )
253             {
254                 FileItem fileItem = ( (MultipartHttpServletRequest) request ).getFile( PREFIX_ENTRY_ID + this.getIdEntry( ) );
255 
256                 if ( fileItem != null )
257                 {
258                     fileItems = new ArrayList<FileItem>( );
259                     fileItems.add( fileItem );
260                 }
261             }
262 
263             if ( ( fileItems != null ) && !fileItems.isEmpty( ) )
264             {
265                 // Checks
266                 if ( bTestDirectoryError )
267                 {
268                     this.checkRecordFieldData( fileItems, locale );
269                 }
270 
271                 // The index is used to distinguish the thumbnails of one image from another
272                 int nIndex = 0;
273 
274                 for ( FileItem fileItem : fileItems )
275                 {
276                     String strFilename = ( fileItem != null ) ? FileUploadService.getFileNameOnly( fileItem ) : StringUtils.EMPTY;
277 
278                     if ( ( fileItem != null ) && ( fileItem.get( ) != null ) && ( fileItem.getSize( ) < Integer.MAX_VALUE ) )
279                     {
280                         PhysicalFile physicalFile = new PhysicalFile( );
281                         physicalFile.setValue( fileItem.get( ) );
282 
283                         File file = new File( );
284                         file.setPhysicalFile( physicalFile );
285                         file.setTitle( strFilename );
286                         file.setSize( (int) fileItem.getSize( ) );
287                         file.setMimeType( FileSystemUtil.getMIMEType( strFilename ) );
288 
289                         // Add the image to the record fields list
290                         RecordField recordField = new RecordField( );
291                         recordField.setEntry( this );
292                         recordField.setValue( FIELD_IMAGE + DirectoryUtils.CONSTANT_UNDERSCORE + nIndex );
293                         recordField.setFile( file );
294 
295                         Field fullsizedField = FieldHome.findByValue( this.getIdEntry( ), FIELD_IMAGE, PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME ) );
296 
297                         if ( fullsizedField != null )
298                         {
299                             recordField.setField( fullsizedField );
300                         }
301 
302                         listRecordField.add( recordField );
303 
304                         // Create thumbnails records
305                         File imageFile = recordField.getFile( );
306                         Field thumbnailField = FieldHome.findByValue( this.getIdEntry( ), FIELD_THUMBNAIL,
307                                 PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME ) );
308 
309                         if ( thumbnailField != null )
310                         {
311                             byte [ ] resizedImage = ImageUtil.resizeImage( imageFile.getPhysicalFile( ).getValue( ),
312                                     String.valueOf( thumbnailField.getWidth( ) ), String.valueOf( thumbnailField.getHeight( ) ), INTEGER_QUALITY_MAXIMUM );
313 
314                             RecordField thbnailRecordField = new RecordField( );
315                             thbnailRecordField.setEntry( this );
316 
317                             PhysicalFile thbnailPhysicalFile = new PhysicalFile( );
318                             thbnailPhysicalFile.setValue( resizedImage );
319 
320                             File thbnailFile = new File( );
321                             thbnailFile.setTitle( imageFile.getTitle( ) );
322                             thbnailFile.setExtension( imageFile.getExtension( ) );
323 
324                             if ( ( imageFile.getExtension( ) != null ) && ( imageFile.getTitle( ) != null ) )
325                             {
326                                 thbnailFile.setMimeType( FileSystemUtil.getMIMEType( imageFile.getTitle( ) ) );
327                             }
328 
329                             thbnailFile.setPhysicalFile( thbnailPhysicalFile );
330                             thbnailFile.setSize( resizedImage.length );
331 
332                             thbnailRecordField.setFile( thbnailFile );
333 
334                             thbnailRecordField.setRecord( record );
335                             thbnailRecordField.setValue( FIELD_THUMBNAIL + DirectoryUtils.CONSTANT_UNDERSCORE + nIndex );
336                             thbnailRecordField.setField( thumbnailField );
337                             listRecordField.add( thbnailRecordField );
338                         }
339 
340                         Field bigThumbnailField = FieldHome.findByValue( this.getIdEntry( ), FIELD_BIG_THUMBNAIL,
341                                 PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME ) );
342 
343                         if ( bigThumbnailField != null )
344                         {
345                             byte [ ] resizedImage = ImageUtil.resizeImage( imageFile.getPhysicalFile( ).getValue( ),
346                                     String.valueOf( bigThumbnailField.getWidth( ) ), String.valueOf( bigThumbnailField.getHeight( ) ), INTEGER_QUALITY_MAXIMUM );
347 
348                             RecordField bigThbnailRecordField = new RecordField( );
349                             bigThbnailRecordField.setEntry( this );
350 
351                             PhysicalFile thbnailPhysicalFile = new PhysicalFile( );
352                             thbnailPhysicalFile.setValue( resizedImage );
353 
354                             File thbnailFile = new File( );
355                             thbnailFile.setTitle( imageFile.getTitle( ) );
356                             thbnailFile.setExtension( imageFile.getExtension( ) );
357 
358                             if ( ( imageFile.getExtension( ) != null ) && ( imageFile.getTitle( ) != null ) )
359                             {
360                                 thbnailFile.setMimeType( FileSystemUtil.getMIMEType( imageFile.getTitle( ) ) );
361                             }
362 
363                             thbnailFile.setPhysicalFile( thbnailPhysicalFile );
364                             thbnailFile.setSize( resizedImage.length );
365 
366                             bigThbnailRecordField.setFile( thbnailFile );
367 
368                             bigThbnailRecordField.setRecord( record );
369                             bigThbnailRecordField.setValue( FIELD_BIG_THUMBNAIL + DirectoryUtils.CONSTANT_UNDERSCORE + nIndex );
370                             bigThbnailRecordField.setField( bigThumbnailField );
371                             listRecordField.add( bigThbnailRecordField );
372                         }
373                     }
374 
375                     nIndex++;
376                 }
377             }
378 
379             if ( bTestDirectoryError && this.isMandatory( ) && ( ( fileItems == null ) || fileItems.isEmpty( ) ) )
380             {
381                 RecordField recordField = new RecordField( );
382                 recordField.setEntry( this );
383                 recordField.setValue( FIELD_IMAGE );
384                 listRecordField.add( recordField );
385 
386                 throw new DirectoryErrorException( this.getTitle( ) );
387             }
388         }
389         else
390             if ( bTestDirectoryError )
391             {
392                 throw new DirectoryErrorException( this.getTitle( ) );
393             }
394     }
395 
396     /**
397      * {@inheritDoc}
398      */
399     @Override
400     public Paginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex )
401     {
402         return new Paginator( this.getFields( ).get( 0 ).getRegularExpressionList( ), nItemPerPage, strBaseUrl, strPageIndexParameterName, strPageIndex );
403     }
404 
405     /**
406      * {@inheritDoc}
407      */
408     @Override
409     public ReferenceList getReferenceListRegularExpression( IEntry entry, Plugin plugin )
410     {
411         ReferenceList refListRegularExpression = null;
412 
413         if ( RegularExpressionService.getInstance( ).isAvailable( ) )
414         {
415             refListRegularExpression = new ReferenceList( );
416 
417             List<RegularExpression> listRegularExpression = RegularExpressionService.getInstance( ).getAllRegularExpression( );
418 
419             for ( RegularExpression regularExpression : listRegularExpression )
420             {
421                 if ( !entry.getFields( ).get( 0 ).getRegularExpressionList( ).contains( regularExpression ) )
422                 {
423                     refListRegularExpression.addItem( regularExpression.getIdExpression( ), regularExpression.getTitle( ) );
424                 }
425             }
426         }
427 
428         return refListRegularExpression;
429     }
430 
431     /**
432      * {@inheritDoc}
433      */
434     @Override
435     public LocalizedPaginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex, Locale locale )
436     {
437         return new LocalizedPaginator( this.getFields( ).get( 0 ).getRegularExpressionList( ), nItemPerPage, strBaseUrl, strPageIndexParameterName,
438                 strPageIndex, locale );
439     }
440 
441     // PROTECTED METHODS
442 
443     /**
444      * {@inheritDoc}
445      */
446     @Override
447     protected String checkEntryData( HttpServletRequest request, Locale locale )
448     {
449         String strError = super.checkEntryData( request, locale );
450 
451         if ( StringUtils.isBlank( strError ) )
452         {
453             String strFieldError = StringUtils.EMPTY;
454             String strTitle = request.getParameter( PARAMETER_TITLE );
455             String strCreateThumbnail = request.getParameter( PARAMETER_CREATE_THUMBNAIL );
456             String strCreateBigThumbnail = request.getParameter( PARAMETER_CREATE_BIG_THUMBNAIL );
457 
458             String strDisplayWidth = request.getParameter( PARAMETER_DISPLAY_WIDTH );
459             String strDisplayHeight = request.getParameter( PARAMETER_DISPLAY_HEIGHT );
460             String strThumbnailWidth = request.getParameter( PARAMETER_THUMBNAIL_WIDTH );
461             String strThumbnailHeight = request.getParameter( PARAMETER_THUMBNAIL_HEIGHT );
462             String strBigThumbnailWidth = request.getParameter( PARAMETER_BIG_THUMBNAIL_WIDTH );
463             String strBigThumbnailHeight = request.getParameter( PARAMETER_BIG_THUMBNAIL_HEIGHT );
464 
465             int nDisplayWidth = DirectoryUtils.convertStringToInt( strDisplayWidth );
466             int nDisplayHeight = DirectoryUtils.convertStringToInt( strDisplayHeight );
467             int nThumbnailWidth = DirectoryUtils.CONSTANT_ID_NULL;
468             int nThumbnailHeight = DirectoryUtils.CONSTANT_ID_NULL;
469             int nBigThumbnailHeight = DirectoryUtils.CONSTANT_ID_NULL;
470             int nBigThumbnailWidth = DirectoryUtils.CONSTANT_ID_NULL;
471 
472             if ( strThumbnailWidth != null )
473             {
474                 nThumbnailWidth = DirectoryUtils.convertStringToInt( strThumbnailWidth );
475             }
476 
477             if ( strThumbnailHeight != null )
478             {
479                 nThumbnailHeight = DirectoryUtils.convertStringToInt( strThumbnailHeight );
480             }
481 
482             if ( strBigThumbnailWidth != null )
483             {
484                 nBigThumbnailWidth = DirectoryUtils.convertStringToInt( strBigThumbnailWidth );
485             }
486 
487             if ( strBigThumbnailHeight != null )
488             {
489                 nBigThumbnailHeight = DirectoryUtils.convertStringToInt( strBigThumbnailHeight );
490             }
491 
492             if ( StringUtils.isBlank( strTitle ) )
493             {
494                 strFieldError = FIELD_TITLE;
495             }
496             else
497                 if ( ( strCreateThumbnail != null ) && ( strThumbnailWidth == null ) )
498                 {
499                     strFieldError = FIELD_THUMBNAIL_WIDTH;
500                 }
501                 else
502                     if ( ( strCreateThumbnail != null ) && ( strThumbnailHeight == null ) )
503                     {
504                         strFieldError = FIELD_THUMBNAIL_HEIGHT;
505                     }
506                     else
507                         if ( ( strCreateBigThumbnail != null ) && ( strThumbnailHeight == null ) )
508                         {
509                             strFieldError = FIELD_BIG_THUMBNAIL_HEIGHT;
510                         }
511                         else
512                             if ( ( strCreateBigThumbnail != null ) && ( strThumbnailWidth == null ) )
513                             {
514                                 strFieldError = FIELD_BIG_THUMBNAIL_WIDTH;
515                             }
516 
517             if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
518             {
519                 Object [ ] tabRequiredFields = {
520                     I18nService.getLocalizedString( strFieldError, locale )
521                 };
522 
523                 return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
524             }
525 
526             if ( ( strDisplayWidth != null ) && ( !strDisplayWidth.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
527                     && ( nDisplayWidth == DirectoryUtils.CONSTANT_ID_NULL ) )
528             {
529                 strFieldError = FIELD_WIDTH_DISPLAY;
530             }
531             else
532                 if ( ( strDisplayHeight != null ) && ( !strDisplayHeight.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
533                         && ( nDisplayHeight == DirectoryUtils.CONSTANT_ID_NULL ) )
534                 {
535                     strFieldError = FIELD_HEIGHT_DISPLAY;
536                 }
537 
538             if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
539             {
540                 Object [ ] tabRequiredFields = {
541                     I18nService.getLocalizedString( strFieldError, locale )
542                 };
543 
544                 return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
545             }
546 
547             if ( ( strCreateThumbnail != null ) && ( ( nThumbnailWidth <= 0 ) || ( nThumbnailHeight <= 0 ) ) )
548             {
549                 strError = AdminMessageService.getMessageUrl( request, ERROR_FIELD_THUMBNAIL, AdminMessage.TYPE_STOP );
550             }
551 
552             if ( ( strCreateBigThumbnail != null ) && ( ( nBigThumbnailWidth <= 0 ) || ( nBigThumbnailHeight <= 0 ) ) )
553             {
554                 strError = AdminMessageService.getMessageUrl( request, ERROR_FIELD_BIG_THUMBNAIL, AdminMessage.TYPE_STOP );
555             }
556 
557             if ( StringUtils.isNotBlank( strFieldError ) )
558             {
559                 Object [ ] tabRequiredFields = {
560                     I18nService.getLocalizedString( strFieldError, locale )
561                 };
562 
563                 return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
564             }
565         }
566 
567         return strError;
568     }
569 
570     /**
571      * {@inheritDoc}
572      */
573     @Override
574     protected void setFields( HttpServletRequest request, List<Field> listFields )
575     {
576         String strCreateThumbnail = request.getParameter( PARAMETER_CREATE_THUMBNAIL );
577         String strCreateBigThumbnail = request.getParameter( PARAMETER_CREATE_BIG_THUMBNAIL );
578         listFields.add( buildFieldFullSize( request ) );
579 
580         if ( strCreateThumbnail != null )
581         {
582             // If the checkbox to create a thumbnail is checked, then add the associated field
583             listFields.add( buildFieldThumbnail( request ) );
584         }
585         else
586         {
587             // Otherwise, remove from db
588             Field fieldThumbnail = DirectoryUtils.findFieldByValueInTheList( FIELD_THUMBNAIL, getFields( ) );
589 
590             if ( fieldThumbnail != null )
591             {
592                 RecordFieldFilter filter = new RecordFieldFilter( );
593                 filter.setIdField( fieldThumbnail.getIdField( ) );
594                 FieldHome.remove( fieldThumbnail.getIdField( ), DirectoryUtils.getPlugin( ) );
595                 RecordFieldHome.removeByFilter( filter, DirectoryUtils.getPlugin( ) );
596             }
597         }
598 
599         if ( strCreateBigThumbnail != null )
600         {
601             // If the checkbox to create a big thumbnail is checked, then add the associated field
602             listFields.add( buildFieldBigThumbnail( request ) );
603         }
604         else
605         {
606             // Otherwise, remove from db
607             Field fieldBigThumbnail = DirectoryUtils.findFieldByValueInTheList( FIELD_BIG_THUMBNAIL, getFields( ) );
608 
609             if ( fieldBigThumbnail != null )
610             {
611                 RecordFieldFilter filter = new RecordFieldFilter( );
612                 filter.setIdField( fieldBigThumbnail.getIdField( ) );
613                 FieldHome.remove( fieldBigThumbnail.getIdField( ), DirectoryUtils.getPlugin( ) );
614                 RecordFieldHome.removeByFilter( filter, DirectoryUtils.getPlugin( ) );
615             }
616         }
617     }
618 
619     /**
620      * {@inheritDoc}
621      */
622     @Override
623     protected void checkRecordFieldData( FileItem fileItem, Locale locale ) throws DirectoryErrorException
624     {
625         String strFilename = ( fileItem != null ) ? FileUploadService.getFileNameOnly( fileItem ) : StringUtils.EMPTY;
626         BufferedImage image = null;
627 
628         try
629         {
630             if ( ( fileItem != null ) && ( fileItem.get( ) != null ) )
631             {
632                 image = ImageIO.read( new ByteArrayInputStream( fileItem.get( ) ) );
633             }
634         }
635         catch( IOException e )
636         {
637             AppLogService.error( e );
638         }
639 
640         if ( ( image == null ) && StringUtils.isNotBlank( strFilename ) )
641         {
642             String strErrorMessage = I18nService.getLocalizedString( MESSAGE_ERROR_NOT_AN_IMAGE, locale );
643             throw new DirectoryErrorException( this.getTitle( ), strErrorMessage );
644         }
645     }
646 
647     // PRIVATE METHODS
648 
649     /**
650      * Build the field full size
651      * 
652      * @param request
653      *            the HTTP request
654      * @return the field
655      */
656     private Field buildFieldFullSize( HttpServletRequest request )
657     {
658         Field fieldFullImage = DirectoryUtils.findFieldByValueInTheList( FIELD_IMAGE, getFields( ) );
659 
660         if ( fieldFullImage == null )
661         {
662             fieldFullImage = new Field( );
663         }
664 
665         if ( request.getParameter( PARAMETER_IMAGE_SHOWN_IN_RESULT_LIST ) != null )
666         {
667             fieldFullImage.setShownInResultList( true );
668         }
669         else
670         {
671             fieldFullImage.setShownInResultList( false );
672         }
673 
674         if ( request.getParameter( PARAMETER_IMAGE_SHOWN_IN_RESULT_RECORD ) != null )
675         {
676             fieldFullImage.setShownInResultRecord( true );
677         }
678         else
679         {
680             fieldFullImage.setShownInResultRecord( false );
681         }
682 
683         fieldFullImage.setEntry( this );
684         fieldFullImage.setValue( FIELD_IMAGE );
685 
686         return fieldFullImage;
687     }
688 
689     /**
690      * Build the field thumbnail
691      * 
692      * @param request
693      *            the HTTP request
694      * @return the field
695      */
696     private Field buildFieldThumbnail( HttpServletRequest request )
697     {
698         String strThumbnailWidth = request.getParameter( PARAMETER_THUMBNAIL_WIDTH );
699         String strThumbnailHeight = request.getParameter( PARAMETER_THUMBNAIL_HEIGHT );
700         int nThumbnailWidth = DirectoryUtils.CONSTANT_ID_NULL;
701         int nThumbnailHeight = DirectoryUtils.CONSTANT_ID_NULL;
702 
703         if ( strThumbnailWidth != null )
704         {
705             nThumbnailWidth = DirectoryUtils.convertStringToInt( strThumbnailWidth );
706         }
707 
708         if ( strThumbnailHeight != null )
709         {
710             nThumbnailHeight = DirectoryUtils.convertStringToInt( strThumbnailHeight );
711         }
712 
713         Field fieldThumbnail = DirectoryUtils.findFieldByValueInTheList( FIELD_THUMBNAIL, getFields( ) );
714 
715         if ( fieldThumbnail == null )
716         {
717             fieldThumbnail = new Field( );
718         }
719 
720         fieldThumbnail.setWidth( nThumbnailWidth );
721         fieldThumbnail.setHeight( nThumbnailHeight );
722         fieldThumbnail.setValue( FIELD_THUMBNAIL );
723         fieldThumbnail.setEntry( this );
724         fieldThumbnail.setShownInResultList( request.getParameter( PARAMETER_THUMBNAIL_SHOWN_IN_RESULT_LIST ) != null );
725         fieldThumbnail.setShownInResultRecord( request.getParameter( PARAMETER_THUMBNAIL_SHOWN_IN_RESULT_RECORD ) != null );
726 
727         return fieldThumbnail;
728     }
729 
730     /**
731      * Build the field for big thumbnail
732      * 
733      * @param request
734      *            the HTTP request
735      * @return the field
736      */
737     private Field buildFieldBigThumbnail( HttpServletRequest request )
738     {
739         String strBigThumbnailWidth = request.getParameter( PARAMETER_BIG_THUMBNAIL_WIDTH );
740         String strBigThumbnailHeight = request.getParameter( PARAMETER_BIG_THUMBNAIL_HEIGHT );
741 
742         int nBigThumbnailHeight = DirectoryUtils.CONSTANT_ID_NULL;
743         int nBigThumbnailWidth = DirectoryUtils.CONSTANT_ID_NULL;
744 
745         if ( strBigThumbnailWidth != null )
746         {
747             nBigThumbnailWidth = DirectoryUtils.convertStringToInt( strBigThumbnailWidth );
748         }
749 
750         if ( strBigThumbnailHeight != null )
751         {
752             nBigThumbnailHeight = DirectoryUtils.convertStringToInt( strBigThumbnailHeight );
753         }
754 
755         Field bigThumbnailField = DirectoryUtils.findFieldByValueInTheList( FIELD_BIG_THUMBNAIL, getFields( ) );
756 
757         if ( bigThumbnailField == null )
758         {
759             bigThumbnailField = new Field( );
760         }
761 
762         bigThumbnailField.setWidth( nBigThumbnailWidth );
763         bigThumbnailField.setHeight( nBigThumbnailHeight );
764         bigThumbnailField.setValue( FIELD_BIG_THUMBNAIL );
765         bigThumbnailField.setEntry( this );
766         bigThumbnailField.setShownInResultList( request.getParameter( PARAMETER_BIG_THUMBNAIL_SHOWN_IN_RESULT_LIST ) != null );
767         bigThumbnailField.setShownInResultRecord( request.getParameter( PARAMETER_BIG_THUMBNAIL_SHOWN_IN_RESULT_RECORD ) != null );
768 
769         return bigThumbnailField;
770     }
771 }