View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.document.service.attributes;
35  
36  import fr.paris.lutece.plugins.document.business.attributes.AttributeTypeParameter;
37  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttributeHome;
38  import fr.paris.lutece.portal.web.constants.Messages;
39  
40  import org.apache.commons.lang3.StringUtils;
41  
42  import java.util.ArrayList;
43  import java.util.Arrays;
44  import java.util.Collection;
45  import java.util.Collections;
46  import java.util.HashMap;
47  import java.util.List;
48  import java.util.Locale;
49  import java.util.Map;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  
54  /**
55   * Manager for ListBox Attribute
56   */
57  public class ListBoxManager extends DefaultManager
58  {
59      // TEMPLATES
60      private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/plugins/document/attributes/create_listbox.html";
61      private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/plugins/document/attributes/modify_listbox.html";
62      private static final String TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE = "admin/plugins/document/attributes/create_parameters_listbox.html";
63      private static final String TEMPLATE_MODIFY_PARAMETERS_ATTRIBUTE = "admin/plugins/document/attributes/modify_parameters_listbox.html";
64  
65      // PARAMETERS
66      private static final String PARAMETER_ITEMS_VALUE = "items_value";
67      private static final String PARAMETER_ITEMS_SELECT = "items_select";
68      private static final String PARAMETER_ADD = "add";
69      private static final String PARAMETER_DELETE = "delete";
70      private static final String PARAMETER_BY_DEFAULT = "bydefault";
71  
72      // NAMES
73      private static final String NAME_ITEMS = "items";
74      private static final String NAME_VALUE = "value";
75  
76      /**
77       * Gets the template to enter the attribute value
78       * @return The template to enter the attribute value
79       */
80      protected String getCreateTemplate(  )
81      {
82          return TEMPLATE_CREATE_ATTRIBUTE;
83      }
84  
85      /**
86       * Gets the template to modify the attribute value
87       * @return The template to modify the attribute value
88       */
89      protected String getModifyTemplate(  )
90      {
91          return TEMPLATE_MODIFY_ATTRIBUTE;
92      }
93  
94      /**
95       * Gets the template to enter the parameters of the attribute value
96       * @return The template to enter the parameters of the attribute value
97       */
98      protected String getCreateParametersTemplate(  )
99      {
100         return TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE;
101     }
102 
103     /**
104      * Gets the template to modify the parameters of the attribute value
105      * @return The template to modify the parameters of the attribute value
106      */
107     protected String getModifyParametersTemplate(  )
108     {
109         return TEMPLATE_MODIFY_PARAMETERS_ATTRIBUTE;
110     }
111 
112     /**
113      * Validate the value for the parameters
114      * @param listParameters The list of parameters to check
115      * @param locale The current Locale
116      * @return null if valid, otherwise message property
117      */
118     public String validateValueParameters( List<AttributeTypeParameter> listParameters, Locale locale )
119     {
120         for ( AttributeTypeParameter parameter : listParameters )
121         {
122             if ( !parameter.getValueList(  ).iterator(  ).hasNext(  ) )
123             {
124                 return Messages.MANDATORY_FIELDS;
125             }
126         }
127 
128         return null;
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public String getCreateParametersFormHtml( List<AttributeTypeParameter> listParameters, Locale locale )
136     {
137         // We sort parameters values alphabetically
138         if ( listParameters != null )
139         {
140             for ( AttributeTypeParameter attributeParameter : listParameters )
141             {
142                 List<String> listValues = attributeParameter.getValueList(  );
143 
144                 if ( ( listValues != null ) && ( listValues.size(  ) > 0 ) )
145                 {
146                     Collections.sort( listValues );
147                     attributeParameter.setValueList( listValues );
148                 }
149             }
150         }
151 
152         return super.getCreateParametersFormHtml( listParameters, locale );
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     @Override
159     public List<AttributeTypeParameter> getExtraParametersValues( Locale locale, int nAttributeId )
160     {
161         List<AttributeTypeParameter> listParameters = getExtraParameters( locale );
162 
163         for ( AttributeTypeParameter parameter : listParameters )
164         {
165             List<String> listValues = DocumentAttributeHome.getAttributeParameterValues( nAttributeId,
166                     parameter.getName(  ) );
167             Collections.sort( listValues );
168             parameter.setValueList( listValues );
169         }
170 
171         return listParameters;
172     }
173 
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     public List<AttributeTypeParameter> getValueParameters( HttpServletRequest request, Locale locale )
179     {
180         // Button "Add" new parameter
181         boolean bAddNewValue = StringUtils.isNotBlank( request.getParameter( PARAMETER_ADD ) );
182 
183         // Button "Delete" parameters
184         boolean bDeleteValue = StringUtils.isNotBlank( request.getParameter( PARAMETER_DELETE ) );
185         boolean bByDefault = StringUtils.isNotBlank( request.getParameter( PARAMETER_BY_DEFAULT ) );
186         List<AttributeTypeParameter> listParameters = super.getExtraParameters( locale );
187 
188         for ( AttributeTypeParameter parameter : listParameters )
189         {
190             List<String> listValues = new ArrayList<String>(  );
191             String[] arrayStrValues = request.getParameterValues( parameter.getName(  ) );
192 
193             if ( NAME_VALUE.equals( parameter.getName(  ) ) )
194             {
195                 // Define default value
196                 if ( bByDefault )
197                 {
198                     String strValue = request.getParameter( PARAMETER_ITEMS_SELECT );
199 
200                     if ( StringUtils.isNotBlank( strValue ) )
201                     {
202                         listValues.add( strValue );
203                     }
204                 }
205                 else
206                 {
207                     if ( ( arrayStrValues != null ) && ( arrayStrValues.length > 0 ) )
208                     {
209                         listValues.addAll( Arrays.asList( arrayStrValues ) );
210                     }
211                 }
212             }
213             else if ( NAME_ITEMS.equals( parameter.getName(  ) ) )
214             {
215                 if ( ( arrayStrValues != null ) && ( arrayStrValues.length > 0 ) )
216                 {
217                     listValues.addAll( Arrays.asList( arrayStrValues ) );
218                 }
219 
220                 // Add new value
221                 if ( bAddNewValue )
222                 {
223                     String strNewValue = request.getParameter( PARAMETER_ITEMS_VALUE );
224 
225                     if ( StringUtils.isNotBlank( strNewValue ) )
226                     {
227                         listValues.add( strNewValue );
228                     }
229                 }
230 
231                 // Remove value
232                 if ( bDeleteValue )
233                 {
234                     String strValue = request.getParameter( PARAMETER_ITEMS_SELECT );
235 
236                     if ( StringUtils.isNotBlank( strValue ) )
237                     {
238                         listValues.remove( strValue );
239                         removeParameterValue( listParameters, strValue );
240                     }
241                 }
242             }
243 
244             parameter.setValueList( listValues );
245             listValues.clear(  );
246         }
247 
248         return listParameters;
249     }
250 
251     /**
252      * {@inheritDoc}
253      */
254     @Override
255     protected Map<String, List<String>> getParameters( int nAttributeId, Locale locale )
256     {
257         HashMap<String, List<String>> mapParameters = new HashMap<String, List<String>>(  );
258         Collection<AttributeTypeParameter> listParameters = getAttributeParametersValues( nAttributeId, locale );
259 
260         for ( AttributeTypeParameter parameter : listParameters )
261         {
262             // We sort attributes alphabetically
263             List<String> listValues = parameter.getValueList(  );
264             Collections.sort( listValues );
265             mapParameters.put( parameter.getName(  ), listValues );
266         }
267 
268         // Set all missing parameters with their default values
269         for ( AttributeTypeParameter parameter : getExtraParameters( locale ) )
270         {
271             if ( !mapParameters.containsKey( parameter.getName(  ) ) )
272             {
273                 mapParameters.put( parameter.getName(  ), parameter.getDefaultValue(  ) );
274             }
275         }
276 
277         return mapParameters;
278     }
279 
280     /**
281      * Remove a parameter value from a list
282      * @param listParameters the list of parameters
283      * @param strValueToRemove the value to remove
284      */
285     private void removeParameterValue( List<AttributeTypeParameter> listParameters, String strValueToRemove )
286     {
287         for ( AttributeTypeParameter parameter : listParameters )
288         {
289             if ( NAME_VALUE.equals( parameter.getName(  ) ) )
290             {
291                 if ( StringUtils.isNotBlank( strValueToRemove ) )
292                 {
293                     List<String> listValues = parameter.getValueList(  );
294                     listValues.remove( strValueToRemove );
295                     parameter.setValueList( listValues );
296 
297                     break;
298                 }
299             }
300         }
301     }
302 }