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.service.content.XPageAppService;
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.util.AppPathService;
46  import fr.paris.lutece.portal.service.util.AppPropertiesService;
47  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
48  import fr.paris.lutece.util.ReferenceList;
49  import fr.paris.lutece.util.html.Paginator;
50  import fr.paris.lutece.util.sort.AttributeComparator;
51  import fr.paris.lutece.util.url.UrlItem;
52  
53  import java.util.Collections;
54  import java.util.List;
55  import java.util.Locale;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  /**
60   *
61   * class EntryTypeText
62   *
63   */
64  public class EntryTypeDirectory extends Entry
65  {
66      // Parameter
67      private static final String PARAMETER_VIEW_DIRECTORY_RECORD = "view_directory_record";
68  
69      // Property
70      private static final String PROPERTY_PAGE_APPLICATION_ID = "directory.xpage.applicationId";
71  
72      // JSP
73      private static final String JSP_DO_VISUALISATION_RECORD = "jsp/admin/plugins/directory/DoVisualisationRecord.jsp";
74  
75      // HTML constants
76      private static final String HTML_LINK_OPEN_BEGIN = "<a href=\"";
77      private static final String HTML_LINK_OPEN_END = "\">";
78      private static final String HTML_LINK_CLOSE = "</a>";
79  
80      // Templates
81      private final String _template_create = "admin/plugins/directory/entrytypedirectory/create_entry_type_directory.html";
82      private final String _template_modify = "admin/plugins/directory/entrytypedirectory/modify_entry_type_directory.html";
83      private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypedirectory/html_code_form_entry_type_directory.html";
84      private final String _template_html_code_form_search_entry = "admin/plugins/directory/entrytypedirectory/html_code_form_search_entry_type_directory.html";
85      private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypedirectory/html_code_entry_value_type_directory.html";
86      private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypedirectory/html_code_form_entry_type_directory.html";
87      private final String _template_html_front_code_form_search_entry = "skin/plugins/directory/entrytypedirectory/html_code_form_search_entry_type_directory.html";
88      private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypedirectory/html_code_entry_value_type_directory.html";
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public String getTemplateHtmlFormEntry( boolean isDisplayFront )
95      {
96          if ( isDisplayFront )
97          {
98              return _template_html_front_code_form_entry;
99          }
100 
101         return _template_html_code_form_entry;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
109     {
110         if ( isDisplayFront )
111         {
112             return _template_html_front_code_entry_value;
113         }
114 
115         return _template_html_code_entry_value;
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
123     {
124         if ( isDisplayFront )
125         {
126             return _template_html_front_code_form_search_entry;
127         }
128 
129         return _template_html_code_form_search_entry;
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136     public String getEntryData( HttpServletRequest request, Locale locale )
137     {
138         String strTitle = request.getParameter( PARAMETER_TITLE );
139         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
140         String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
141                 .trim( ) : null;
142         String strComment = request.getParameter( PARAMETER_COMMENT );
143         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
144         String strIndexed = request.getParameter( PARAMETER_INDEXED );
145         String strIndexedAsTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
146         String strIndexedAsSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
147         String strShowInAdvancedSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
148         String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
149         String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
150         String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
151         String strIsAllSearch = request.getParameter( PARAMETER_IS_ADD_VALUE_SEARCH_ALL );
152         String strLabelValueAllSearch = request.getParameter( PARAMETER_LABEL_VALUE_SEARCH_ALL );
153         String strIdEntryAssociate = request.getParameter( PARAMETER_ENTRY_ASSOCIATE );
154         String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
155         String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
156 
157         int idEntryAssociate = DirectoryUtils.convertStringToInt( strIdEntryAssociate );
158 
159         String strFieldError = DirectoryUtils.EMPTY_STRING;
160 
161         if ( ( strTitle == null ) || strTitle.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
162         {
163             strFieldError = FIELD_TITLE;
164         }
165         else
166             if ( idEntryAssociate == -1 )
167             {
168                 strFieldError = FIELD_ENTRY_ASSOCIATE;
169             }
170             else
171                 if ( ( strIsAllSearch != null ) && ( strLabelValueAllSearch == null ) )
172                 {
173                     strFieldError = FIELD_LABEL_ALL_SEARCH;
174                 }
175 
176         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
177         {
178             Object [ ] tabRequiredFields = {
179                 I18nService.getLocalizedString( strFieldError, locale )
180             };
181 
182             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
183         }
184 
185         this.setTitle( strTitle );
186         this.setHelpMessage( strHelpMessage );
187         this.setHelpMessageSearch( strHelpMessageSearch );
188         this.setComment( strComment );
189 
190         // for don't update fields listFields=null
191         this.setFields( null );
192 
193         this.setMandatory( strMandatory != null );
194         this.setIndexed( strIndexed != null );
195         this.setIndexedAsTitle( strIndexedAsTitle != null );
196         this.setIndexedAsSummary( strIndexedAsSummary != null );
197         this.setShownInAdvancedSearch( strShowInAdvancedSearch != null );
198         this.setShownInResultList( strShowInResultList != null );
199         this.setShownInResultRecord( strShowInResultRecord != null );
200         this.setShownInHistory( strShowInHistory != null );
201         this.setEntryAssociate( idEntryAssociate );
202         this.setAddValueAllSearch( strIsAllSearch != null );
203         this.setShownInExport( strShowInExport != null );
204         this.setShownInCompleteness( strShowInCompleteness != null );
205 
206         if ( strIsAllSearch != null )
207         {
208             this.setLabelValueAllSearch( strLabelValueAllSearch );
209         }
210         else
211         {
212             this.setLabelValueAllSearch( null );
213         }
214 
215         return null;
216     }
217 
218     /**
219      * {@inheritDoc}
220      */
221     @Override
222     public String getTemplateCreate( )
223     {
224         return _template_create;
225     }
226 
227     /**
228      * {@inheritDoc}
229      */
230     @Override
231     public String getTemplateModify( )
232     {
233         return _template_modify;
234     }
235 
236     /**
237      * {@inheritDoc}
238      */
239     @Override
240     public Paginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex )
241     {
242         return new Paginator( this.getFields( ), nItemPerPage, strBaseUrl, strPageIndexParameterName, strPageIndex );
243     }
244 
245     /**
246      * {@inheritDoc}
247      */
248     @Override
249     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
250             Locale locale ) throws DirectoryErrorException
251     {
252         String strValueEntry = ( ( lstValue != null ) && ( lstValue.size( ) > 0 ) ) ? lstValue.get( 0 ) : null;
253         RecordField response = new RecordField( );
254         response.setEntry( this );
255 
256         if ( strValueEntry != null )
257         {
258             if ( bTestDirectoryError && this.isMandatory( )
259                     && ( strValueEntry.equals( DirectoryUtils.EMPTY_STRING ) || strValueEntry.equals( String.valueOf( DirectoryUtils.CONSTANT_ID_NULL ) ) ) )
260             {
261                 throw new DirectoryErrorException( this.getTitle( ) );
262             }
263 
264             response.setValue( strValueEntry );
265         }
266 
267         listRecordField.add( response );
268     }
269 
270     public ReferenceList getSelectListRecordAssociate( boolean bDisplayFormSearch )
271     {
272         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
273         ReferenceList referenceList = new ReferenceList( );
274 
275         if ( ( bDisplayFormSearch || !this.isMandatory( ) ) && this.isAddValueAllSearch( ) )
276         {
277             referenceList.addItem( DirectoryUtils.EMPTY_STRING, this.getLabelValueAllSearch( ) );
278         }
279 
280         RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
281         // recordFieldFilter.setIdDirectory(this.getDirectoryAssociate());
282         recordFieldFilter.setIdEntry( this.getEntryAssociate( ) );
283 
284         List<RecordField> listRecordField = RecordFieldHome.getRecordFieldList( recordFieldFilter, plugin );
285         Collections.sort( listRecordField, new AttributeComparator( "value", true ) );
286 
287         for ( RecordField recordField : listRecordField )
288         {
289             String title;
290 
291             if ( recordField.getFile( ) != null )
292             {
293                 title = recordField.getFile( ).getTitle( );
294             }
295             else
296                 if ( recordField.getField( ) != null )
297                 {
298                     title = recordField.getField( ).getTitle( );
299                 }
300                 else
301                 {
302                     title = recordField.getValue( );
303                 }
304 
305             if ( title != null )
306             {
307                 referenceList.addItem( recordField.getRecord( ).getIdRecord( ), title );
308             }
309         }
310 
311         return referenceList;
312     }
313 
314     /**
315      * {@inheritDoc}
316      */
317     @Override
318     public String convertRecordFieldValueToString( RecordField recordField, Locale locale, boolean bDisplayFront, boolean bExportDirectory )
319     {
320         String value = DirectoryUtils.EMPTY_STRING;
321         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
322         int idDirectory = -1;
323 
324         if ( recordField.getValue( ) != null )
325         {
326             if ( !bExportDirectory )
327             {
328                 RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
329                 IEntry entry = EntryHome.findByPrimaryKey( this.getIdEntry( ), plugin );
330                 idDirectory = entry.getDirectory( ).getIdDirectory( );
331 
332                 recordFieldFilter.setIdEntry( entry.getEntryAssociate( ) );
333                 recordFieldFilter.setIdRecord( DirectoryUtils.convertStringToInt( recordField.getValue( ) ) );
334 
335                 List<RecordField> listRecordField = RecordFieldHome.getRecordFieldList( recordFieldFilter, plugin );
336 
337                 if ( ( listRecordField != null ) && !listRecordField.isEmpty( ) )
338                 {
339                     RecordField recordFieldResult = listRecordField.get( 0 );
340 
341                     if ( recordFieldResult != null )
342                     {
343                         if ( recordFieldResult.getFile( ) != null )
344                         {
345                             value = recordFieldResult.getFile( ).getTitle( );
346                         }
347                         else
348                             if ( recordFieldResult.getField( ) != null )
349                             {
350                                 value = recordFieldResult.getField( ).getTitle( );
351                             }
352                             else
353                             {
354                                 value = recordFieldResult.getValue( );
355                             }
356                     }
357 
358                     if ( bDisplayFront && !value.equals( DirectoryUtils.EMPTY_STRING ) )
359                     {
360                         UrlItem url = new UrlItem( AppPathService.getPortalUrl( ) );
361                         url.addParameter( XPageAppService.PARAM_XPAGE_APP, AppPropertiesService.getProperty( PROPERTY_PAGE_APPLICATION_ID ) );
362                         url.addParameter( PARAMETER_ID_DIRECTORY_RECORD, recordField.getValue( ) );
363                         url.addParameter( PARAMETER_VIEW_DIRECTORY_RECORD, idDirectory );
364 
365                         return HTML_LINK_OPEN_BEGIN + url.getUrl( ) + HTML_LINK_OPEN_END + value + HTML_LINK_CLOSE;
366                     }
367                     else
368                         if ( !bExportDirectory && !value.equals( DirectoryUtils.EMPTY_STRING ) )
369                         {
370                             UrlItem url = new UrlItem( JSP_DO_VISUALISATION_RECORD );
371                             url.addParameter( PARAMETER_ID_DIRECTORY_RECORD, recordField.getValue( ) );
372 
373                             return HTML_LINK_OPEN_BEGIN + url.getUrl( ) + HTML_LINK_OPEN_END + value + HTML_LINK_CLOSE;
374                         }
375                 }
376                 else
377                 {
378                     return recordField.getValue( );
379                 }
380             }
381             else
382             {
383                 return recordField.getValue( );
384             }
385         }
386         else
387         {
388             return DirectoryUtils.EMPTY_STRING;
389         }
390 
391         return DirectoryUtils.EMPTY_STRING;
392     }
393 
394     /**
395      * {@inheritDoc}
396      */
397     @Override
398     public String convertRecordFieldTitleToString( RecordField recordField, Locale locale, boolean bDisplayFront )
399     {
400         return convertRecordFieldValueToString( recordField, locale, bDisplayFront, false );
401     }
402 
403     /**
404      * {@inheritDoc}
405      */
406     @Override
407     public LocalizedPaginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex, Locale locale )
408     {
409         return new LocalizedPaginator( this.getFields( ), nItemPerPage, strBaseUrl, strPageIndexParameterName, strPageIndex, locale );
410     }
411 }