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.service.directorysearch.DirectorySearchItem;
38  import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
39  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
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.PluginService;
44  
45  import org.apache.commons.lang.StringUtils;
46  
47  import java.util.ArrayList;
48  import java.util.HashMap;
49  import java.util.List;
50  import java.util.Locale;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  /**
55   *
56   * class EntryTypeDate
57   *
58   */
59  public class EntryTypeArray extends Entry
60  {
61      private final String _template_create = "admin/plugins/directory/entrytypearray/create_entry_type_array.html";
62      private final String _template_modify = "admin/plugins/directory/entrytypearray/modify_entry_type_array.html";
63      private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypearray/html_code_form_entry_type_array.html";
64      private final String _template_html_code_form_search_entry = "admin/plugins/directory/entrytypearray/html_code_form_search_entry_type_array.html";
65      private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypearray/html_code_entry_value_type_array.html";
66      private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypearray/html_code_form_entry_type_array.html";
67      private final String _template_html_front_code_form_search_entry = "skin/plugins/directory/entrytypearray/html_code_form_search_entry_type_array.html";
68      private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypearray/html_code_entry_value_type_array.html";
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public String getTemplateHtmlFormEntry( boolean isDisplayFront )
75      {
76          if ( isDisplayFront )
77          {
78              return _template_html_front_code_form_entry;
79          }
80  
81          return _template_html_code_form_entry;
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
89      {
90          if ( isDisplayFront )
91          {
92              return _template_html_front_code_entry_value;
93          }
94  
95          return _template_html_code_entry_value;
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
103     {
104         if ( isDisplayFront )
105         {
106             return _template_html_front_code_form_search_entry;
107         }
108 
109         return _template_html_code_form_search_entry;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public String getEntryData( HttpServletRequest request, Locale locale )
117     {
118         String strTitle = request.getParameter( PARAMETER_TITLE );
119         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
120         String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
121                 .trim( ) : null;
122         String strComment = request.getParameter( PARAMETER_COMMENT );
123         String strIndexed = request.getParameter( PARAMETER_INDEXED );
124         String strIndexedAsTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
125         String strIndexedAsSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
126         String strShowInAdvancedSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
127         String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
128         String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
129         String strMultipleSearchFields = request.getParameter( PARAMETER_MULTIPLE_SEARCH_FIELDS );
130         String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
131         String strFieldError = DirectoryUtils.EMPTY_STRING;
132         String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
133         String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
134         String strNumberRows = request.getParameter( PARAMETER_NUMBER_ROWS );
135         String strNumberColumns = request.getParameter( PARAMETER_NUMBER_COLUMNS );
136 
137         if ( ( strTitle == null ) || strTitle.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
138         {
139             strFieldError = FIELD_TITLE;
140         }
141         else
142             if ( StringUtils.isEmpty( strNumberRows ) )
143             {
144                 strFieldError = FIELD_NUMBER_ROWS;
145             }
146             else
147                 if ( StringUtils.isEmpty( strNumberColumns ) )
148                 {
149                     strFieldError = FIELD_NUMBER_COLUMNS;
150                 }
151 
152         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
153         {
154             Object [ ] tabRequiredFields = {
155                 I18nService.getLocalizedString( strFieldError, locale )
156             };
157 
158             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
159         }
160         else
161             if ( !isValid( strNumberRows ) )
162             {
163                 Object [ ] tabRequiredFields = {
164                     I18nService.getLocalizedString( FIELD_NUMBER_ROWS, locale )
165                 };
166 
167                 return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
168             }
169             else
170                 if ( !isValid( strNumberColumns ) )
171                 {
172                     Object [ ] tabRequiredFields = {
173                         I18nService.getLocalizedString( FIELD_NUMBER_COLUMNS, locale )
174                     };
175 
176                     return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
177                 }
178 
179         int row = Integer.valueOf( strNumberRows );
180         int column = Integer.valueOf( strNumberColumns );
181 
182         this.setTitle( strTitle );
183         this.setHelpMessage( strHelpMessage );
184         this.setHelpMessageSearch( strHelpMessageSearch );
185         this.setComment( strComment );
186         this.setMandatory( false );
187         this.setIndexed( strIndexed != null );
188         this.setIndexedAsTitle( strIndexedAsTitle != null );
189         this.setIndexedAsSummary( strIndexedAsSummary != null );
190         this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
191         this.setShownInResultList( strShowInResultList != null );
192         this.setShownInResultRecord( strShowInResultRecord != null );
193         this.setMultipleSearchFields( strMultipleSearchFields != null );
194         this.setShownInHistory( strShowInHistory != null );
195         this.setShownInExport( strShowInExport != null );
196         this.setShownInCompleteness( strShowInCompleteness != null );
197         this.setNumberColumn( column );
198         this.setNumberRow( row );
199 
200         ArrayList<Field> listFields = new ArrayList<Field>( );
201         List<Field> fields = FieldHome.getFieldListByIdEntry( this.getIdEntry( ), PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME ) );
202 
203         for ( int i = 1; i <= ( row + 1 ); i++ )
204         {
205             for ( int j = 1; j <= ( column + 1 ); j++ )
206             {
207                 Field existingFields = null;
208 
209                 for ( Field f : fields )
210                 {
211                     if ( f.getValue( ).equals( i + "_" + j ) )
212                     {
213                         existingFields = f;
214 
215                         break;
216                     }
217                 }
218 
219                 String strTitleRow = request.getParameter( "field_" + i + "_" + j );
220 
221                 if ( ( i == 1 ) && ( j != 1 ) )
222                 {
223                     Field field = new Field( );
224 
225                     if ( existingFields != null )
226                     {
227                         field = existingFields;
228                     }
229 
230                     field.setEntry( this );
231                     field.setValue( i + "_" + j );
232                     field.setTitle( StringUtils.defaultString( strTitleRow ) );
233                     listFields.add( field );
234                 }
235                 else
236                     if ( ( i != 1 ) && ( j == 1 ) )
237                     {
238                         Field field = new Field( );
239 
240                         if ( existingFields != null )
241                         {
242                             field = existingFields;
243                         }
244 
245                         field.setEntry( this );
246                         field.setValue( i + "_" + j );
247                         field.setTitle( StringUtils.defaultString( strTitleRow ) );
248                         listFields.add( field );
249                     }
250                     else
251                     {
252                         Field field = new Field( );
253                         field.setEntry( this );
254                         field.setValue( i + "_" + j );
255                         listFields.add( field );
256                     }
257             }
258         }
259 
260         this.setFields( listFields );
261 
262         return null;
263     }
264 
265     /**
266      * {@inheritDoc}
267      */
268     @Override
269     public String getTemplateCreate( )
270     {
271         return _template_create;
272     }
273 
274     /**
275      * {@inheritDoc}
276      */
277     @Override
278     public String getTemplateModify( )
279     {
280         return _template_modify;
281     }
282 
283     /**
284      * {@inheritDoc}
285      */
286     @Override
287     public void getRecordFieldData( Record record, HttpServletRequest request, boolean bTestDirectoryError, boolean bAddNewValue,
288             List<RecordField> listRecordField, Locale locale ) throws DirectoryErrorException
289     {
290         List<Field> fields = FieldHome.getFieldListByIdEntry( this.getIdEntry( ), PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME ) );
291 
292         for ( int i = 1; i <= ( this.getNumberRow( ) + 1 ); i++ )
293         {
294             for ( int j = 1; j <= ( this.getNumberColumn( ) + 1 ); j++ )
295             {
296                 Field existingFields = null;
297 
298                 for ( Field f : fields )
299                 {
300                     if ( f.getValue( ).equals( i + "_" + j ) )
301                     {
302                         existingFields = f;
303 
304                         break;
305                     }
306                 }
307 
308                 String strValueEntry = request.getParameter( this.getIdEntry( ) + "_field_" + i + "_" + j );
309 
310                 if ( ( i != 1 ) && ( j != 1 ) )
311                 {
312                     RecordField recordField = new RecordField( );
313                     recordField.setEntry( this );
314                     recordField.setValue( strValueEntry );
315                     recordField.setField( existingFields );
316                     listRecordField.add( recordField );
317                 }
318             }
319         }
320     }
321 
322     /**
323      * {@inheritDoc}
324      */
325     @Override
326     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
327             Locale locale ) throws DirectoryErrorException
328     {
329         // Do nothing
330     }
331 
332     /**
333      * {@inheritDoc}
334      */
335     @Override
336     public String convertRecordFieldValueToString( RecordField recordField, Locale locale, boolean bDisplayFront, boolean bDisplayExport )
337     {
338         Field field = recordField.getField( );
339 
340         String [ ] fieldValues = field.getValue( ).split( "_" );
341 
342         int nRow = Integer.valueOf( fieldValues [0] );
343         int nColumn = Integer.valueOf( fieldValues [1] );
344 
345         Field fieldRow = FieldHome.findByValue( field.getEntry( ).getIdEntry( ), nRow + "_1", PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME ) );
346         Field fieldColumn = FieldHome.findByValue( field.getEntry( ).getIdEntry( ), "1_" + nColumn, PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME ) );
347 
348         return fieldRow.getTitle( ) + "/" + fieldColumn.getTitle( ) + " : " + recordField.getValue( );
349     }
350 
351     /**
352      * {@inheritDoc}
353      */
354     @Override
355     public String convertRecordFieldTitleToString( RecordField recordField, Locale locale, boolean bDisplayFront )
356     {
357         return convertRecordFieldValueToString( recordField, locale, bDisplayFront, false );
358     }
359 
360     /**
361      * {@inheritDoc}
362      */
363     @Override
364     public void addSearchCriteria( HashMap<String, Object> mapSearchItem, RecordField recordField )
365     {
366         if ( !recordField.getValue( ).equals( DirectoryUtils.EMPTY_STRING ) && ( recordField.getField( ) != null )
367                 && ( recordField.getField( ).getValue( ) != null ) && !recordField.getField( ).getValue( ).equals( DirectoryUtils.EMPTY_STRING ) )
368         {
369             List<Integer> listIdField = (List<Integer>) mapSearchItem.get( DirectorySearchItem.FIELD_ID_DIRECTORY_FIELD );
370 
371             if ( listIdField == null )
372             {
373                 listIdField = new ArrayList<Integer>( );
374             }
375 
376             listIdField.add( recordField.getField( ).getIdField( ) );
377             mapSearchItem.put( DirectorySearchItem.FIELD_ID_DIRECTORY_FIELD, listIdField );
378 
379             if ( recordField.getValue( ) != null )
380             {
381                 String strValue = new String( recordField.getValue( ) );
382 
383                 if ( !strValue.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
384                 {
385                     mapSearchItem.put( DirectorySearchItem.FIELD_CONTENTS, strValue );
386                 }
387             }
388         }
389     }
390 
391     /**
392      * Check if param is a valid integer
393      * 
394      * @param strValue
395      *            the value to check
396      * @return true if valid, false otherwise
397      */
398     private boolean isValid( String strValue )
399     {
400         if ( !StringUtils.isNumeric( strValue ) )
401         {
402             return false;
403         }
404         else
405             if ( Integer.valueOf( strValue ) <= 0 )
406             {
407                 return false;
408             }
409 
410         return true;
411     }
412 }