View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.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   * AttributeFieldJspBean
60   */
61  public class AttributeFieldJspBean extends AdminFeaturesPageJspBean
62  {
63      /**
64       * Generated serial version UID
65       */
66      private static final long serialVersionUID = 3304151197655135630L;
67  
68      // CONSTANTS
69      private static final String QUESTION_MARK = "?";
70      private static final String EQUAL = "=";
71      private static final String AMPERSAND = "&";
72  
73      // PROPERTIES
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      // TEMPLATES
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      // PARAMETERS
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      // MARKS
91      private static final String MARK_ATTRIBUTE_FIELD = "attribute_field";
92      private static final String MARK_ATTRIBUTE = "attribute";
93  
94      // JSP
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      * Create attribute field
102      * @param request HttpServletRequest
103      * @return the html form
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      * @param request the HttpServletRequest
126      * @return Url
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      * Modify an attribute field
166      * @param request HttpServletRequest
167      * @return the html form
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      * Modify an attribute field
194      * @param request HttpServletRequest
195      * @return The Jsp URL of the process result
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      * Confirm the removal of the attribute field
232      * @param request HttpServletRequest
233      * @return the html form
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      * Remove the attribute field
250      * @param request HttpServletRequest
251      * @return The Jsp URL of the process result
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      * Move up the position of the attribute field
271      * @param request HttpServletRequest
272      * @return The Jsp URL of the process result
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      * Move down the position of the attribute field
317      * @param request HttpServletRequest
318      * @return The Jsp URL of the process result
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 }