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.DirectoryService;
38  import fr.paris.lutece.plugins.directory.service.record.IRecordService;
39  import fr.paris.lutece.plugins.directory.service.record.RecordService;
40  import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
41  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
42  import fr.paris.lutece.portal.service.i18n.I18nService;
43  import fr.paris.lutece.portal.service.message.AdminMessage;
44  import fr.paris.lutece.portal.service.message.AdminMessageService;
45  import fr.paris.lutece.portal.service.plugin.Plugin;
46  import fr.paris.lutece.portal.service.plugin.PluginService;
47  import fr.paris.lutece.portal.service.spring.SpringContextService;
48  import fr.paris.lutece.portal.service.template.AppTemplateService;
49  import fr.paris.lutece.util.html.HtmlTemplate;
50  
51  import org.apache.commons.lang.StringUtils;
52  
53  import java.util.ArrayList;
54  import java.util.HashMap;
55  import java.util.List;
56  import java.util.Locale;
57  import java.util.Map;
58  
59  import javax.servlet.http.HttpServletRequest;
60  
61  /**
62   *
63   * class EntryTypeNumbering
64   *
65   */
66  public class EntryTypeNumbering extends Entry
67  {
68      private static final String PARAMETER_PREFIX = "prefix";
69  
70      // MARKS
71      private static final String MARK_MAX_NUMBER = "max_number";
72  
73      // SQL
74      private static final String SQL_ORDER_BY_RECORD_FIELD_VALUE = " ORDER BY CAST(drf.record_field_value AS DECIMAL) ";
75      private final String _template_create = "admin/plugins/directory/entrytypenumbering/create_entry_type_numbering.html";
76      private final String _template_modify = "admin/plugins/directory/entrytypenumbering/modify_entry_type_numbering.html";
77      private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypenumbering/html_code_form_entry_type_numbering.html";
78      private final String _template_html_code_form_search_entry = "admin/plugins/directory/entrytypenumbering/html_code_form_search_entry_type_numbering.html";
79      private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypenumbering/html_code_entry_value_type_numbering.html";
80      private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypenumbering/html_code_form_entry_type_numbering.html";
81      private final String _template_html_front_code_form_search_entry = "skin/plugins/directory/entrytypenumbering/html_code_form_search_entry_type_numbering.html";
82      private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypenumbering/html_code_entry_value_type_numbering.html";
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public String getTemplateHtmlFormEntry( boolean isDisplayFront )
89      {
90          if ( isDisplayFront )
91          {
92              return _template_html_front_code_form_entry;
93          }
94          else
95          {
96              return _template_html_code_form_entry;
97          }
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
105     {
106         if ( isDisplayFront )
107         {
108             return _template_html_front_code_entry_value;
109         }
110 
111         return _template_html_code_entry_value;
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
119     {
120         if ( isDisplayFront )
121         {
122             return _template_html_front_code_form_search_entry;
123         }
124 
125         return _template_html_code_form_search_entry;
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public String getHtmlFormEntry( Locale locale, boolean isDisplayFront )
133     {
134         if ( getTemplateHtmlFormEntry( isDisplayFront ) != null )
135         {
136             Map<String, Object> model = new HashMap<String, Object>( );
137             model.put( MARK_ENTRY, this );
138             model.put( MARK_LOCALE, locale );
139             model.put( MARK_MAX_NUMBER, DirectoryService.getInstance( ).getMaxNumber( this ) );
140 
141             HtmlTemplate template = AppTemplateService.getTemplate( getTemplateHtmlFormEntry( isDisplayFront ), locale, model );
142 
143             return template.getHtml( );
144         }
145 
146         return null;
147     }
148 
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     public String getEntryData( HttpServletRequest request, Locale locale )
154     {
155         String strTitle = request.getParameter( PARAMETER_TITLE );
156         String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
157                 .trim( ) : null;
158         String strComment = request.getParameter( PARAMETER_COMMENT );
159 
160         String strIndexed = request.getParameter( PARAMETER_INDEXED );
161         String strIndexedAsTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
162         String strIndexedAsSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
163         String strShowInAdvancedSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
164         String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
165         String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
166         String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
167         String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
168         String strPrefix = request.getParameter( PARAMETER_PREFIX );
169 
170         String strFieldError = DirectoryUtils.EMPTY_STRING;
171 
172         if ( ( strTitle == null ) || strTitle.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
173         {
174             strFieldError = FIELD_TITLE;
175         }
176 
177         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
178         {
179             Object [ ] tabRequiredFields = {
180                 I18nService.getLocalizedString( strFieldError, locale )
181             };
182 
183             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
184         }
185 
186         this.setTitle( strTitle );
187         this.setHelpMessageSearch( strHelpMessageSearch );
188         this.setComment( strComment );
189 
190         if ( this.getFields( ) == null )
191         {
192             ArrayList<Field> listFields = new ArrayList<Field>( );
193             Field field = new Field( );
194             listFields.add( field );
195             this.setFields( listFields );
196         }
197 
198         this.getFields( ).get( 0 ).setValue( "1" );
199         this.getFields( ).get( 0 ).setTitle( StringUtils.isNotEmpty( strPrefix ) ? strPrefix : StringUtils.EMPTY );
200         this.setIndexed( strIndexed != null );
201         this.setIndexedAsTitle( strIndexedAsTitle != null );
202         this.setIndexedAsSummary( strIndexedAsSummary != null );
203         this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
204         this.setShownInResultList( strShowInResultList != null );
205         this.setShownInResultRecord( strShowInResultRecord != null );
206         this.setShownInHistory( strShowInHistory != null );
207         this.setShownInExport( strShowInExport != null );
208         // This entry cannot be shown in completeness
209         this.setShownInCompleteness( false );
210 
211         return null;
212     }
213 
214     /**
215      * {@inheritDoc}
216      */
217     @Override
218     public String getTemplateCreate( )
219     {
220         return _template_create;
221     }
222 
223     /**
224      * {@inheritDoc}
225      */
226     @Override
227     public String getTemplateModify( )
228     {
229         return _template_modify;
230     }
231 
232     /**
233      * {@inheritDoc}
234      */
235     @Override
236     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
237             Locale locale ) throws DirectoryErrorException
238     {
239         /*
240          * This method is called from several operations : 1) Creating a record 2) Updating a record 3) Search from BO 4) Search from FO
241          */
242         Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
243         RecordField recordField = new RecordField( );
244         recordField.setEntry( this );
245 
246         Record recordOld = null;
247 
248         if ( record != null )
249         {
250             /*
251              * CASES 1 AND 2 : (The record is not null for cases 1) and 2))
252              */
253             IRecordService recordService = SpringContextService.getBean( RecordService.BEAN_SERVICE );
254             recordOld = recordService.findByPrimaryKey( record.getIdRecord( ), pluginDirectory );
255         }
256 
257         String strValueEntry = ( ( lstValue != null ) && ( lstValue.size( ) > 0 ) ) ? lstValue.get( 0 ) : null;
258 
259         if ( recordOld != null )
260         {
261             /*
262              * CASE 2 : (The record old is not null for case 2))
263              */
264             recordField.setValue( strValueEntry );
265         }
266         else
267             if ( record == null )
268             {
269                 /*
270                  * CASES 3 AND 4 : (The record is null for cases 3) and 4))
271                  */
272                 if ( bTestDirectoryError && this.isMandatory( ) && StringUtils.isBlank( strValueEntry ) )
273                 {
274                     throw new DirectoryErrorException( this.getTitle( ) );
275                 }
276 
277                 if ( StringUtils.isNotBlank( strValueEntry ) )
278                 {
279                     recordField.setValue( strValueEntry );
280                 }
281             }
282             else
283             {
284                 /*
285                  * CASE 1 : (Create the record, thus fetch the max number)
286                  */
287                 int numbering = DirectoryService.getInstance( ).getMaxNumber( this );
288                 this.getFields( ).get( 0 ).setValue( String.valueOf( numbering + 1 ) );
289                 FieldHome.update( this.getFields( ).get( 0 ), pluginDirectory );
290 
291                 recordField.setValue( String.valueOf( numbering ) );
292             }
293 
294         if ( recordField.getValue( ) != null )
295         {
296             /*
297              * In cases 3 and 4, if the strValueEntry is null, then that means that the user did not use the search function and the user only wants to display
298              * every records or did not want to filter his/her search for this entry.
299              */
300             listRecordField.add( recordField );
301         }
302     }
303 
304     /**
305      * {@inheritDoc}
306      */
307     @Override
308     public void getImportRecordFieldData( Record record, String strImportValue, boolean bTestDirectoryError, List<RecordField> listRecordField, Locale locale )
309             throws DirectoryErrorException
310     {
311         int numbering = DirectoryService.getInstance( ).getNumber( this, strImportValue );
312 
313         if ( numbering != DirectoryUtils.CONSTANT_ID_NULL )
314         {
315             RecordField recordField = new RecordField( );
316             recordField.setEntry( this );
317             recordField.setValue( Integer.toString( numbering ) );
318             listRecordField.add( recordField );
319         }
320     }
321 
322     /**
323      * {@inheritDoc}
324      */
325     @Override
326     public String convertRecordFieldValueToString( RecordField recordField, Locale locale, boolean bDisplayFront, boolean bExportDirectory )
327     {
328         Field field = null;
329 
330         if ( recordField.getField( ) == null )
331         {
332             if ( getFields( ) == null )
333             {
334                 Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
335                 setFields( FieldHome.getFieldListByIdEntry( getIdEntry( ), plugin ) );
336             }
337 
338             if ( ( getFields( ) != null ) && ( getFields( ).size( ) > 0 ) )
339             {
340                 field = getFields( ).get( 0 );
341             }
342         }
343         else
344         {
345             field = recordField.getField( );
346         }
347 
348         if ( ( field != null ) && StringUtils.isNotBlank( field.getTitle( ) ) )
349         {
350             return field.getTitle( ) + recordField.getValue( );
351         }
352 
353         return super.convertRecordFieldValueToString( recordField, locale, bDisplayFront, bExportDirectory );
354     }
355 
356     /**
357      * {@inheritDoc}
358      */
359     @Override
360     public boolean isSortable( )
361     {
362         return true;
363     }
364 
365     /**
366      * {@inheritDoc}
367      */
368     @Override
369     public String getSQLOrderBy( )
370     {
371         // Special query in order to sort numerically and not alphabetically (thus avoiding list like 1, 10, 11, 2, ... instead of 1, 2, ..., 10, 11)
372         return SQL_ORDER_BY_RECORD_FIELD_VALUE;
373     }
374 }