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   * AttributeComboBox
54   *
55   */
56  public class AttributeComboBox 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_MULTIPLE = "multiple";
66      private static final String PARAMETER_IS_SHOWN_IN_SEARCH = "is_shown_in_search";
67  
68      // PROPERTY
69      private static final String PROPERTY_TYPE_COMBOBOX = "mylutece.attribute.type.comboBox";
70      private static final String PROPERTY_CREATE_COMBOBOX_PAGETITLE = "mylutece.create_attribute.pageTitleAttributeComboBox";
71      private static final String PROPERTY_MODIFY_COMBOBOX_PAGETITLE = "mylutece.modify_attribute.pageTitleAttributeComboBox";
72  
73      // TEMPLATES
74      private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/plugins/mylutece/attribute/combobox/create_attribute_combobox.html";
75      private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/plugins/mylutece/attribute/combobox/modify_attribute_combobox.html";
76      private static final String TEMPLATE_HTML_FORM_ATTRIBUTE = "admin/plugins/mylutece/attribute/combobox/html_code_form_attribute_combobox.html";
77      private static final String TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE = "admin/plugins/mylutece/attribute/combobox/html_code_form_search_attribute_combobox.html";
78  
79      /**
80       * Constructor
81       */
82      public AttributeComboBox( )
83      {
84      }
85  
86      /**
87       * Get the template create an attribute
88       * 
89       * @return The URL of the template
90       */
91      public String getTemplateCreateAttribute( )
92      {
93          return TEMPLATE_CREATE_ATTRIBUTE;
94      }
95  
96      /**
97       * Get the template modify an attribute
98       * 
99       * @return The URL of the template
100      */
101     public String getTemplateModifyAttribute( )
102     {
103         return TEMPLATE_MODIFY_ATTRIBUTE;
104     }
105 
106     /**
107      * Get the template html form attribute
108      * 
109      * @return the template
110      */
111     public String getTemplateHtmlFormAttribute( )
112     {
113         return TEMPLATE_HTML_FORM_ATTRIBUTE;
114     }
115 
116     /**
117      * Get the template html form search attribute
118      * 
119      * @return the template
120      */
121     public String getTemplateHtmlFormSearchAttribute( )
122     {
123         return TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE;
124     }
125 
126     /**
127      * Get page title for create page
128      * 
129      * @return page title
130      */
131     public String getPropertyCreatePageTitle( )
132     {
133         return PROPERTY_CREATE_COMBOBOX_PAGETITLE;
134     }
135 
136     /**
137      * Get page title for modify page
138      * 
139      * @return page title
140      */
141     public String getPropertyModifyPageTitle( )
142     {
143         return PROPERTY_MODIFY_COMBOBOX_PAGETITLE;
144     }
145 
146     /**
147      * Set the data of the attribute
148      * 
149      * @param request
150      *            HttpServletRequest
151      * @return null if there are no errors
152      */
153     public String setAttributeData( HttpServletRequest request )
154     {
155         String strTitle = request.getParameter( PARAMETER_TITLE );
156         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
157         String strIsShownInSearch = request.getParameter( PARAMETER_IS_SHOWN_IN_SEARCH );
158         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
159         String strMultiple = request.getParameter( PARAMETER_MULTIPLE );
160 
161         if ( ( strTitle == null ) || ( strTitle.equals( EMPTY_STRING ) ) )
162         {
163             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
164         }
165 
166         setTitle( strTitle );
167         setHelpMessage( strHelpMessage );
168         setMandatory( strMandatory != null );
169         setShownInSearch( strIsShownInSearch != null );
170 
171         if ( getListAttributeFields( ) == null )
172         {
173             List<AttributeField> listAttributeFields = new ArrayList<AttributeField>( );
174             AttributeFieldsiness/attribute/AttributeField.html#AttributeField">AttributeField attributeField = new AttributeField( );
175             listAttributeFields.add( attributeField );
176             setListAttributeFields( listAttributeFields );
177         }
178 
179         getListAttributeFields( ).get( 0 ).setMultiple( strMultiple != null );
180 
181         return null;
182     }
183 
184     /**
185      * Set attribute type
186      * 
187      * @param locale
188      *            locale
189      */
190     public void setAttributeType( Locale locale )
191     {
192         AttributeTypebusiness/attribute/AttributeType.html#AttributeType">AttributeType attributeType = new AttributeType( );
193         attributeType.setLocale( locale );
194         attributeType.setClassName( this.getClass( ).getName( ) );
195         attributeType.setLabelType( PROPERTY_TYPE_COMBOBOX );
196         setAttributeType( attributeType );
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     @Override
203     public List<MyLuteceUserField> getUserFieldsData( String [ ] values, int nIdUser )
204     {
205         List<MyLuteceUserField> listUserFields = new ArrayList<MyLuteceUserField>( );
206 
207         if ( values != null )
208         {
209             for ( String strValue : values )
210             {
211                 MyLuteceUserFieldbusiness/attribute/MyLuteceUserField.html#MyLuteceUserField">MyLuteceUserField userField = new MyLuteceUserField( );
212                 AttributeField attributeField;
213 
214                 if ( ( strValue == null ) || strValue.equals( EMPTY_STRING ) )
215                 {
216                     strValue = EMPTY_STRING;
217                     attributeField = new AttributeField( );
218                     attributeField.setAttribute( this );
219                     attributeField.setTitle( EMPTY_STRING );
220                     attributeField.setValue( EMPTY_STRING );
221                 }
222                 else
223                     if ( StringUtils.isNumeric( strValue ) )
224                     {
225                         int nIdField = Integer.parseInt( strValue );
226                         Plugin plugin = PluginService.getPlugin( MyLutecePlugin.PLUGIN_NAME );
227                         attributeField = AttributeFieldHome.findByPrimaryKey( nIdField, plugin );
228                     }
229                     else
230                     {
231                         attributeField = new AttributeField( );
232                         attributeField.setAttribute( this );
233                         attributeField.setTitle( strValue );
234                         attributeField.setValue( strValue );
235                     }
236 
237                 userField.setUserId( nIdUser );
238                 userField.setAttribute( this );
239                 userField.setAttributeField( attributeField );
240                 userField.setValue( attributeField.getTitle( ) );
241 
242                 listUserFields.add( userField );
243             }
244         }
245 
246         return listUserFields;
247     }
248 
249     /**
250      * Get whether the attribute is anonymizable.
251      * 
252      * @return True if the attribute can be anonymized, false otherwise.
253      */
254     public boolean isAnonymizable( )
255     {
256         return false;
257     }
258 }