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.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   * AttributeJspBean
60   *
61   */
62  public class AttributeJspBean extends AdminFeaturesPageJspBean
63  {
64      /**
65       * Generated serial version UID
66       */
67      private static final long serialVersionUID = 183073111112521149L;
68  
69      // CONSTANTS
70      private static final String QUESTION_MARK = "?";
71      private static final String EQUAL = "=";
72  
73      // PARAMETERS
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      // MARKS
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      // PROPERTIES
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      // TEMPLATES
91      private static final String TEMPLATE_MANAGE_ATTRIBUTES = "admin/user/attribute/manage_attributes.html";
92  
93      // JSP
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      * Get list of user attributes
102      * @param request HttpServletRequest
103      * @return list of attributes
104      */
105     public String getManageAttributes( HttpServletRequest request )
106     {
107         setPageTitleProperty( PROPERTY_MANAGE_ATTRIBUTES_PAGETITLE );
108 
109         List<IAttribute> listAttributes = _attributeService.getAllAttributesWithoutFields( getLocale(  ) );
110 
111         // ATTRIBUTE TYPES
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      * Get user attribute creation interface
126      * @param request HttpServletRequest
127      * @return the Html form
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             // class doesn't exist
142             AppLogService.error( e );
143         }
144         catch ( InstantiationException e )
145         {
146             // Class is abstract or is an interface or haven't accessible
147             // builder
148             AppLogService.error( e );
149         }
150         catch ( IllegalAccessException e )
151         {
152             // can't access to the class
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      * Create an user attribute
176      * @param request HttpServletRequest
177      * @return The Jsp URL of the process result
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                 // class doesn't exist
196                 AppLogService.error( e );
197             }
198             catch ( InstantiationException e )
199             {
200                 // Class is abstract or is an interface or haven't accessible
201                 // builder
202                 AppLogService.error( e );
203             }
204             catch ( IllegalAccessException e )
205             {
206                 // can't access to the class
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      * Get the user attribute modification interface
236      * @param request HttpServletRequest
237      * @return the html form
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             // Check if the ID attribute is correct
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         // Otherwise, we redirect the user to the attribute management interface
263         return getManageAttributes( request );
264     }
265 
266     /**
267      * Modify the attribute
268      * @param request HttpServletRequest
269      * @return The Jsp URL of the process result
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      * Get the confirmation to remove an user attribute
306      * @param request HttpServletRequest
307      * @return The Jsp URL of the confirmation window
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      * Remove an user attribute
323      * @param request HttpServletRequest
324      * @return The Jsp URL of the process result
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      * Move up the position of the attribute field
341      * @param request HttpServletRequest
342      * @return The Jsp URL of the process result
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      * Move down the position of the attribute field
380      * @param request HttpServletRequest
381      * @return The Jsp URL of the process result
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 }