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