View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.portal.business.user.attribute;
35  
36  import fr.paris.lutece.portal.business.user.AdminUser;
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.user.attribute.AttributeService;
40  import fr.paris.lutece.portal.web.constants.Messages;
41  
42  import org.apache.commons.collections.CollectionUtils;
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 implements ISimpleValuesAttributes
57  {
58      // CONSTANTS
59      private static final String EMPTY_STRING = "";
60      private static final String CONSTANT_UNDERSCORE = "_";
61  
62      // PARAMETERS
63      private static final String PARAMETER_TITLE = "title";
64      private static final String PARAMETER_HELP_MESSAGE = "help_message";
65      private static final String PARAMETER_MANDATORY = "mandatory";
66      private static final String PARAMETER_WIDTH = "width";
67      private static final String PARAMETER_MAX_SIZE_ENTER = "max_size_enter";
68      private static final String PARAMETER_VALUE = "value";
69      private static final String PARAMETER_IS_SHOWN_IN_SEARCH = "is_shown_in_search";
70      private static final String PARAMETER_IS_SHOWN_IN_RESULT_LIST = "is_shown_in_result_list";
71      private static final String PARAMETER_ATTRIBUTE = "attribute";
72  
73      // PROPERTY
74      private static final String PROPERTY_TYPE_TEXT = "portal.users.attribute.type.text";
75      private static final String PROPERTY_CREATE_TEXT_PAGETITLE = "portal.users.create_attribute.pageTitleAttributeText";
76      private static final String PROPERTY_MODIFY_TEXT_PAGETITLE = "portal.users.modify_attribute.pageTitleAttributeText";
77      private static final String PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS = "portal.users.message.noArithmeticalCharacters";
78  
79      // TEMPLATES
80      private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/user/attribute/text/create_attribute_text.html";
81      private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/user/attribute/text/modify_attribute_text.html";
82      private static final String TEMPLATE_HTML_FORM_ATTRIBUTE = "admin/user/attribute/text/html_code_form_attribute_text.html";
83      private static final String TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE = "admin/user/attribute/text/html_code_form_search_attribute_text.html";
84      private static final String TEMPLATE_HTML_VALUE = "admin/user/attribute/text/html_code_value_attribute_text.html";
85      private static final String REGEX_ID = "-?[0-9]+";
86  
87      /**
88       * Constructor
89       */
90      public AttributeText( )
91      {
92          // Ctor
93      }
94  
95      /**
96       * Get the template create an attribute
97       * 
98       * @return The URL of the template
99       */
100     @Override
101     public String getTemplateCreateAttribute( )
102     {
103         return TEMPLATE_CREATE_ATTRIBUTE;
104     }
105 
106     /**
107      * Get the template modify an attribute
108      * 
109      * @return The URL of the template
110      */
111     @Override
112     public String getTemplateModifyAttribute( )
113     {
114         return TEMPLATE_MODIFY_ATTRIBUTE;
115     }
116 
117     /**
118      * Get the template html form attribute
119      * 
120      * @return the template
121      */
122     @Override
123     public String getTemplateHtmlFormAttribute( )
124     {
125         return TEMPLATE_HTML_FORM_ATTRIBUTE;
126     }
127 
128     /**
129      * Get the template html form search attribute
130      * 
131      * @return the template
132      */
133     @Override
134     public String getTemplateHtmlFormSearchAttribute( )
135     {
136         return TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE;
137     }
138 
139     /**
140      * Get the template html for the value of the attribute
141      * 
142      * @return the template
143      */
144     @Override
145     public String getTemplateHtmlValue( )
146     {
147         return TEMPLATE_HTML_VALUE;
148     }
149 
150     /**
151      * Get the page title for create page
152      * 
153      * @return page title
154      */
155     @Override
156     public String getPropertyCreatePageTitle( )
157     {
158         return PROPERTY_CREATE_TEXT_PAGETITLE;
159     }
160 
161     /**
162      * Get the page title for modify page
163      * 
164      * @return page title
165      */
166     @Override
167     public String getPropertyModifyPageTitle( )
168     {
169         return PROPERTY_MODIFY_TEXT_PAGETITLE;
170     }
171 
172     /**
173      * Set the data of the attribute
174      * 
175      * @param request
176      *            HttpServletRequest
177      * @return null if there are no errors
178      */
179     @Override
180     public String setAttributeData( HttpServletRequest request )
181     {
182         String strTitle = request.getParameter( PARAMETER_TITLE );
183         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
184         String strIsShownInSearch = request.getParameter( PARAMETER_IS_SHOWN_IN_SEARCH );
185         String strIsShownInResultList = request.getParameter( PARAMETER_IS_SHOWN_IN_RESULT_LIST );
186         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
187         String strWidth = request.getParameter( PARAMETER_WIDTH );
188         String strMaxSizeEnter = request.getParameter( PARAMETER_MAX_SIZE_ENTER );
189         String strValue = request.getParameter( PARAMETER_VALUE );
190 
191         if ( StringUtils.isBlank( strTitle ) || StringUtils.isBlank( strWidth ) )
192         {
193             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
194         }
195 
196         if ( !strWidth.matches( REGEX_ID ) )
197         {
198             return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS, AdminMessage.TYPE_STOP );
199         }
200 
201         int nWidth = Integer.parseInt( strWidth );
202 
203         int nMaxSizeEnter;
204 
205         if ( StringUtils.isNotBlank( strMaxSizeEnter ) && !strMaxSizeEnter.matches( REGEX_ID ) )
206         {
207             return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS, AdminMessage.TYPE_STOP );
208         }
209         else
210             if ( ( strMaxSizeEnter == null ) || strMaxSizeEnter.equals( EMPTY_STRING ) )
211             {
212                 nMaxSizeEnter = -1;
213             }
214             else
215             {
216                 nMaxSizeEnter = Integer.parseInt( strMaxSizeEnter );
217             }
218 
219         setTitle( strTitle );
220         setHelpMessage( strHelpMessage );
221         setMandatory( strMandatory != null );
222         setShownInSearch( strIsShownInSearch != null );
223         setShownInResultList( strIsShownInResultList != null );
224 
225         if ( getListAttributeFields( ) == null )
226         {
227             List<AttributeField> listAttributeFields = new ArrayList<>( );
228             AttributeFieldr/attribute/AttributeField.html#AttributeField">AttributeField attributeField = new AttributeField( );
229             listAttributeFields.add( attributeField );
230             setListAttributeFields( listAttributeFields );
231         }
232 
233         getListAttributeFields( ).get( 0 ).setValue( strValue );
234         getListAttributeFields( ).get( 0 ).setWidth( nWidth );
235         getListAttributeFields( ).get( 0 ).setMaxSizeEnter( nMaxSizeEnter );
236 
237         return null;
238     }
239 
240     /**
241      * Set attribute type
242      * 
243      * @param locale
244      *            locale
245      */
246     @Override
247     public void setAttributeType( Locale locale )
248     {
249         AttributeTypeser/attribute/AttributeType.html#AttributeType">AttributeType attributeType = new AttributeType( );
250         attributeType.setLocale( locale );
251         attributeType.setClassName( this.getClass( ).getName( ) );
252         attributeType.setLabelType( PROPERTY_TYPE_TEXT );
253         setAttributeType( attributeType );
254     }
255 
256     /**
257      * Get the data of the user fields
258      * 
259      * @param request
260      *            HttpServletRequest
261      * @param user
262      *            user
263      * @return user field data
264      */
265     @Override
266     public List<AdminUserField> getUserFieldsData( HttpServletRequest request, AdminUser user )
267     {
268         String [ ] strValues = request.getParameterValues( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + getIdAttribute( ) );
269 
270         return getUserFieldsData( strValues, user );
271     }
272 
273     /**
274      * Get the data of the user fields
275      * 
276      * @param strValues
277      *            Values
278      * @param user
279      *            user
280      * @return user field data
281      */
282     @Override
283     public List<AdminUserField> getUserFieldsData( String [ ] strValues, AdminUser user )
284     {
285         List<AdminUserField> listUserFields = new ArrayList<>( );
286         AdminUserFields/user/attribute/AdminUserField.html#AdminUserField">AdminUserField userField = new AdminUserField( );
287         AttributeService.getInstance( ).setAttributeField( this );
288 
289         if ( strValues != null )
290         {
291             for ( String strValue : strValues )
292             {
293                 if ( strValue == null )
294                 {
295                     strValue = EMPTY_STRING;
296                 }
297 
298                 userField.setUser( user );
299                 userField.setAttribute( this );
300 
301                 if ( CollectionUtils.isNotEmpty( getListAttributeFields( ) ) )
302                 {
303                     userField.setAttributeField( getListAttributeFields( ).get( 0 ) );
304                 }
305 
306                 userField.setValue( strValue );
307 
308                 listUserFields.add( userField );
309             }
310         }
311 
312         return listUserFields;
313     }
314 
315     /**
316      * Get whether the attribute is anonymizable.
317      * 
318      * @return True if the attribute can be anonymized, false otherwise.
319      */
320     @Override
321     public boolean isAnonymizable( )
322     {
323         return true;
324     }
325 }