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.directorysearch.DirectorySearchItem;
37  import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
38  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
44  import fr.paris.lutece.util.html.Paginator;
45  
46  import java.util.ArrayList;
47  import java.util.HashMap;
48  import java.util.List;
49  import java.util.Locale;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  /**
54   *
55   * class EntryTypeCheckBox
56   *
57   */
58  public class EntryTypeCheckBox extends Entry
59  {
60      private final String _template_create = "admin/plugins/directory/entrytypecheckbox/create_entry_type_check_box.html";
61      private final String _template_modify = "admin/plugins/directory/entrytypecheckbox/modify_entry_type_check_box.html";
62      private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypecheckbox/html_code_form_entry_type_check_box.html";
63      private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypecheckbox/html_code_entry_value_type_check_box.html";
64      private final String _template_html_code_form_search_entry = "admin/plugins/directory/entrytypecheckbox/html_code_form_search_entry_type_check_box.html";
65      private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypecheckbox/html_code_form_entry_type_check_box.html";
66      private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypecheckbox/html_code_entry_value_type_check_box.html";
67      private final String _template_html_front_code_form_search_entry = "skin/plugins/directory/entrytypecheckbox/html_code_form_search_entry_type_check_box.html";
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public String getTemplateHtmlFormEntry( boolean isDisplayFront )
74      {
75          if ( isDisplayFront )
76          {
77              return _template_html_front_code_form_entry;
78          }
79  
80          return _template_html_code_form_entry;
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
88      {
89          if ( isDisplayFront )
90          {
91              return _template_html_front_code_form_search_entry;
92          }
93  
94          return _template_html_code_form_search_entry;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
102     {
103         if ( isDisplayFront )
104         {
105             return _template_html_front_code_entry_value;
106         }
107 
108         return _template_html_code_entry_value;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public String getEntryData( HttpServletRequest request, Locale locale )
116     {
117         String strTitle = request.getParameter( PARAMETER_TITLE );
118         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
119         String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
120                 .trim( ) : null;
121         String strComment = request.getParameter( PARAMETER_COMMENT );
122         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
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 strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
130         String strFieldInLine = request.getParameter( PARAMETER_FIELD_IN_LINE );
131         String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
132         String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
133         int nFieldInLine = DirectoryUtils.convertStringToInt( strFieldInLine );
134 
135         String strFieldError = DirectoryUtils.EMPTY_STRING;
136 
137         if ( ( strTitle == null ) || strTitle.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
138         {
139             strFieldError = FIELD_TITLE;
140         }
141 
142         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
143         {
144             Object [ ] tabRequiredFields = {
145                 I18nService.getLocalizedString( strFieldError, locale )
146             };
147 
148             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
149         }
150 
151         // for don't update fields listFields=null
152         this.setFields( null );
153 
154         this.setTitle( strTitle );
155         this.setHelpMessage( strHelpMessage );
156         this.setHelpMessageSearch( strHelpMessageSearch );
157         this.setComment( strComment );
158         this.setMandatory( strMandatory != null );
159         this.setIndexed( strIndexed != null );
160         this.setIndexedAsTitle( strIndexedAsTitle != null );
161         this.setIndexedAsSummary( strIndexedAsSummary != null );
162         this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
163         this.setShownInResultList( strShowInResultList != null );
164         this.setShownInResultRecord( strShowInResultRecord != null );
165         this.setFieldInLine( nFieldInLine == 1 );
166         this.setShownInHistory( strShowInHistory != null );
167         this.setShownInExport( strShowInExport != null );
168         this.setShownInCompleteness( strShowInCompleteness != null );
169 
170         return null;
171     }
172 
173     /**
174      * {@inheritDoc}
175      */
176     @Override
177     public String getTemplateCreate( )
178     {
179         return _template_create;
180     }
181 
182     /**
183      * {@inheritDoc}
184      */
185     @Override
186     public String getTemplateModify( )
187     {
188         return _template_modify;
189     }
190 
191     /**
192      * {@inheritDoc}
193      */
194     @Override
195     public Paginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex )
196     {
197         return new Paginator( this.getFields( ), nItemPerPage, strBaseUrl, strPageIndexParameterName, strPageIndex );
198     }
199 
200     @Override
201     public void getRecordFieldData( Record record, HttpServletRequest request, boolean bTestDirectoryError, boolean bAddNewValue,
202             List<RecordField> listRecordField, Locale locale ) throws DirectoryErrorException
203     {
204         List<String> lstValue = new ArrayList<String>( );
205         String [ ] strTabIdField = request.getParameterValues( DirectoryUtils.EMPTY_STRING + this.getIdEntry( ) );
206 
207         if ( strTabIdField != null )
208         {
209             for ( String strIdField : strTabIdField )
210             {
211                 lstValue.add( strIdField );
212             }
213         }
214 
215         getRecordFieldData( record, lstValue, bTestDirectoryError, bAddNewValue, listRecordField, locale );
216     }
217 
218     /**
219      * {@inheritDoc}
220      */
221     @Override
222     public void getImportRecordFieldData( Record record, String strImportValue, boolean bTestDirectoryError, List<RecordField> listRecordField, Locale locale )
223             throws DirectoryErrorException
224     {
225         String strMultipleValueSeparator = AppPropertiesService.getProperty( PROPERTY_IMPORT_MULTIPLE_VALUE_DELIMITER );
226         List<String> lstValue = new ArrayList<String>( );
227         String [ ] tabStrValue = strImportValue.split( strMultipleValueSeparator );
228         Field field;
229 
230         for ( String strValue : tabStrValue )
231         {
232             field = DirectoryUtils.findFieldByValueInTheList( strValue, this.getFields( ) );
233 
234             if ( field != null )
235             {
236                 lstValue.add( Integer.toString( field.getIdField( ) ) );
237             }
238         }
239 
240         getRecordFieldData( record, lstValue, bTestDirectoryError, false, listRecordField, locale );
241     }
242 
243     /**
244      * {@inheritDoc}
245      */
246     @Override
247     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
248             Locale locale ) throws DirectoryErrorException
249     {
250         List<Field> listFieldInResponse = new ArrayList<Field>( );
251         int nIdField = -1;
252         Field field = null;
253         RecordField recordField;
254 
255         if ( lstValue != null )
256         {
257             for ( String strIdField : lstValue )
258             {
259                 nIdField = DirectoryUtils.convertStringToInt( strIdField );
260                 field = DirectoryUtils.findFieldByIdInTheList( nIdField, this.getFields( ) );
261 
262                 if ( field != null )
263                 {
264                     listFieldInResponse.add( field );
265                 }
266             }
267         }
268 
269         if ( bTestDirectoryError && this.isMandatory( ) )
270         {
271             boolean bAllFieldEmpty = true;
272 
273             for ( Field fieldInResponse : listFieldInResponse )
274             {
275                 if ( !fieldInResponse.getValue( ).equals( DirectoryUtils.EMPTY_STRING ) )
276                 {
277                     bAllFieldEmpty = false;
278                 }
279             }
280 
281             if ( bAllFieldEmpty )
282             {
283                 throw new DirectoryErrorException( this.getTitle( ) );
284             }
285         }
286 
287         if ( listFieldInResponse.size( ) == 0 )
288         {
289             recordField = new RecordField( );
290             recordField.setEntry( this );
291             listRecordField.add( recordField );
292         }
293         else
294         {
295             for ( Field fieldInResponse : listFieldInResponse )
296             {
297                 recordField = new RecordField( );
298                 recordField.setEntry( this );
299                 recordField.setValue( fieldInResponse.getValue( ) );
300                 recordField.setField( fieldInResponse );
301                 listRecordField.add( recordField );
302             }
303         }
304     }
305 
306     /**
307      * {@inheritDoc}
308      */
309     @Override
310     public void addSearchCriteria( HashMap<String, Object> mapSearchItem, RecordField recordField )
311     {
312         if ( ( recordField.getField( ) != null ) && ( recordField.getField( ).getValue( ) != null )
313                 && !recordField.getField( ).getValue( ).equals( DirectoryUtils.EMPTY_STRING ) )
314         {
315             List<Integer> listIdField = (List<Integer>) mapSearchItem.get( DirectorySearchItem.FIELD_ID_DIRECTORY_FIELD );
316 
317             if ( listIdField == null )
318             {
319                 listIdField = new ArrayList<Integer>( );
320             }
321 
322             listIdField.add( recordField.getField( ).getIdField( ) );
323             mapSearchItem.put( DirectorySearchItem.FIELD_ID_DIRECTORY_FIELD, listIdField );
324         }
325     }
326 
327     /**
328      * {@inheritDoc}
329      */
330     @Override
331     public boolean isSortable( )
332     {
333         return false;
334     }
335 
336     /**
337      * {@inheritDoc}
338      */
339     @Override
340     public LocalizedPaginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex, Locale locale )
341     {
342         return new LocalizedPaginator( this.getFields( ), nItemPerPage, strBaseUrl, strPageIndexParameterName, strPageIndex, locale );
343     }
344 }