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.AttributeFieldService;
40  import fr.paris.lutece.portal.web.constants.Messages;
41  
42  import org.apache.commons.lang3.StringUtils;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  import java.util.Locale;
47  
48  import javax.servlet.http.HttpServletRequest;
49  
50  /**
51   *
52   * AttributeComboBox
53   *
54   */
55  public class AttributeComboBox extends AbstractAttribute implements ISimpleValuesAttributes
56  {
57      // Constants
58      private static final String CONSTANT_UNDERSCORE = "_";
59  
60      // Parameters
61      private static final String PARAMETER_TITLE = "title";
62      private static final String PARAMETER_HELP_MESSAGE = "help_message";
63      private static final String PARAMETER_MANDATORY = "mandatory";
64      private static final String PARAMETER_MULTIPLE = "multiple";
65      private static final String PARAMETER_IS_SHOWN_IN_SEARCH = "is_shown_in_search";
66      private static final String PARAMETER_IS_SHOWN_IN_RESULT_LIST = "is_shown_in_result_list";
67      private static final String PARAMETER_ATTRIBUTE = "attribute";
68  
69      // Properties
70      private static final String PROPERTY_TYPE_COMBOBOX = "portal.users.attribute.type.comboBox";
71      private static final String PROPERTY_CREATE_COMBOBOX_PAGETITLE = "portal.users.create_attribute.pageTitleAttributeComboBox";
72      private static final String PROPERTY_MODIFY_COMBOBOX_PAGETITLE = "portal.users.modify_attribute.pageTitleAttributeComboBox";
73  
74      // Templates
75      private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/user/attribute/combobox/create_attribute_combobox.html";
76      private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/user/attribute/combobox/modify_attribute_combobox.html";
77      private static final String TEMPLATE_HTML_FORM_ATTRIBUTE = "admin/user/attribute/combobox/html_code_form_attribute_combobox.html";
78      private static final String TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE = "admin/user/attribute/combobox/html_code_form_search_attribute_combobox.html";
79      private static final String TEMPLATE_HTML_VALUE = "admin/user/attribute/combobox/html_code_value_attribute_combobox.html";
80  
81      /**
82       * Constructor
83       */
84      public AttributeComboBox( )
85      {
86          // Ctor
87      }
88  
89      /**
90       * Get the template create an attribute
91       * 
92       * @return The URL of the template
93       */
94      @Override
95      public String getTemplateCreateAttribute( )
96      {
97          return TEMPLATE_CREATE_ATTRIBUTE;
98      }
99  
100     /**
101      * Get the template modify an attribute
102      * 
103      * @return The URL of the template
104      */
105     @Override
106     public String getTemplateModifyAttribute( )
107     {
108         return TEMPLATE_MODIFY_ATTRIBUTE;
109     }
110 
111     /**
112      * Get the template html form attribute
113      * 
114      * @return the template
115      */
116     @Override
117     public String getTemplateHtmlFormAttribute( )
118     {
119         return TEMPLATE_HTML_FORM_ATTRIBUTE;
120     }
121 
122     /**
123      * Get the template html form search attribute
124      * 
125      * @return the template
126      */
127     @Override
128     public String getTemplateHtmlFormSearchAttribute( )
129     {
130         return TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE;
131     }
132 
133     /**
134      * Get the template html for the value of the attribute
135      * 
136      * @return the template
137      */
138     @Override
139     public String getTemplateHtmlValue( )
140     {
141         return TEMPLATE_HTML_VALUE;
142     }
143 
144     /**
145      * Get page title for create page
146      * 
147      * @return page title
148      */
149     @Override
150     public String getPropertyCreatePageTitle( )
151     {
152         return PROPERTY_CREATE_COMBOBOX_PAGETITLE;
153     }
154 
155     /**
156      * Get page title for modify page
157      * 
158      * @return page title
159      */
160     @Override
161     public String getPropertyModifyPageTitle( )
162     {
163         return PROPERTY_MODIFY_COMBOBOX_PAGETITLE;
164     }
165 
166     /**
167      * Set the data of the attribute
168      * 
169      * @param request
170      *            HttpServletRequest
171      * @return null if there are no errors
172      */
173     @Override
174     public String setAttributeData( HttpServletRequest request )
175     {
176         String strTitle = request.getParameter( PARAMETER_TITLE );
177         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
178         String strIsShownInSearch = request.getParameter( PARAMETER_IS_SHOWN_IN_SEARCH );
179         String strIsShownInResultList = request.getParameter( PARAMETER_IS_SHOWN_IN_RESULT_LIST );
180         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
181         String strMultiple = request.getParameter( PARAMETER_MULTIPLE );
182 
183         if ( StringUtils.isNotBlank( strTitle ) )
184         {
185             setTitle( strTitle );
186             setHelpMessage( strHelpMessage );
187             setMandatory( strMandatory != null );
188             setShownInSearch( strIsShownInSearch != null );
189             setShownInResultList( strIsShownInResultList != null );
190 
191             if ( getListAttributeFields( ) == null )
192             {
193                 List<AttributeField> listAttributeFields = new ArrayList<>( );
194                 AttributeFieldr/attribute/AttributeField.html#AttributeField">AttributeField attributeField = new AttributeField( );
195                 listAttributeFields.add( attributeField );
196                 setListAttributeFields( listAttributeFields );
197             }
198 
199             getListAttributeFields( ).get( 0 ).setMultiple( strMultiple != null );
200 
201             return null;
202         }
203 
204         return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
205     }
206 
207     /**
208      * Set attribute type
209      * 
210      * @param locale
211      *            locale
212      */
213     @Override
214     public void setAttributeType( Locale locale )
215     {
216         AttributeTypeser/attribute/AttributeType.html#AttributeType">AttributeType attributeType = new AttributeType( );
217         attributeType.setLocale( locale );
218         attributeType.setClassName( this.getClass( ).getName( ) );
219         attributeType.setLabelType( PROPERTY_TYPE_COMBOBOX );
220         setAttributeType( attributeType );
221     }
222 
223     /**
224      * Get the data of the user fields
225      * 
226      * @param request
227      *            HttpServletRequest
228      * @param user
229      *            user
230      * @return user field data
231      */
232     @Override
233     public List<AdminUserField> getUserFieldsData( HttpServletRequest request, AdminUser user )
234     {
235         String [ ] strValues = request.getParameterValues( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + getIdAttribute( ) );
236 
237         return getUserFieldsData( strValues, user );
238     }
239 
240     /**
241      * Get the data of the user fields
242      * 
243      * @param strValues
244      *            Values
245      * @param user
246      *            user
247      * @return user field data
248      */
249     @Override
250     public List<AdminUserField> getUserFieldsData( String [ ] strValues, AdminUser user )
251     {
252         List<AdminUserField> listUserFields = new ArrayList<>( );
253 
254         if ( strValues != null )
255         {
256             for ( String strValue : strValues )
257             {
258                 AdminUserFields/user/attribute/AdminUserField.html#AdminUserField">AdminUserField userField = new AdminUserField( );
259                 AttributeField attributeField;
260 
261                 if ( StringUtils.isNotBlank( strValue ) && StringUtils.isNumeric( strValue ) )
262                 {
263                     int nIdField = Integer.parseInt( strValue );
264                     attributeField = AttributeFieldService.getInstance( ).getAttributeField( nIdField );
265                 }
266                 else
267                 {
268                     attributeField = new AttributeField( );
269                     attributeField.setAttribute( this );
270                     attributeField.setTitle( strValue );
271                     attributeField.setValue( strValue );
272                 }
273 
274                 userField.setUser( user );
275                 userField.setAttribute( this );
276                 userField.setAttributeField( attributeField );
277                 userField.setValue( attributeField.getTitle( ) );
278 
279                 listUserFields.add( userField );
280             }
281         }
282         // If the combobox is multiple and nothing is selected, the request will have no paramater at all
283         else
284         {
285             AdminUserFields/user/attribute/AdminUserField.html#AdminUserField">AdminUserField userField = new AdminUserField( );
286             AttributeField attributeField;
287             attributeField = new AttributeField( );
288             attributeField.setAttribute( this );
289             attributeField.setTitle( "" );
290             attributeField.setValue( "" );
291             userField.setUser( user );
292             userField.setAttribute( this );
293             userField.setAttributeField( attributeField );
294             userField.setValue( attributeField.getTitle( ) );
295 
296             listUserFields.add( userField );
297         }
298 
299         return listUserFields;
300     }
301 
302     /**
303      * Get whether the attribute is anonymizable.
304      * 
305      * @return True if the attribute can be anonymized, false otherwise.
306      */
307     @Override
308     public boolean isAnonymizable( )
309     {
310         return false;
311     }
312 }