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