View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of 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.mylutece.business.attribute;
35  
36  import fr.paris.lutece.plugins.mylutece.service.MyLutecePlugin;
37  import fr.paris.lutece.portal.service.message.AdminMessage;
38  import fr.paris.lutece.portal.service.message.AdminMessageService;
39  import fr.paris.lutece.portal.service.plugin.Plugin;
40  import fr.paris.lutece.portal.service.plugin.PluginService;
41  import fr.paris.lutece.portal.web.constants.Messages;
42  
43  import org.apache.commons.lang3.StringUtils;
44  
45  import java.util.ArrayList;
46  import java.util.List;
47  import java.util.Locale;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  /**
52   *
53   * AttributeText
54   *
55   */
56  public class AttributeText extends AbstractAttribute
57  {
58      // CONSTANTS
59      private static final String EMPTY_STRING = "";
60  
61      // PARAMETERS
62      private static final String PARAMETER_TITLE = "title";
63      private static final String PARAMETER_HELP_MESSAGE = "help_message";
64      private static final String PARAMETER_MANDATORY = "mandatory";
65      private static final String PARAMETER_WIDTH = "width";
66      private static final String PARAMETER_MAX_SIZE_ENTER = "max_size_enter";
67      private static final String PARAMETER_VALUE = "value";
68      private static final String PARAMETER_IS_SHOWN_IN_SEARCH = "is_shown_in_search";
69  
70      // PROPERTY
71      private static final String PROPERTY_TYPE_TEXT = "mylutece.attribute.type.text";
72      private static final String PROPERTY_CREATE_TEXT_PAGETITLE = "mylutece.create_attribute.pageTitleAttributeText";
73      private static final String PROPERTY_MODIFY_TEXT_PAGETITLE = "mylutece.modify_attribute.pageTitleAttributeText";
74      private static final String PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS = "mylutece.message.noArithmeticalCharacters";
75  
76      // TEMPLATES
77      private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/plugins/mylutece/attribute/text/create_attribute_text.html";
78      private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/plugins/mylutece/attribute/text/modify_attribute_text.html";
79      private static final String TEMPLATE_HTML_FORM_ATTRIBUTE = "admin/plugins/mylutece/attribute/text/html_code_form_attribute_text.html";
80      private static final String TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE = "admin/plugins/mylutece/attribute/text/html_code_form_search_attribute_text.html";
81  
82      /**
83       * Constructor
84       */
85      public AttributeText( )
86      {
87      }
88  
89      /**
90       * Get the template create an attribute
91       * 
92       * @return The URL of the template
93       */
94      public String getTemplateCreateAttribute( )
95      {
96          return TEMPLATE_CREATE_ATTRIBUTE;
97      }
98  
99      /**
100      * Get the template modify an attribute
101      * 
102      * @return The URL of the template
103      */
104     public String getTemplateModifyAttribute( )
105     {
106         return TEMPLATE_MODIFY_ATTRIBUTE;
107     }
108 
109     /**
110      * Get the template html form attribute
111      * 
112      * @return the template
113      */
114     public String getTemplateHtmlFormAttribute( )
115     {
116         return TEMPLATE_HTML_FORM_ATTRIBUTE;
117     }
118 
119     /**
120      * Get the template html form search attribute
121      * 
122      * @return the template
123      */
124     public String getTemplateHtmlFormSearchAttribute( )
125     {
126         return TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE;
127     }
128 
129     /**
130      * Get the page title for create page
131      * 
132      * @return page title
133      */
134     public String getPropertyCreatePageTitle( )
135     {
136         return PROPERTY_CREATE_TEXT_PAGETITLE;
137     }
138 
139     /**
140      * Get the page title for modify page
141      * 
142      * @return page title
143      */
144     public String getPropertyModifyPageTitle( )
145     {
146         return PROPERTY_MODIFY_TEXT_PAGETITLE;
147     }
148 
149     /**
150      * Set the data of the attribute
151      * 
152      * @param request
153      *            HttpServletRequest
154      * @return null if there are no errors
155      */
156     public String setAttributeData( HttpServletRequest request )
157     {
158         String strTitle = request.getParameter( PARAMETER_TITLE );
159         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
160         String strIsShownInSearch = request.getParameter( PARAMETER_IS_SHOWN_IN_SEARCH );
161         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
162         String strWidth = request.getParameter( PARAMETER_WIDTH );
163         String strMaxSizeEnter = request.getParameter( PARAMETER_MAX_SIZE_ENTER );
164         String strValue = request.getParameter( PARAMETER_VALUE );
165 
166         if ( StringUtils.isBlank( strTitle ) )
167         {
168             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
169         }
170 
171         if ( StringUtils.isBlank( strWidth ) )
172         {
173             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
174         }
175 
176         int nWidth;
177 
178         try
179         {
180             nWidth = Integer.parseInt( strWidth );
181         }
182         catch( NumberFormatException nfe )
183         {
184             return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS, AdminMessage.TYPE_STOP );
185         }
186 
187         int nMaxSizeEnter = -1;
188 
189         if ( StringUtils.isNotBlank( strMaxSizeEnter ) )
190         {
191             try
192             {
193                 nMaxSizeEnter = Integer.parseInt( strMaxSizeEnter );
194             }
195             catch( NumberFormatException nfe )
196             {
197                 return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS, AdminMessage.TYPE_STOP );
198             }
199         }
200 
201         setTitle( strTitle );
202         setHelpMessage( strHelpMessage );
203         setMandatory( strMandatory != null );
204         setShownInSearch( strIsShownInSearch != null );
205 
206         if ( getListAttributeFields( ) == null )
207         {
208             List<AttributeField> listAttributeFields = new ArrayList<AttributeField>( );
209             AttributeFieldsiness/attribute/AttributeField.html#AttributeField">AttributeField attributeField = new AttributeField( );
210             listAttributeFields.add( attributeField );
211             setListAttributeFields( listAttributeFields );
212         }
213 
214         getListAttributeFields( ).get( 0 ).setValue( strValue );
215         getListAttributeFields( ).get( 0 ).setWidth( nWidth );
216         getListAttributeFields( ).get( 0 ).setMaxSizeEnter( nMaxSizeEnter );
217 
218         return null;
219     }
220 
221     /**
222      * Set attribute type
223      * 
224      * @param locale
225      *            locale
226      */
227     public void setAttributeType( Locale locale )
228     {
229         AttributeTypebusiness/attribute/AttributeType.html#AttributeType">AttributeType attributeType = new AttributeType( );
230         attributeType.setLocale( locale );
231         attributeType.setClassName( this.getClass( ).getName( ) );
232         attributeType.setLabelType( PROPERTY_TYPE_TEXT );
233         setAttributeType( attributeType );
234     }
235 
236     /**
237      * Get the data of the user fields
238      * 
239      * @param values
240      *            The values of user fields
241      * @param nIdUser
242      *            Id of the user
243      * @return user field data
244      */
245     public List<MyLuteceUserField> getUserFieldsData( String [ ] values, int nIdUser )
246     {
247         List<MyLuteceUserField> listUserFields = new ArrayList<MyLuteceUserField>( );
248         MyLuteceUserFieldbusiness/attribute/MyLuteceUserField.html#MyLuteceUserField">MyLuteceUserField userField = new MyLuteceUserField( );
249         Plugin plugin = PluginService.getPlugin( MyLutecePlugin.PLUGIN_NAME );
250         List<AttributeField> listAttributeFields = AttributeFieldHome.selectAttributeFieldsByIdAttribute( _nIdAttribute, plugin );
251         String strValue;
252 
253         if ( ( values == null ) || ( values [0] == null ) )
254         {
255             strValue = EMPTY_STRING;
256         }
257         else
258         {
259             strValue = values [0];
260         }
261 
262         userField.setUserId( nIdUser );
263         userField.setAttribute( this );
264         userField.setAttributeField( listAttributeFields.get( 0 ) );
265         userField.setValue( strValue );
266 
267         listUserFields.add( userField );
268 
269         return listUserFields;
270     }
271 
272     /**
273      * Get whether the attribute is anonymizable.
274      * 
275      * @return True if the attribute can be anonymized, false otherwise.
276      */
277     public boolean isAnonymizable( )
278     {
279         return true;
280     }
281 }