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.utils.DirectoryErrorException;
37  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
38  import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
39  import fr.paris.lutece.portal.service.fileupload.FileUploadService;
40  import fr.paris.lutece.portal.service.i18n.I18nService;
41  import fr.paris.lutece.portal.service.message.AdminMessage;
42  import fr.paris.lutece.portal.service.message.AdminMessageService;
43  import fr.paris.lutece.portal.service.plugin.Plugin;
44  import fr.paris.lutece.portal.service.regularexpression.RegularExpressionService;
45  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
46  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
47  import fr.paris.lutece.util.ReferenceList;
48  import fr.paris.lutece.util.filesystem.FileSystemUtil;
49  import fr.paris.lutece.util.html.Paginator;
50  import fr.paris.lutece.util.url.UrlItem;
51  
52  import org.apache.commons.collections.CollectionUtils;
53  import org.apache.commons.fileupload.FileItem;
54  import org.apache.commons.lang.StringUtils;
55  
56  import java.util.ArrayList;
57  import java.util.List;
58  import java.util.Locale;
59  
60  import javax.servlet.http.HttpServletRequest;
61  
62  /**
63   *
64   * class EntryTypeFile
65   *
66   */
67  public class EntryTypeFile extends AbstractEntryTypeUpload
68  {
69      private final String _template_create = "admin/plugins/directory/entrytypefile/create_entry_type_file.html";
70      private final String _template_modify = "admin/plugins/directory/entrytypefile/modify_entry_type_file.html";
71      private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypefile/html_code_form_entry_type_file.html";
72      private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypefile/html_code_entry_value_type_file.html";
73      private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypefile/html_code_form_entry_type_file.html";
74      private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypefile/html_code_entry_value_type_file.html";
75      private final String PREFIX_ENTRY_ID = "directory_";
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public String getTemplateHtmlFormEntry( boolean isDisplayFront )
82      {
83          if ( isDisplayFront )
84          {
85              return _template_html_front_code_form_entry;
86          }
87  
88          return _template_html_code_form_entry;
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
96      {
97          if ( isDisplayFront )
98          {
99              return _template_html_front_code_entry_value;
100         }
101 
102         return _template_html_code_entry_value;
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public String getEntryData( HttpServletRequest request, Locale locale )
110     {
111         String strTitle = request.getParameter( PARAMETER_TITLE );
112         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
113         String strComment = request.getParameter( PARAMETER_COMMENT );
114         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
115         String strIndexed = request.getParameter( PARAMETER_INDEXED );
116         String strIndexedAsTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
117         String strIndexedAsSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
118         String strShowInFormMainSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
119         String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
120         String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
121         String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
122         String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
123         String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
124 
125         String strError = this.checkEntryData( request, locale );
126 
127         if ( StringUtils.isNotBlank( strError ) )
128         {
129             return strError;
130         }
131 
132         this.setTitle( strTitle );
133         this.setHelpMessage( strHelpMessage );
134         this.setComment( strComment );
135 
136         this.setFields( request );
137 
138         this.setMandatory( strMandatory != null );
139         this.setIndexed( strIndexed != null );
140         this.setIndexedAsTitle( strIndexedAsTitle != null );
141         this.setIndexedAsSummary( strIndexedAsSummary != null );
142         this.setShownInAdvancedSearch( strShowInFormMainSearch != null );
143         this.setShownInResultList( strShowInResultList != null );
144         this.setShownInResultRecord( strShowInResultRecord != null );
145         this.setShownInHistory( strShowInHistory != null );
146         this.setShownInExport( strShowInExport != null );
147         this.setShownInCompleteness( strShowInCompleteness != null );
148 
149         return null;
150     }
151 
152     /**
153      * {@inheritDoc}
154      */
155     @Override
156     public String getTemplateCreate( )
157     {
158         return _template_create;
159     }
160 
161     /**
162      * {@inheritDoc}
163      */
164     @Override
165     public String getTemplateModify( )
166     {
167         return _template_modify;
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
175             Locale locale ) throws DirectoryErrorException
176     {
177         // Add Empty recordField(Use for data import)
178         RecordField recordField = new RecordField( );
179         recordField.setEntry( this );
180         listRecordField.add( recordField );
181     }
182 
183     /**
184      * {@inheritDoc}
185      */
186     @Override
187     public void getImportRecordFieldData( Record record, byte [ ] strImportValue, String nomFile, boolean bTestDirectoryError,
188             List<RecordField> listRecordField, Locale locale ) throws DirectoryErrorException
189     {
190         // Create a file with the data of the pdf file, the file will then be imported
191         RecordField recordField = new RecordField( );
192         recordField.setEntry( this );
193 
194         File file2 = new File( );
195         file2.setExtension( "pdf" );
196         file2.setMimeType( "application/pdf" );
197         file2.setTitle( nomFile );
198         file2.setSize( strImportValue.length );
199 
200         PhysicalFile ph = new PhysicalFile( );
201         ph.setValue( strImportValue );
202         file2.setPhysicalFile( ph );
203         recordField.setFile( file2 );
204         listRecordField.add( recordField );
205     }
206 
207     /**
208      * {@inheritDoc}
209      */
210     @Override
211     public void getRecordFieldData( Record record, HttpServletRequest request, boolean bTestDirectoryError, boolean bAddNewValue,
212             List<RecordField> listRecordField, Locale locale ) throws DirectoryErrorException
213     {
214         if ( request instanceof MultipartHttpServletRequest )
215         {
216             // get asynchronous file items
217             List<FileItem> fileItems = getFileSources( request );
218 
219             // if asynchronous file items is empty get the file in the multipart request
220             if ( CollectionUtils.isEmpty( fileItems ) )
221             {
222                 FileItem fileItem = ( (MultipartHttpServletRequest) request ).getFile( PREFIX_ENTRY_ID + this.getIdEntry( ) );
223 
224                 if ( fileItem != null && StringUtils.isNotBlank( fileItem.getName( ) ) )
225                 {
226                     fileItems = new ArrayList<FileItem>( );
227                     fileItems.add( fileItem );
228                 }
229             }
230 
231             if ( ( fileItems != null ) && !fileItems.isEmpty( ) )
232             {
233                 // Checks
234                 if ( bTestDirectoryError )
235                 {
236                     this.checkRecordFieldData( fileItems, locale );
237                 }
238 
239                 for ( FileItem fileItem : fileItems )
240                 {
241                     String strFilename = ( fileItem != null ) ? FileUploadService.getFileNameOnly( fileItem ) : StringUtils.EMPTY;
242 
243                     // Add the file to the record fields list
244                     RecordField recordField = new RecordField( );
245                     recordField.setEntry( this );
246 
247                     if ( ( fileItem != null ) && ( fileItem.get( ) != null ) && ( fileItem.getSize( ) < Integer.MAX_VALUE ) )
248                     {
249                         PhysicalFile physicalFile = new PhysicalFile( );
250                         physicalFile.setValue( fileItem.get( ) );
251 
252                         File file = new File( );
253                         file.setPhysicalFile( physicalFile );
254                         file.setTitle( strFilename );
255                         file.setSize( (int) fileItem.getSize( ) );
256                         file.setMimeType( FileSystemUtil.getMIMEType( strFilename ) );
257 
258                         recordField.setFile( file );
259                     }
260 
261                     listRecordField.add( recordField );
262                 }
263             }
264             else
265                 if ( bTestDirectoryError && this.isMandatory( ) )
266                 {
267                     throw new DirectoryErrorException( this.getTitle( ) );
268                 }
269         }
270         else
271             if ( bTestDirectoryError )
272             {
273                 throw new DirectoryErrorException( this.getTitle( ) );
274             }
275     }
276 
277     /**
278      * {@inheritDoc}
279      */
280     @Override
281     public Paginator<RegularExpression> getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex )
282     {
283         return new Paginator<RegularExpression>( this.getFields( ).get( 0 ).getRegularExpressionList( ), nItemPerPage, strBaseUrl, strPageIndexParameterName,
284                 strPageIndex );
285     }
286 
287     /**
288      * {@inheritDoc}
289      */
290     @Override
291     public ReferenceList getReferenceListRegularExpression( IEntry entry, Plugin plugin )
292     {
293         ReferenceList refListRegularExpression = null;
294 
295         if ( RegularExpressionService.getInstance( ).isAvailable( ) )
296         {
297             refListRegularExpression = new ReferenceList( );
298 
299             List<RegularExpression> listRegularExpression = RegularExpressionService.getInstance( ).getAllRegularExpression( );
300 
301             for ( RegularExpression regularExpression : listRegularExpression )
302             {
303                 if ( !entry.getFields( ).get( 0 ).getRegularExpressionList( ).contains( regularExpression ) )
304                 {
305                     refListRegularExpression.addItem( regularExpression.getIdExpression( ), regularExpression.getTitle( ) );
306                 }
307             }
308         }
309 
310         return refListRegularExpression;
311     }
312 
313     /**
314      * {@inheritDoc}
315      */
316     @Override
317     public LocalizedPaginator<RegularExpression> getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex,
318             Locale locale )
319     {
320         return new LocalizedPaginator<RegularExpression>( this.getFields( ).get( 0 ).getRegularExpressionList( ), nItemPerPage, strBaseUrl,
321                 strPageIndexParameterName, strPageIndex, locale );
322     }
323 
324     /**
325      * {@inheritDoc}
326      */
327     @Override
328     public String convertRecordFieldValueToString( RecordField recordField, Locale locale, boolean bDisplayFront, boolean bExportDirectory )
329     {
330         if ( recordField.getFile( ) != null )
331         {
332             UrlItem url = new UrlItem( DirectoryUtils.getBaseUrl( null ) + JSP_DOWNLOAD_FILE );
333             url.addParameter( DirectoryUtils.PARAMETER_ID_FILE, recordField.getFile( ).getIdFile( ) );
334 
335             return url.getUrl( );
336         }
337 
338         if ( StringUtils.isNotBlank( recordField.getValue( ) ) )
339         {
340             return recordField.getValue( );
341         }
342 
343         return StringUtils.EMPTY;
344     }
345 
346     /**
347      * {@inheritDoc}
348      */
349     @Override
350     protected String checkEntryData( HttpServletRequest request, Locale locale )
351     {
352         String strError = super.checkEntryData( request, locale );
353 
354         if ( StringUtils.isBlank( strError ) )
355         {
356             String strWidth = request.getParameter( PARAMETER_WIDTH );
357 
358             if ( StringUtils.isBlank( strWidth ) )
359             {
360                 Object [ ] tabRequiredFields = {
361                     I18nService.getLocalizedString( FIELD_WIDTH, locale )
362                 };
363 
364                 return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
365             }
366             else
367                 if ( !StringUtils.isNumeric( strWidth ) )
368                 {
369                     Object [ ] tabRequiredFields = {
370                         I18nService.getLocalizedString( FIELD_WIDTH, locale )
371                     };
372 
373                     return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
374                 }
375         }
376 
377         return strError;
378     }
379 
380     /**
381      * {@inheritDoc}
382      */
383     @Override
384     protected void setFields( HttpServletRequest request, List<Field> listFields )
385     {
386         Field field = buildDefaultField( request );
387 
388         listFields.add( field );
389     }
390 
391     /**
392      * {@inheritDoc}
393      */
394     @Override
395     protected void checkRecordFieldData( FileItem fileItem, Locale locale ) throws DirectoryErrorException
396     {
397         // Do nothing
398     }
399 
400     // PRIVATE METHODS
401 
402     /**
403      * Build the default field
404      * 
405      * @param request
406      *            the HTTP request
407      * @return the default field
408      */
409     private Field buildDefaultField( HttpServletRequest request )
410     {
411         String strWidth = request.getParameter( PARAMETER_WIDTH );
412         int nWidth = DirectoryUtils.convertStringToInt( strWidth );
413 
414         Field field = DirectoryUtils.findFieldByTitleInTheList( null, getFields( ) );
415 
416         if ( field == null )
417         {
418             field = new Field( );
419         }
420 
421         field.setEntry( this );
422         field.setWidth( nWidth );
423 
424         return field;
425     }
426 }