1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
56
57 public class ListBoxManager extends DefaultManager
58 {
59
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
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
73 private static final String NAME_ITEMS = "items";
74 private static final String NAME_VALUE = "value";
75
76
77
78
79
80 protected String getCreateTemplate( )
81 {
82 return TEMPLATE_CREATE_ATTRIBUTE;
83 }
84
85
86
87
88
89 protected String getModifyTemplate( )
90 {
91 return TEMPLATE_MODIFY_ATTRIBUTE;
92 }
93
94
95
96
97
98 protected String getCreateParametersTemplate( )
99 {
100 return TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE;
101 }
102
103
104
105
106
107 protected String getModifyParametersTemplate( )
108 {
109 return TEMPLATE_MODIFY_PARAMETERS_ATTRIBUTE;
110 }
111
112
113
114
115
116
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
133
134 @Override
135 public String getCreateParametersFormHtml( List<AttributeTypeParameter> listParameters, Locale locale )
136 {
137
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
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
176
177 @Override
178 public List<AttributeTypeParameter> getValueParameters( HttpServletRequest request, Locale locale )
179 {
180
181 boolean bAddNewValue = StringUtils.isNotBlank( request.getParameter( PARAMETER_ADD ) );
182
183
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
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
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
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
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
263 List<String> listValues = parameter.getValueList( );
264 Collections.sort( listValues );
265 mapParameters.put( parameter.getName( ), listValues );
266 }
267
268
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
282
283
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 }