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.web.util.LocalizedPaginator;
43  import fr.paris.lutece.util.html.Paginator;
44  
45  import java.util.ArrayList;
46  import java.util.Collections;
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 EntryTypeSelect
56   *
57   */
58  public class EntryTypeSelect extends Entry
59  {
60      private static final String SQL_JOIN_DIRECTORY_RECORD_FIELD = " LEFT JOIN directory_record_field drf ON drf.id_record = dr.id_record AND drf.id_entry = ? LEFT JOIN directory_field df ON df.id_entry = drf.id_entry AND drf.id_field = df.id_field ";
61      private static final String SQL_ORDER_BY_TITLE = " ORDER BY df.title ";
62      private final String _template_create = "admin/plugins/directory/entrytypeselect/create_entry_type_select.html";
63      private final String _template_modify = "admin/plugins/directory/entrytypeselect/modify_entry_type_select.html";
64      private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypeselect/html_code_form_entry_type_select.html";
65      private final String _template_html_code_form_search_entry = "admin/plugins/directory/entrytypeselect/html_code_form_search_entry_type_select.html";
66      private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypeselect/html_code_entry_value_type_select.html";
67      private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypeselect/html_code_form_entry_type_select.html";
68      private final String _template_html_front_code_form_search_entry = "skin/plugins/directory/entrytypeselect/html_code_form_search_entry_type_select.html";
69      private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypeselect/html_code_entry_value_type_select.html";
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public String getTemplateHtmlFormEntry( boolean isDisplayFront )
76      {
77          if ( isDisplayFront )
78          {
79              return _template_html_front_code_form_entry;
80          }
81  
82          return _template_html_code_form_entry;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
90      {
91          if ( isDisplayFront )
92          {
93              return _template_html_front_code_entry_value;
94          }
95  
96          return _template_html_code_entry_value;
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
104     {
105         if ( isDisplayFront )
106         {
107             return _template_html_front_code_form_search_entry;
108         }
109 
110         return _template_html_code_form_search_entry;
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     public String getEntryData( HttpServletRequest request, Locale locale )
118     {
119         String strTitle = request.getParameter( PARAMETER_TITLE );
120         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
121         String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
122                 .trim( ) : null;
123         String strComment = request.getParameter( PARAMETER_COMMENT );
124         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
125         String strIndexed = request.getParameter( PARAMETER_INDEXED );
126         String strIndexedAsTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
127         String strIndexedAsSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
128         String strShowInAdvancedSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
129         String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
130         String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
131         String strWorkgroupAssociated = request.getParameter( PARAMETER_WORKGROUP_ASSOCIATED );
132         String strRoleAssociated = request.getParameter( PARAMETER_ROLE_ASSOCIATED );
133         String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
134         String strIsAllSearch = request.getParameter( PARAMETER_IS_ADD_VALUE_SEARCH_ALL );
135         String strLabelValueAllSearch = request.getParameter( PARAMETER_LABEL_VALUE_SEARCH_ALL );
136         String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
137         String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
138         String strFieldError = DirectoryUtils.EMPTY_STRING;
139 
140         if ( ( strTitle == null ) || strTitle.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
141         {
142             strFieldError = FIELD_TITLE;
143         }
144         else
145             if ( ( strIsAllSearch != null ) && ( strLabelValueAllSearch == null ) )
146             {
147                 strFieldError = FIELD_LABEL_ALL_SEARCH;
148             }
149 
150         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
151         {
152             Object [ ] tabRequiredFields = {
153                 I18nService.getLocalizedString( strFieldError, locale )
154             };
155 
156             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
157         }
158 
159         // for don't update fields listFields=null
160         this.setFields( null );
161 
162         this.setTitle( strTitle );
163         this.setHelpMessage( strHelpMessage );
164         this.setHelpMessageSearch( strHelpMessageSearch );
165         this.setComment( strComment );
166         this.setMandatory( strMandatory != null );
167         this.setIndexed( strIndexed != null );
168         this.setIndexedAsTitle( strIndexedAsTitle != null );
169         this.setIndexedAsSummary( strIndexedAsSummary != null );
170         this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
171         this.setShownInResultList( strShowInResultList != null );
172         this.setShownInResultRecord( strShowInResultRecord != null );
173         this.setWorkgroupAssociated( strWorkgroupAssociated != null );
174         this.setRoleAssociated( strRoleAssociated != null );
175         this.setShownInHistory( strShowInHistory != null );
176         this.setShownInExport( strShowInExport != null );
177         this.setShownInCompleteness( strShowInCompleteness != null );
178         this.setAddValueAllSearch( strIsAllSearch != null );
179 
180         if ( strIsAllSearch != null )
181         {
182             this.setLabelValueAllSearch( strLabelValueAllSearch );
183         }
184         else
185         {
186             this.setLabelValueAllSearch( null );
187         }
188 
189         return null;
190     }
191 
192     /**
193      * {@inheritDoc}
194      */
195     @Override
196     public String getTemplateCreate( )
197     {
198         return _template_create;
199     }
200 
201     /**
202      * {@inheritDoc}
203      */
204     @Override
205     public String getTemplateModify( )
206     {
207         return _template_modify;
208     }
209 
210     /**
211      * {@inheritDoc}
212      */
213     @Override
214     public Paginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex )
215     {
216         return new Paginator( this.getFields( ), nItemPerPage, strBaseUrl, strPageIndexParameterName, strPageIndex );
217     }
218 
219     /**
220      * {@inheritDoc}
221      */
222     @Override
223     public void getImportRecordFieldData( Record record, String strImportValue, boolean bTestDirectoryError, List<RecordField> listRecordField, Locale locale )
224             throws DirectoryErrorException
225     {
226         List<String> lstValue = new ArrayList<String>( );
227         Field field;
228         field = DirectoryUtils.findFieldByValueInTheList( strImportValue, this.getFields( ) );
229 
230         if ( field != null )
231         {
232             lstValue.add( Integer.toString( field.getIdField( ) ) );
233         }
234 
235         getRecordFieldData( record, lstValue, bTestDirectoryError, false, listRecordField, locale );
236     }
237 
238     /**
239      * {@inheritDoc}
240      */
241     @Override
242     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
243             Locale locale ) throws DirectoryErrorException
244     {
245         String strIdField = ( ( lstValue != null ) && ( lstValue.size( ) > 0 ) ) ? lstValue.get( 0 ) : null;
246         int nIdField = DirectoryUtils.convertStringToInt( strIdField );
247         Field field = null;
248         RecordField recordField = new RecordField( );
249         recordField.setEntry( this );
250 
251         if ( nIdField != DirectoryUtils.CONSTANT_ID_NULL )
252         {
253             field = DirectoryUtils.findFieldByIdInTheList( nIdField, this.getFields( ) );
254         }
255 
256         if ( bTestDirectoryError && this.isMandatory( ) )
257         {
258             if ( ( field == null ) || field.getValue( ).equals( DirectoryUtils.EMPTY_STRING ) )
259             {
260                 throw new DirectoryErrorException( this.getTitle( ) );
261             }
262         }
263 
264         if ( field != null )
265         {
266             recordField.setValue( field.getValue( ) );
267             recordField.setField( field );
268 
269             // set in the record the workgroup and the role associated to the field
270             if ( record != null )
271             {
272                 if ( this.isRoleAssociated( ) )
273                 {
274                     record.setRoleKey( field.getRoleKey( ) );
275                 }
276 
277                 if ( this.isWorkgroupAssociated( ) )
278                 {
279                     record.setWorkgroup( field.getWorkgroup( ) );
280                 }
281             }
282         }
283 
284         listRecordField.add( recordField );
285     }
286 
287     /**
288      * {@inheritDoc}
289      */
290     @Override
291     public void addSearchCriteria( HashMap<String, Object> mapSearchItem, RecordField recordField )
292     {
293         if ( ( recordField.getField( ) != null ) && ( recordField.getField( ).getValue( ) != null )
294                 && !recordField.getField( ).getValue( ).equals( DirectoryUtils.EMPTY_STRING ) )
295         {
296             List<Integer> listIdField = (List<Integer>) mapSearchItem.get( DirectorySearchItem.FIELD_ID_DIRECTORY_FIELD );
297 
298             if ( listIdField == null )
299             {
300                 listIdField = new ArrayList<Integer>( );
301             }
302 
303             listIdField.add( recordField.getField( ).getIdField( ) );
304             mapSearchItem.put( DirectorySearchItem.FIELD_ID_DIRECTORY_FIELD, listIdField );
305         }
306     }
307 
308     /**
309      * {@inheritDoc}
310      */
311     @Override
312     public boolean isSortable( )
313     {
314         return true;
315     }
316 
317     /**
318      * {@inheritDoc}
319      */
320     @Override
321     public LocalizedPaginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex, Locale locale )
322     {
323         return new LocalizedPaginator( this.getFields( ), nItemPerPage, strBaseUrl, strPageIndexParameterName, strPageIndex, locale );
324     }
325 
326     /**
327      * {@inheritDoc}
328      */
329     @Override
330     public String getSQLJoin( )
331     {
332         return SQL_JOIN_DIRECTORY_RECORD_FIELD;
333     }
334 
335     /**
336      * {@inheritDoc}
337      */
338     @Override
339     public String getSQLOrderBy( )
340     {
341         return SQL_ORDER_BY_TITLE;
342     }
343 
344     /**
345      * {@inheritDoc}
346      */
347     @Override
348     public List<Object> getSQLParametersValues( )
349     {
350         return Collections.<Object> singletonList( Integer.valueOf( getIdEntry( ) ) );
351     }
352 }