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.utils.DirectoryErrorException;
38  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
39  import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
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.plugin.PluginService;
45  import fr.paris.lutece.portal.service.regularexpression.RegularExpressionService;
46  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
47  import fr.paris.lutece.util.ReferenceList;
48  import fr.paris.lutece.util.html.Paginator;
49  
50  import org.apache.commons.lang.StringUtils;
51  
52  import java.util.ArrayList;
53  import java.util.List;
54  import java.util.Locale;
55  
56  import javax.servlet.http.HttpServletRequest;
57  
58  /**
59   *
60   * class EntryTypeText
61   *
62   */
63  public class EntryTypeText extends Entry
64  {
65      private final String _template_create = "admin/plugins/directory/entrytypetext/create_entry_type_text.html";
66      private final String _template_modify = "admin/plugins/directory/entrytypetext/modify_entry_type_text.html";
67      private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypetext/html_code_form_entry_type_text.html";
68      private final String _template_html_code_form_search_entry = "admin/plugins/directory/entrytypetext/html_code_form_search_entry_type_text.html";
69      private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypetext/html_code_entry_value_type_text.html";
70      private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypetext/html_code_form_entry_type_text.html";
71      private final String _template_html_front_code_form_search_entry = "skin/plugins/directory/entrytypetext/html_code_form_search_entry_type_text.html";
72      private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypetext/html_code_entry_value_type_text.html";
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public String getTemplateHtmlFormEntry( boolean isDisplayFront )
79      {
80          if ( isDisplayFront )
81          {
82              return _template_html_front_code_form_entry;
83          }
84  
85          return _template_html_code_form_entry;
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
93      {
94          if ( isDisplayFront )
95          {
96              return _template_html_front_code_entry_value;
97          }
98  
99          return _template_html_code_entry_value;
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
107     {
108         if ( isDisplayFront )
109         {
110             return _template_html_front_code_form_search_entry;
111         }
112 
113         return _template_html_code_form_search_entry;
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public String getEntryData( HttpServletRequest request, Locale locale )
121     {
122         String strTitle = request.getParameter( PARAMETER_TITLE );
123         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
124         String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
125                 .trim( ) : null;
126         String strComment = request.getParameter( PARAMETER_COMMENT );
127         String strValue = request.getParameter( PARAMETER_VALUE );
128         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
129         String strIndexed = request.getParameter( PARAMETER_INDEXED );
130         String strDocumentTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
131         String strDocumentSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
132         String strShowInAdvancedSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
133         String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
134         String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
135         String strWidth = request.getParameter( PARAMETER_WIDTH );
136         String strMaxSizeEnter = request.getParameter( PARAMETER_MAX_SIZE_ENTER );
137         String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
138         String strAutocomplete = request.getParameter( PARAMETER_AUTOCOMPLETE );
139         String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
140         String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
141 
142         int nWidth = DirectoryUtils.convertStringToInt( strWidth );
143         int nMaxSizeEnter = DirectoryUtils.convertStringToInt( strMaxSizeEnter );
144 
145         String strFieldError = DirectoryUtils.EMPTY_STRING;
146 
147         if ( ( strTitle == null ) || strTitle.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
148         {
149             strFieldError = FIELD_TITLE;
150         }
151 
152         else
153             if ( ( strWidth == null ) || strWidth.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
154             {
155                 strFieldError = FIELD_WIDTH;
156             }
157 
158         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
159         {
160             Object [ ] tabRequiredFields = {
161                 I18nService.getLocalizedString( strFieldError, locale )
162             };
163 
164             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
165         }
166 
167         if ( nWidth == -1 )
168         {
169             strFieldError = FIELD_WIDTH;
170         }
171         else
172             if ( ( strMaxSizeEnter != null ) && !strMaxSizeEnter.trim( ).equals( DirectoryUtils.EMPTY_STRING ) && ( nMaxSizeEnter == -1 ) )
173             {
174                 strFieldError = FIELD_MAX_SIZE_ENTER;
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_NUMERIC_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
184         }
185 
186         this.setTitle( strTitle );
187         this.setHelpMessage( strHelpMessage );
188         this.setHelpMessageSearch( strHelpMessageSearch );
189         this.setComment( strComment );
190 
191         if ( this.getFields( ) == null )
192         {
193             ArrayList<Field> listFields = new ArrayList<Field>( );
194             Field field = new Field( );
195             listFields.add( field );
196             this.setFields( listFields );
197         }
198 
199         this.getFields( ).get( 0 ).setValue( strValue );
200         this.getFields( ).get( 0 ).setWidth( nWidth );
201         this.getFields( ).get( 0 ).setMaxSizeEnter( nMaxSizeEnter );
202         this.setMandatory( strMandatory != null );
203         this.setIndexed( strIndexed != null );
204         this.setIndexedAsTitle( strDocumentTitle != null );
205         this.setIndexedAsSummary( strDocumentSummary != null );
206         this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
207         this.setShownInResultList( strShowInResultList != null );
208         this.setShownInResultRecord( strShowInResultRecord != null );
209         this.setShownInHistory( strShowInHistory != null );
210         this.setAutocompleteEntry( strAutocomplete != null );
211         this.setShownInExport( strShowInExport != null );
212         this.setShownInCompleteness( strShowInCompleteness != null );
213 
214         return null;
215     }
216 
217     /**
218      * {@inheritDoc}
219      */
220     @Override
221     public String getTemplateCreate( )
222     {
223         return _template_create;
224     }
225 
226     /**
227      * {@inheritDoc}
228      */
229     @Override
230     public String getTemplateModify( )
231     {
232         return _template_modify;
233     }
234 
235     /**
236      * {@inheritDoc}
237      */
238     @Override
239     public Paginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex )
240     {
241         return new Paginator( this.getFields( ).get( 0 ).getRegularExpressionList( ), nItemPerPage, strBaseUrl, strPageIndexParameterName, strPageIndex );
242     }
243 
244     /**
245      * {@inheritDoc}
246      */
247     @Override
248     public ReferenceList getReferenceListRegularExpression( IEntry entry, Plugin plugin )
249     {
250         ReferenceList refListRegularExpression = null;
251 
252         if ( RegularExpressionService.getInstance( ).isAvailable( ) )
253         {
254             refListRegularExpression = new ReferenceList( );
255 
256             List<RegularExpression> listRegularExpression = RegularExpressionService.getInstance( ).getAllRegularExpression( );
257 
258             for ( RegularExpression regularExpression : listRegularExpression )
259             {
260                 if ( !entry.getFields( ).get( 0 ).getRegularExpressionList( ).contains( regularExpression ) )
261                 {
262                     refListRegularExpression.addItem( regularExpression.getIdExpression( ), regularExpression.getTitle( ) );
263                 }
264             }
265         }
266 
267         return refListRegularExpression;
268     }
269 
270     /**
271      * {@inheritDoc}
272      */
273     @Override
274     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
275             Locale locale ) throws DirectoryErrorException
276     {
277         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
278 
279         String strValueEntry = ( ( lstValue != null ) && ( lstValue.size( ) > 0 ) ) ? lstValue.get( 0 ) : null;
280         List<RegularExpression> listRegularExpression = this.getFields( ).get( 0 ).getRegularExpressionList( );
281         RecordField response = new RecordField( );
282         response.setEntry( this );
283 
284         if ( ( record != null ) && bAddNewValue )
285         {
286             RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
287             recordFieldFilter.setIdDirectory( record.getDirectory( ).getIdDirectory( ) );
288             recordFieldFilter.setIdEntry( this.getIdEntry( ) );
289             recordFieldFilter.setIdRecord( record.getIdRecord( ) );
290 
291             List<RecordField> recordFieldList = RecordFieldHome.getRecordFieldList( recordFieldFilter, plugin );
292 
293             if ( ( recordFieldList != null ) && !recordFieldList.isEmpty( ) && StringUtils.isNotBlank( recordFieldList.get( 0 ).getValue( ) ) )
294             {
295                 strValueEntry = recordFieldList.get( 0 ).getValue( ) + ", " + strValueEntry;
296             }
297         }
298 
299         if ( strValueEntry != null )
300         {
301             if ( bTestDirectoryError && this.isMandatory( ) && strValueEntry.equals( DirectoryUtils.EMPTY_STRING ) )
302             {
303                 throw new DirectoryErrorException( this.getTitle( ) );
304             }
305 
306             if ( bTestDirectoryError && ( !strValueEntry.equals( DirectoryUtils.EMPTY_STRING ) ) && ( listRegularExpression != null )
307                     && ( listRegularExpression.size( ) != 0 ) && RegularExpressionService.getInstance( ).isAvailable( ) )
308             {
309                 for ( RegularExpression regularExpression : listRegularExpression )
310                 {
311                     if ( !RegularExpressionService.getInstance( ).isMatches( strValueEntry, regularExpression ) )
312                     {
313                         throw new DirectoryErrorException( this.getTitle( ), regularExpression.getErrorMessage( ) );
314                     }
315                 }
316             }
317 
318             response.setValue( strValueEntry );
319         }
320 
321         listRecordField.add( response );
322     }
323 
324     /**
325      * {@inheritDoc}
326      */
327     @Override
328     public boolean isSortable( )
329     {
330         return true;
331     }
332 
333     /**
334      * {@inheritDoc}
335      */
336     @Override
337     public LocalizedPaginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex, Locale locale )
338     {
339         return new LocalizedPaginator( this.getFields( ).get( 0 ).getRegularExpressionList( ), nItemPerPage, strBaseUrl, strPageIndexParameterName,
340                 strPageIndex, locale );
341     }
342 
343     /**
344      * Check if entries of this type are anonymizable or not.
345      * 
346      * @return True if the entry type is anonymizable, false otherwise
347      */
348     @Override
349     public boolean isAnonymizable( )
350     {
351         return true;
352     }
353 }