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.portal.web.user.attribute;
35
36 import fr.paris.lutece.portal.business.user.attribute.AttributeField;
37 import fr.paris.lutece.portal.business.user.attribute.IAttribute;
38 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
39 import fr.paris.lutece.portal.service.message.AdminMessage;
40 import fr.paris.lutece.portal.service.message.AdminMessageService;
41 import fr.paris.lutece.portal.service.security.SecurityTokenService;
42 import fr.paris.lutece.portal.service.template.AppTemplateService;
43 import fr.paris.lutece.portal.service.user.attribute.AdminUserFieldService;
44 import fr.paris.lutece.portal.service.user.attribute.AttributeFieldService;
45 import fr.paris.lutece.portal.service.user.attribute.AttributeService;
46 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
47 import fr.paris.lutece.portal.web.constants.Messages;
48 import fr.paris.lutece.util.html.HtmlTemplate;
49
50 import org.apache.commons.collections.CollectionUtils;
51 import org.apache.commons.lang3.StringUtils;
52
53 import java.util.HashMap;
54 import java.util.Iterator;
55 import java.util.List;
56 import java.util.Map;
57
58 import javax.servlet.http.HttpServletRequest;
59
60
61
62
63 public class AttributeFieldJspBean extends AdminFeaturesPageJspBean
64 {
65
66
67
68 private static final long serialVersionUID = 3304151197655135630L;
69
70
71 private static final String QUESTION_MARK = "?";
72 private static final String EQUAL = "=";
73
74
75 private static final String PROPERTY_CREATE_ATTRIBUTE_FIELDS_PAGETITLE = "portal.users.create_attribute_field.pageTitle";
76 private static final String PROPERTY_MODIFY_ATTRIBUTE_FIELDS_PAGETITLE = "portal.users.modify_attribute_field.pageTitle";
77 private static final String PROPERTY_MESSAGE_CONFIRM_REMOVE_ATTRIBUTE_FIELD = "portal.users.modify_attribute.message.removeAttributeField";
78
79
80 private static final String TEMPLATE_CREATE_ATTRIBUTE_FIELD = "admin/user/attribute/create_attribute_field.html";
81 private static final String TEMPLATE_MODIFY_ATTRIBUTE_FIELD = "admin/user/attribute/modify_attribute_field.html";
82
83
84 private static final String PARAMETER_CANCEL = "cancel";
85 private static final String PARAMETER_ID_ATTRIBUTE = "id_attribute";
86 private static final String PARAMETER_TITLE = "title";
87 private static final String PARAMETER_VALUE = "value";
88 private static final String PARAMETER_DEFAULT_VALUE = "default_value";
89 private static final String PARAMETER_ID_FIELD = "id_field";
90
91
92 private static final String MARK_ATTRIBUTE_FIELD = "attribute_field";
93 private static final String MARK_ATTRIBUTE = "attribute";
94
95
96 private static final String JSP_MODIFY_ATTRIBUTE = "ModifyAttribute.jsp";
97 private static final String JSP_URL_REMOVE_ATTRIBUTE_FIELD = "jsp/admin/user/attribute/DoRemoveAttributeField.jsp";
98 private static final AttributeService _attributeService = AttributeService.getInstance( );
99 private static final AttributeFieldService _attributeFieldService = AttributeFieldService.getInstance( );
100
101
102
103
104
105
106
107
108 public String getCreateAttributeField( HttpServletRequest request )
109 {
110 setPageTitleProperty( PROPERTY_CREATE_ATTRIBUTE_FIELDS_PAGETITLE );
111
112 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
113 int nIdAttribute = Integer.parseInt( strIdAttribute );
114
115 IAttribute attribute = _attributeService.getAttributeWithoutFields( nIdAttribute, getLocale( ) );
116
117 HtmlTemplate template;
118 Map<String, Object> model = new HashMap<>( );
119 model.put( MARK_ATTRIBUTE, attribute );
120 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_ATTRIBUTE_FIELD ) );
121
122 template = AppTemplateService.getTemplate( TEMPLATE_CREATE_ATTRIBUTE_FIELD, getLocale( ), model );
123
124 return getAdminPage( template.getHtml( ) );
125 }
126
127
128
129
130
131
132
133
134
135 public String doCreateAttributeField( HttpServletRequest request ) throws AccessDeniedException
136 {
137 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
138 int nIdAttribute = Integer.parseInt( strIdAttribute );
139 String strTitle = request.getParameter( PARAMETER_TITLE );
140 String strValue = request.getParameter( PARAMETER_VALUE );
141 String strDefaultValue = request.getParameter( PARAMETER_DEFAULT_VALUE );
142 String strCancel = request.getParameter( PARAMETER_CANCEL );
143
144 if ( StringUtils.isEmpty( strCancel ) )
145 {
146 if ( StringUtils.isBlank( strTitle ) )
147 {
148 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
149 }
150
151 if ( StringUtils.isBlank( strValue ) )
152 {
153 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
154 }
155
156 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_ATTRIBUTE_FIELD ) )
157 {
158 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
159 }
160 AttributeFieldr/attribute/AttributeField.html#AttributeField">AttributeField attributeField = new AttributeField( );
161 attributeField.setTitle( strTitle );
162 attributeField.setValue( strValue );
163 attributeField.setDefaultValue( strDefaultValue != null );
164
165 IAttribute attribute = _attributeService.getAttributeWithoutFields( nIdAttribute, getLocale( ) );
166 attributeField.setAttribute( attribute );
167 _attributeFieldService.createAttributeField( attributeField );
168 }
169
170 return JSP_MODIFY_ATTRIBUTE + QUESTION_MARK + PARAMETER_ID_ATTRIBUTE + EQUAL + nIdAttribute;
171 }
172
173
174
175
176
177
178
179
180 public String getModifyAttributeField( HttpServletRequest request )
181 {
182 setPageTitleProperty( PROPERTY_MODIFY_ATTRIBUTE_FIELDS_PAGETITLE );
183
184 String strIdField = request.getParameter( PARAMETER_ID_FIELD );
185 int nIdField = Integer.parseInt( strIdField );
186 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
187 int nIdAttribute = Integer.parseInt( strIdAttribute );
188
189 IAttribute attribute = _attributeService.getAttributeWithoutFields( nIdAttribute, getLocale( ) );
190
191 AttributeField attributeField = _attributeFieldService.getAttributeField( nIdField );
192
193 HtmlTemplate template;
194 Map<String, Object> model = new HashMap<>( );
195 model.put( MARK_ATTRIBUTE_FIELD, attributeField );
196 model.put( MARK_ATTRIBUTE, attribute );
197 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_ATTRIBUTE_FIELD ) );
198
199 template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_ATTRIBUTE_FIELD, getLocale( ), model );
200
201 return getAdminPage( template.getHtml( ) );
202 }
203
204
205
206
207
208
209
210
211
212
213 public String doModifyAttributeField( HttpServletRequest request ) throws AccessDeniedException
214 {
215 String strTitle = request.getParameter( PARAMETER_TITLE );
216 String strValue = request.getParameter( PARAMETER_VALUE );
217 String strDefaultValue = request.getParameter( PARAMETER_DEFAULT_VALUE );
218 String strIdField = request.getParameter( PARAMETER_ID_FIELD );
219 int nIdField = Integer.parseInt( strIdField );
220 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
221 String strCancel = request.getParameter( PARAMETER_CANCEL );
222
223 if ( StringUtils.isEmpty( strCancel ) )
224 {
225 if ( StringUtils.isBlank( strTitle ) )
226 {
227 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
228 }
229
230 if ( StringUtils.isBlank( strValue ) )
231 {
232 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
233 }
234
235 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_ATTRIBUTE_FIELD ) )
236 {
237 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
238 }
239 AttributeField currentAttributeField = _attributeFieldService.getAttributeField( nIdField );
240 int nPosition = currentAttributeField.getPosition( );
241
242 AttributeFieldr/attribute/AttributeField.html#AttributeField">AttributeField attributeField = new AttributeField( );
243 attributeField.setIdField( nIdField );
244 attributeField.setTitle( strTitle );
245 attributeField.setValue( strValue );
246 attributeField.setDefaultValue( strDefaultValue != null );
247 attributeField.setPosition( nPosition );
248 _attributeFieldService.updateAttributeField( attributeField );
249 }
250
251 return JSP_MODIFY_ATTRIBUTE + QUESTION_MARK + PARAMETER_ID_ATTRIBUTE + EQUAL + strIdAttribute;
252 }
253
254
255
256
257
258
259
260
261 public String doConfirmRemoveAttributeField( HttpServletRequest request )
262 {
263 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
264 String strIdField = request.getParameter( PARAMETER_ID_FIELD );
265
266 Map<String, String> parameters = new HashMap<>( );
267 parameters.put( PARAMETER_ID_ATTRIBUTE, strIdAttribute );
268 parameters.put( PARAMETER_ID_FIELD, strIdField );
269 parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_URL_REMOVE_ATTRIBUTE_FIELD ) );
270
271 return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_CONFIRM_REMOVE_ATTRIBUTE_FIELD, JSP_URL_REMOVE_ATTRIBUTE_FIELD,
272 AdminMessage.TYPE_CONFIRMATION, parameters );
273 }
274
275
276
277
278
279
280
281
282
283
284 public String doRemoveAttributeField( HttpServletRequest request ) throws AccessDeniedException
285 {
286 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
287 String strIdField = request.getParameter( PARAMETER_ID_FIELD );
288
289 if ( !SecurityTokenService.getInstance( ).validate( request, JSP_URL_REMOVE_ATTRIBUTE_FIELD ) )
290 {
291 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
292 }
293 if ( StringUtils.isNotBlank( strIdField ) && StringUtils.isNumeric( strIdField ) )
294 {
295 int nIdField = Integer.parseInt( strIdField );
296
297 _attributeFieldService.removeAttributeFieldFromIdField( nIdField );
298 AdminUserFieldService.doRemoveUserFieldsByIdField( nIdField );
299 }
300
301 return JSP_MODIFY_ATTRIBUTE + QUESTION_MARK + PARAMETER_ID_ATTRIBUTE + EQUAL + strIdAttribute;
302 }
303
304
305
306
307
308
309
310
311
312
313 public String doMoveUpAttributeField( HttpServletRequest request ) throws AccessDeniedException
314 {
315 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
316 String strIdField = request.getParameter( PARAMETER_ID_FIELD );
317
318 if ( StringUtils.isNotBlank( strIdField ) && StringUtils.isNumeric( strIdField ) && StringUtils.isNotBlank( strIdAttribute )
319 && StringUtils.isNumeric( strIdAttribute ) )
320 {
321 int nIdAttribute = Integer.parseInt( strIdAttribute );
322 int nIdField = Integer.parseInt( strIdField );
323
324 IAttribute attribute = _attributeService.getAttributeWithFields( nIdAttribute, getLocale( ) );
325 List<AttributeField> listAttributeFields = attribute.getListAttributeFields( );
326
327 if ( !SecurityTokenService.getInstance( ).validate( request, attribute.getTemplateModifyAttribute( ) ) )
328 {
329 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
330 }
331 if ( CollectionUtils.isNotEmpty( listAttributeFields ) )
332 {
333 AttributeField previousField = null;
334 AttributeField currentField = null;
335
336 Iterator<AttributeField> it = listAttributeFields.iterator( );
337 previousField = it.next( );
338 currentField = it.next( );
339
340 while ( it.hasNext( ) && ( currentField.getIdField( ) != nIdField ) )
341 {
342 previousField = currentField;
343 currentField = it.next( );
344 }
345
346 int previousFieldPosition = previousField.getPosition( );
347 int currentFieldPosition = currentField.getPosition( );
348 previousField.setPosition( currentFieldPosition );
349 currentField.setPosition( previousFieldPosition );
350 _attributeFieldService.updateAttributeField( previousField );
351 _attributeFieldService.updateAttributeField( currentField );
352 }
353 }
354
355 return JSP_MODIFY_ATTRIBUTE + "?" + PARAMETER_ID_ATTRIBUTE + "=" + strIdAttribute;
356 }
357
358
359
360
361
362
363
364
365
366
367 public String doMoveDownAttributeField( HttpServletRequest request ) throws AccessDeniedException
368 {
369 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
370 String strIdField = request.getParameter( PARAMETER_ID_FIELD );
371
372 if ( StringUtils.isNotBlank( strIdField ) && StringUtils.isNumeric( strIdField ) && StringUtils.isNotBlank( strIdAttribute )
373 && StringUtils.isNumeric( strIdAttribute ) )
374 {
375 int nIdAttribute = Integer.parseInt( strIdAttribute );
376 int nIdField = Integer.parseInt( strIdField );
377
378 IAttribute attribute = _attributeService.getAttributeWithFields( nIdAttribute, getLocale( ) );
379 List<AttributeField> listAttributeFields = attribute.getListAttributeFields( );
380 if ( !SecurityTokenService.getInstance( ).validate( request, attribute.getTemplateModifyAttribute( ) ) )
381 {
382 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
383 }
384 if ( CollectionUtils.isNotEmpty( listAttributeFields ) )
385 {
386 AttributeField currentField = null;
387 AttributeField nextField = null;
388
389 Iterator<AttributeField> it = listAttributeFields.iterator( );
390 currentField = it.next( );
391 nextField = it.next( );
392
393 while ( it.hasNext( ) && ( currentField.getIdField( ) != nIdField ) )
394 {
395 currentField = nextField;
396 nextField = it.next( );
397 }
398
399 int nextFieldPosition = nextField.getPosition( );
400 int currentFieldPosition = currentField.getPosition( );
401 nextField.setPosition( currentFieldPosition );
402 currentField.setPosition( nextFieldPosition );
403
404 _attributeFieldService.updateAttributeField( nextField );
405 _attributeFieldService.updateAttributeField( currentField );
406 }
407 }
408
409 return JSP_MODIFY_ATTRIBUTE + "?" + PARAMETER_ID_ATTRIBUTE + "=" + strIdAttribute;
410 }
411 }