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.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.service.plugin.Plugin;
43  import fr.paris.lutece.portal.service.plugin.PluginService;
44  
45  import org.apache.commons.lang.StringUtils;
46  
47  import java.util.ArrayList;
48  import java.util.List;
49  import java.util.Locale;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  /**
54   *
55   * class EntryTypeTextArea
56   *
57   */
58  public class EntryTypeTextArea extends Entry
59  {
60      private final String _template_create = "admin/plugins/directory/entrytypetextarea/create_entry_type_text_area.html";
61      private final String _template_modify = "admin/plugins/directory/entrytypetextarea/modify_entry_type_text_area.html";
62      private final String _template_html_code_form_entry = "admin/plugins/directory/entrytypetextarea/html_code_form_entry_type_text_area.html";
63      private final String _template_html_code_form_search_entry = "admin/plugins/directory/entrytypetextarea/html_code_form_search_entry_type_text_area.html";
64      private final String _template_html_code_entry_value = "admin/plugins/directory/entrytypetextarea/html_code_entry_value_type_text_area.html";
65      private final String _template_html_front_code_form_entry = "skin/plugins/directory/entrytypetextarea/html_code_form_entry_type_text_area.html";
66      private final String _template_html_front_code_form_search_entry = "skin/plugins/directory/entrytypetextarea/html_code_form_search_entry_type_text_area.html";
67      private final String _template_html_front_code_entry_value = "skin/plugins/directory/entrytypetextarea/html_code_entry_value_type_text_area.html";
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public String getTemplateHtmlFormEntry( boolean isDisplayFront )
74      {
75          if ( isDisplayFront )
76          {
77              return _template_html_front_code_form_entry;
78          }
79  
80          return _template_html_code_form_entry;
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public String getTemplateHtmlRecordFieldValue( boolean isDisplayFront )
88      {
89          if ( isDisplayFront )
90          {
91              return _template_html_front_code_entry_value;
92          }
93  
94          return _template_html_code_entry_value;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public String getTemplateHtmlFormSearchEntry( boolean isDisplayFront )
102     {
103         if ( isDisplayFront )
104         {
105             return _template_html_front_code_form_search_entry;
106         }
107 
108         return _template_html_code_form_search_entry;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public String getEntryData( HttpServletRequest request, Locale locale )
116     {
117         String strTitle = request.getParameter( PARAMETER_TITLE );
118         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
119         String strHelpMessageSearch = ( request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE_SEARCH )
120                 .trim( ) : null;
121         String strComment = request.getParameter( PARAMETER_COMMENT );
122         String strValue = request.getParameter( PARAMETER_VALUE );
123         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
124         String strIndexed = request.getParameter( PARAMETER_INDEXED );
125         String strDocumentTitle = request.getParameter( PARAMETER_INDEXED_AS_TITLE );
126         String strDocumentSummary = request.getParameter( PARAMETER_INDEXED_AS_SUMMARY );
127         String strShowInAdvancedSearch = request.getParameter( PARAMETER_SHOWN_IN_ADVANCED_SEARCH );
128         String strShowInResultList = request.getParameter( PARAMETER_SHOWN_IN_RESULT_LIST );
129         String strShowInResultRecord = request.getParameter( PARAMETER_SHOWN_IN_RESULT_RECORD );
130         String strWidth = request.getParameter( PARAMETER_WIDTH );
131         String strHeight = request.getParameter( PARAMETER_HEIGHT );
132         String strShowInHistory = request.getParameter( PARAMETER_SHOWN_IN_HISTORY );
133         String strShowInExport = request.getParameter( PARAMETER_SHOWN_IN_EXPORT );
134         String strShowInCompleteness = request.getParameter( PARAMETER_SHOWN_IN_COMPLETENESS );
135 
136         int nWidth = DirectoryUtils.convertStringToInt( strWidth );
137         int nHeight = DirectoryUtils.convertStringToInt( strHeight );
138 
139         String strFieldError = DirectoryUtils.EMPTY_STRING;
140 
141         if ( ( strTitle == null ) || strTitle.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
142         {
143             strFieldError = FIELD_TITLE;
144         }
145 
146         else
147             if ( ( strWidth == null ) || strWidth.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
148             {
149                 strFieldError = FIELD_WIDTH;
150             }
151             else
152                 if ( ( strHeight == null ) || strHeight.trim( ).equals( DirectoryUtils.EMPTY_STRING ) )
153                 {
154                     strFieldError = FIELD_HEIGHT;
155                 }
156 
157         if ( !strFieldError.equals( DirectoryUtils.EMPTY_STRING ) )
158         {
159             Object [ ] tabRequiredFields = {
160                 I18nService.getLocalizedString( strFieldError, locale )
161             };
162 
163             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
164         }
165 
166         if ( nWidth == -1 )
167         {
168             strFieldError = FIELD_WIDTH;
169         }
170 
171         else
172             if ( nHeight == -1 )
173             {
174                 strFieldError = FIELD_HEIGHT;
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 ).setHeight( nHeight );
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.setShownInExport( strShowInExport != null );
211         this.setShownInCompleteness( strShowInCompleteness != null );
212 
213         return null;
214     }
215 
216     /**
217      * {@inheritDoc}
218      */
219     @Override
220     public String getTemplateCreate( )
221     {
222         return _template_create;
223     }
224 
225     /**
226      * {@inheritDoc}
227      */
228     @Override
229     public String getTemplateModify( )
230     {
231         return _template_modify;
232     }
233 
234     /**
235      * {@inheritDoc}
236      */
237     @Override
238     public void getRecordFieldData( Record record, List<String> lstValue, boolean bTestDirectoryError, boolean bAddNewValue, List<RecordField> listRecordField,
239             Locale locale ) throws DirectoryErrorException
240     {
241         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
242 
243         String strValueEntry = ( ( lstValue != null ) && ( lstValue.size( ) > 0 ) ) ? lstValue.get( 0 ) : null;
244         RecordField recordField = new RecordField( );
245         recordField.setEntry( this );
246 
247         if ( ( record != null ) && bAddNewValue )
248         {
249             RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
250             recordFieldFilter.setIdDirectory( record.getDirectory( ).getIdDirectory( ) );
251             recordFieldFilter.setIdEntry( this.getIdEntry( ) );
252             recordFieldFilter.setIdRecord( record.getIdRecord( ) );
253 
254             List<RecordField> recordFieldList = RecordFieldHome.getRecordFieldList( recordFieldFilter, plugin );
255 
256             if ( ( recordFieldList != null ) && !recordFieldList.isEmpty( ) && StringUtils.isNotBlank( recordFieldList.get( 0 ).getValue( ) ) )
257             {
258                 strValueEntry = recordFieldList.get( 0 ).getValue( ) + ", " + strValueEntry;
259             }
260         }
261 
262         if ( strValueEntry != null )
263         {
264             if ( bTestDirectoryError && this.isMandatory( ) && strValueEntry.equals( DirectoryUtils.EMPTY_STRING ) )
265             {
266                 throw new DirectoryErrorException( this.getTitle( ) );
267             }
268 
269             recordField.setValue( strValueEntry );
270         }
271 
272         listRecordField.add( recordField );
273     }
274 
275     /**
276      * {@inheritDoc}
277      */
278     @Override
279     public boolean isSortable( )
280     {
281         return true;
282     }
283 
284     /**
285      * {@inheritDoc}
286      */
287     @Override
288     public boolean isAnonymizable( )
289     {
290         return true;
291     }
292 }