View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.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   * AttributeFieldJspBean
62   */
63  public class AttributeFieldJspBean extends AdminFeaturesPageJspBean
64  {
65      /**
66       * Generated serial version UID
67       */
68      private static final long serialVersionUID = 3304151197655135630L;
69  
70      // CONSTANTS
71      private static final String QUESTION_MARK = "?";
72      private static final String EQUAL = "=";
73  
74      // PROPERTIES
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      // TEMPLATES
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      // PARAMETERS
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      // MARKS
92      private static final String MARK_ATTRIBUTE_FIELD = "attribute_field";
93      private static final String MARK_ATTRIBUTE = "attribute";
94  
95      // JSP
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      * Create attribute field
103      * 
104      * @param request
105      *            HttpServletRequest
106      * @return the html form
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      * @param request
130      *            the HttpServletRequest
131      * @return Url
132      * @throws AccessDeniedException
133      *             if the security token is invalid
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      * Modify an attribute field
175      * 
176      * @param request
177      *            HttpServletRequest
178      * @return the html form
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      * Modify an attribute field
206      * 
207      * @param request
208      *            HttpServletRequest
209      * @return The Jsp URL of the process result
210      * @throws AccessDeniedException
211      *             if the security token is invalid
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      * Confirm the removal of the attribute field
256      * 
257      * @param request
258      *            HttpServletRequest
259      * @return the html form
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      * Remove the attribute field
277      * 
278      * @param request
279      *            HttpServletRequest
280      * @return The Jsp URL of the process result
281      * @throws AccessDeniedException
282      *             if the security token is invalid
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      * Move up the position of the attribute field
306      * 
307      * @param request
308      *            HttpServletRequest
309      * @return The Jsp URL of the process result
310      * @throws AccessDeniedException
311      *             if the security token is invalid
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      * Move down the position of the attribute field
360      * 
361      * @param request
362      *            HttpServletRequest
363      * @return The Jsp URL of the process result
364      * @throws AccessDeniedException
365      *             if the security token is invalid
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 }