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