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.AttributeType;
37 import fr.paris.lutece.portal.business.user.attribute.IAttribute;
38 import fr.paris.lutece.portal.service.message.AdminMessage;
39 import fr.paris.lutece.portal.service.message.AdminMessageService;
40 import fr.paris.lutece.portal.service.template.AppTemplateService;
41 import fr.paris.lutece.portal.service.user.attribute.AttributeService;
42 import fr.paris.lutece.portal.service.user.attribute.AttributeTypeService;
43 import fr.paris.lutece.portal.service.util.AppLogService;
44 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
45 import fr.paris.lutece.util.html.HtmlTemplate;
46
47 import org.apache.commons.lang.StringUtils;
48
49 import java.util.HashMap;
50 import java.util.Iterator;
51 import java.util.List;
52 import java.util.Map;
53
54 import javax.servlet.http.HttpServletRequest;
55
56
57
58
59
60
61
62 public class AttributeJspBean extends AdminFeaturesPageJspBean
63 {
64
65
66
67 private static final long serialVersionUID = 183073111112521149L;
68
69
70 private static final String QUESTION_MARK = "?";
71 private static final String EQUAL = "=";
72
73
74 private static final String PARAMETER_ATTRIBUTE_TYPE_CLASS_NAME = "attribute_type_class_name";
75 private static final String PARAMETER_CANCEL = "cancel";
76 private static final String PARAMETER_APPLY = "apply";
77 private static final String PARAMETER_ID_ATTRIBUTE = "id_attribute";
78
79
80 private static final String MARK_ATTRIBUTE_TYPES_LIST = "attribute_types_list";
81 private static final String MARK_ATTRIBUTE_TYPE = "attribute_type";
82 private static final String MARK_ATTRIBUTES_LIST = "attributes_list";
83 private static final String MARK_ATTRIBUTE = "attribute";
84 private static final String MARK_ATTRIBUTE_FIELDS_LIST = "attribute_fields_list";
85
86
87 private static final String PROPERTY_MANAGE_ATTRIBUTES_PAGETITLE = "portal.users.manage_attributes.pageTitle";
88 private static final String PROPERTY_MESSAGE_CONFIRM_REMOVE_ATTRIBUTE = "portal.users.manage_attributes.message.confirmRemoveAttribute";
89
90
91 private static final String TEMPLATE_MANAGE_ATTRIBUTES = "admin/user/attribute/manage_attributes.html";
92
93
94 private static final String JSP_URL_REMOVE_ATTRIBUTE = "jsp/admin/user/attribute/DoRemoveAttribute.jsp";
95 private static final String JSP_MANAGE_ATTRIBUTES = "ManageAttributes.jsp";
96 private static final String JSP_MODIFY_ATTRIBUTE = "ModifyAttribute.jsp";
97 private static final AttributeService _attributeService = AttributeService.getInstance( );
98 private static final AttributeTypeService _attributeTypeService = AttributeTypeService.getInstance( );
99
100
101
102
103
104
105 public String getManageAttributes( HttpServletRequest request )
106 {
107 setPageTitleProperty( PROPERTY_MANAGE_ATTRIBUTES_PAGETITLE );
108
109 List<IAttribute> listAttributes = _attributeService.getAllAttributesWithoutFields( getLocale( ) );
110
111
112 List<AttributeType> listAttributeTypes = _attributeTypeService.getAttributeTypes( getLocale( ) );
113
114 HtmlTemplate template;
115 Map<String, Object> model = new HashMap<String, Object>( );
116 model.put( MARK_ATTRIBUTES_LIST, listAttributes );
117 model.put( MARK_ATTRIBUTE_TYPES_LIST, listAttributeTypes );
118
119 template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_ATTRIBUTES, getLocale( ), model );
120
121 return getAdminPage( template.getHtml( ) );
122 }
123
124
125
126
127
128
129 public String getCreateAttribute( HttpServletRequest request )
130 {
131 String strAttributeTypeClassName = request.getParameter( PARAMETER_ATTRIBUTE_TYPE_CLASS_NAME );
132
133 IAttribute attribute = null;
134
135 try
136 {
137 attribute = (IAttribute) Class.forName( strAttributeTypeClassName ).newInstance( );
138 }
139 catch ( ClassNotFoundException e )
140 {
141
142 AppLogService.error( e );
143 }
144 catch ( InstantiationException e )
145 {
146
147
148 AppLogService.error( e );
149 }
150 catch ( IllegalAccessException e )
151 {
152
153 AppLogService.error( e );
154 }
155
156 if ( attribute == null )
157 {
158 return getManageAttributes( request );
159 }
160
161 setPageTitleProperty( attribute.getPropertyCreatePageTitle( ) );
162
163 attribute.setAttributeType( getLocale( ) );
164
165 HtmlTemplate template;
166 Map<String, Object> model = new HashMap<String, Object>( );
167 model.put( MARK_ATTRIBUTE_TYPE, attribute.getAttributeType( ) );
168
169 template = AppTemplateService.getTemplate( attribute.getTemplateCreateAttribute( ), getLocale( ), model );
170
171 return getAdminPage( template.getHtml( ) );
172 }
173
174
175
176
177
178
179 public String doCreateAttribute( HttpServletRequest request )
180 {
181 String strAttributeTypeClassName = request.getParameter( PARAMETER_ATTRIBUTE_TYPE_CLASS_NAME );
182 String strActionCancel = request.getParameter( PARAMETER_CANCEL );
183 String strActionApply = request.getParameter( PARAMETER_APPLY );
184
185 if ( StringUtils.isEmpty( strActionCancel ) )
186 {
187 IAttribute attribute = null;
188
189 try
190 {
191 attribute = (IAttribute) Class.forName( strAttributeTypeClassName ).newInstance( );
192 }
193 catch ( ClassNotFoundException e )
194 {
195
196 AppLogService.error( e );
197 }
198 catch ( InstantiationException e )
199 {
200
201
202 AppLogService.error( e );
203 }
204 catch ( IllegalAccessException e )
205 {
206
207 AppLogService.error( e );
208 }
209
210 if ( attribute == null )
211 {
212 return getManageAttributes( request );
213 }
214
215 String strError = attribute.setAttributeData( request );
216
217 if ( StringUtils.isNotBlank( strError ) )
218 {
219 return strError;
220 }
221
222 _attributeService.createAttribute( attribute );
223
224 if ( strActionApply != null )
225 {
226 return JSP_MODIFY_ATTRIBUTE + QUESTION_MARK + PARAMETER_ID_ATTRIBUTE + EQUAL +
227 attribute.getIdAttribute( );
228 }
229 }
230
231 return JSP_MANAGE_ATTRIBUTES;
232 }
233
234
235
236
237
238
239 public String getModifyAttribute( HttpServletRequest request )
240 {
241 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
242
243 if ( StringUtils.isNotBlank( strIdAttribute ) && StringUtils.isNumeric( strIdAttribute ) )
244 {
245
246 int nIdAttribute = Integer.parseInt( strIdAttribute );
247
248 IAttribute attribute = _attributeService.getAttributeWithFields( nIdAttribute, getLocale( ) );
249
250 setPageTitleProperty( attribute.getPropertyModifyPageTitle( ) );
251
252 HtmlTemplate template;
253 Map<String, Object> model = new HashMap<String, Object>( );
254 model.put( MARK_ATTRIBUTE, attribute );
255 model.put( MARK_ATTRIBUTE_FIELDS_LIST, attribute.getListAttributeFields( ) );
256
257 template = AppTemplateService.getTemplate( attribute.getTemplateModifyAttribute( ), getLocale( ), model );
258
259 return getAdminPage( template.getHtml( ) );
260 }
261
262
263 return getManageAttributes( request );
264 }
265
266
267
268
269
270
271 public String doModifyAttribute( HttpServletRequest request )
272 {
273 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
274 int nIdAttribute = Integer.parseInt( strIdAttribute );
275 String strActionCancel = request.getParameter( PARAMETER_CANCEL );
276 String strActionApply = request.getParameter( PARAMETER_APPLY );
277
278 if ( StringUtils.isEmpty( strActionCancel ) )
279 {
280 IAttribute attribute = _attributeService.getAttributeWithFields( nIdAttribute, getLocale( ) );
281
282 if ( attribute != null )
283 {
284 String strError = attribute.setAttributeData( request );
285
286 if ( strError != null )
287 {
288 return strError;
289 }
290
291 _attributeService.updateAttribute( attribute );
292
293 if ( strActionApply != null )
294 {
295 return JSP_MODIFY_ATTRIBUTE + QUESTION_MARK + PARAMETER_ID_ATTRIBUTE + EQUAL +
296 attribute.getIdAttribute( );
297 }
298 }
299 }
300
301 return JSP_MANAGE_ATTRIBUTES;
302 }
303
304
305
306
307
308
309 public String doConfirmRemoveAttribute( HttpServletRequest request )
310 {
311 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
312 String strUrlRemove = JSP_URL_REMOVE_ATTRIBUTE + QUESTION_MARK + PARAMETER_ID_ATTRIBUTE + EQUAL +
313 strIdAttribute;
314
315 String strUrl = AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_CONFIRM_REMOVE_ATTRIBUTE,
316 strUrlRemove, AdminMessage.TYPE_CONFIRMATION );
317
318 return strUrl;
319 }
320
321
322
323
324
325
326 public String doRemoveAttribute( HttpServletRequest request )
327 {
328 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
329
330 if ( StringUtils.isNotBlank( strIdAttribute ) && StringUtils.isNumeric( strIdAttribute ) )
331 {
332 int nIdAttribute = Integer.parseInt( strIdAttribute );
333 _attributeService.removeAttribute( nIdAttribute );
334 }
335
336 return JSP_MANAGE_ATTRIBUTES;
337 }
338
339
340
341
342
343
344 public String doMoveUpAttribute( HttpServletRequest request )
345 {
346 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
347
348 if ( StringUtils.isNotBlank( strIdAttribute ) && StringUtils.isNumeric( strIdAttribute ) )
349 {
350 int nIdAttribute = Integer.parseInt( strIdAttribute );
351
352 List<IAttribute> listAttributes = _attributeService.getAllAttributesWithoutFields( getLocale( ) );
353 IAttribute previousAttribute = null;
354 IAttribute currentAttribute = null;
355
356 Iterator<IAttribute> it = listAttributes.iterator( );
357 previousAttribute = it.next( );
358 currentAttribute = it.next( );
359
360 while ( it.hasNext( ) && ( currentAttribute.getIdAttribute( ) != nIdAttribute ) )
361 {
362 previousAttribute = currentAttribute;
363 currentAttribute = it.next( );
364 }
365
366 int previousAttributePosition = previousAttribute.getPosition( );
367 int currentAttributePosition = currentAttribute.getPosition( );
368 previousAttribute.setPosition( currentAttributePosition );
369 currentAttribute.setPosition( previousAttributePosition );
370
371 _attributeService.updateAttribute( previousAttribute );
372 _attributeService.updateAttribute( currentAttribute );
373 }
374
375 return JSP_MANAGE_ATTRIBUTES;
376 }
377
378
379
380
381
382
383 public String doMoveDownAttribute( HttpServletRequest request )
384 {
385 String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
386
387 if ( StringUtils.isNotBlank( strIdAttribute ) && StringUtils.isNumeric( strIdAttribute ) )
388 {
389 int nIdAttribute = Integer.parseInt( strIdAttribute );
390
391 List<IAttribute> listAttributes = _attributeService.getAllAttributesWithoutFields( getLocale( ) );
392 IAttribute nextAttribute = null;
393 IAttribute currentAttribute = null;
394
395 Iterator<IAttribute> it = listAttributes.iterator( );
396 currentAttribute = it.next( );
397 nextAttribute = it.next( );
398
399 while ( it.hasNext( ) && ( currentAttribute.getIdAttribute( ) != nIdAttribute ) )
400 {
401 currentAttribute = nextAttribute;
402 nextAttribute = it.next( );
403 }
404
405 int nextAttributePosition = nextAttribute.getPosition( );
406 int currentAttributePosition = currentAttribute.getPosition( );
407 nextAttribute.setPosition( currentAttributePosition );
408 currentAttribute.setPosition( nextAttributePosition );
409
410 _attributeService.updateAttribute( nextAttribute );
411 _attributeService.updateAttribute( currentAttribute );
412 }
413
414 return JSP_MANAGE_ATTRIBUTES;
415 }
416 }