View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.plugins.mylutece.web.attribute;
35  
36  import fr.paris.lutece.plugins.mylutece.business.attribute.AttributeField;
37  import fr.paris.lutece.plugins.mylutece.business.attribute.AttributeFieldHome;
38  import fr.paris.lutece.plugins.mylutece.business.attribute.AttributeHome;
39  import fr.paris.lutece.plugins.mylutece.business.attribute.AttributeType;
40  import fr.paris.lutece.plugins.mylutece.business.attribute.IAttribute;
41  import fr.paris.lutece.plugins.mylutece.business.attribute.MyLuteceUserFieldHome;
42  import fr.paris.lutece.plugins.mylutece.service.MyLutecePlugin;
43  import fr.paris.lutece.portal.service.message.AdminMessage;
44  import fr.paris.lutece.portal.service.message.AdminMessageService;
45  import fr.paris.lutece.portal.service.plugin.Plugin;
46  import fr.paris.lutece.portal.service.plugin.PluginService;
47  import fr.paris.lutece.portal.service.spring.SpringContextService;
48  import fr.paris.lutece.portal.service.template.AppTemplateService;
49  import fr.paris.lutece.portal.service.util.AppLogService;
50  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
51  import fr.paris.lutece.util.html.HtmlTemplate;
52  
53  import java.util.ArrayList;
54  import java.util.HashMap;
55  import java.util.Iterator;
56  import java.util.List;
57  import java.util.Map;
58  
59  import javax.servlet.http.HttpServletRequest;
60  
61  /**
62   *
63   * AttributeJspBean
64   *
65   */
66  public class AttributeJspBean extends AdminFeaturesPageJspBean
67  {
68      public static final String RIGHT_MANAGE_MYLUTECE = "MYLUTECE_MANAGEMENT";
69  
70      // PARAMETERS
71      private static final String PARAMETER_ATTRIBUTE_TYPE_CLASS_NAME = "attribute_type_class_name";
72      private static final String PARAMETER_CANCEL = "cancel";
73      private static final String PARAMETER_APPLY = "apply";
74      private static final String PARAMETER_ID_ATTRIBUTE = "id_attribute";
75  
76      // MARKS
77      private static final String MARK_ATTRIBUTE_TYPES_LIST = "attribute_types_list";
78      private static final String MARK_ATTRIBUTE_TYPE = "attribute_type";
79      private static final String MARK_ATTRIBUTES_LIST = "attributes_list";
80      private static final String MARK_ATTRIBUTE = "attribute";
81      private static final String MARK_ATTRIBUTE_FIELDS_LIST = "attribute_fields_list";
82  
83      // PROPERTIES
84      private static final String PROPERTY_MANAGE_ATTRIBUTES_PAGETITLE = "mylutece.manage_attributes.pageTitle";
85      private static final String PROPERTY_MESSAGE_CONFIRM_REMOVE_ATTRIBUTE = "mylutece.manage_attributes.message.confirmRemoveAttribute";
86  
87      // TEMPLATES
88      private static final String TEMPLATE_MANAGE_ATTRIBUTES = "admin/plugins/mylutece/attribute/manage_attributes.html";
89  
90      // JSP
91      private static final String JSP_URL_REMOVE_ATTRIBUTE = "jsp/admin/plugins/mylutece/attribute/DoRemoveAttribute.jsp";
92      private static final String JSP_MANAGE_ATTRIBUTES = "ManageAttributes.jsp";
93      private static final String JSP_MODIFY_ATTRIBUTE = "ModifyAttribute.jsp";
94  
95      /**
96       * Get list of user attributes
97       * 
98       * @param request
99       *            HttpServletRequest
100      * @return list of attributes
101      */
102     public String getManageAttributes( HttpServletRequest request )
103     {
104         setPageTitleProperty( PROPERTY_MANAGE_ATTRIBUTES_PAGETITLE );
105 
106         Plugin plugin = PluginService.getPlugin( MyLutecePlugin.PLUGIN_NAME );
107         List<IAttribute> listAttributes = AttributeHome.findAll( getLocale( ), plugin );
108 
109         // ATTRIBUTE TYPES
110         List<AttributeType> listAttributeTypes = new ArrayList<AttributeType>( );
111 
112         for ( IAttribute attribute : SpringContextService.getBeansOfType( IAttribute.class ) )
113         {
114             attribute.setAttributeType( getLocale( ) );
115             listAttributeTypes.add( attribute.getAttributeType( ) );
116         }
117 
118         HtmlTemplate template;
119         Map<String, Object> model = new HashMap<String, Object>( );
120         model.put( MARK_ATTRIBUTES_LIST, listAttributes );
121         model.put( MARK_ATTRIBUTE_TYPES_LIST, listAttributeTypes );
122 
123         template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_ATTRIBUTES, getLocale( ), model );
124 
125         return getAdminPage( template.getHtml( ) );
126     }
127 
128     /**
129      * Get user attribute creation interface
130      * 
131      * @param request
132      *            HttpServletRequest
133      * @return the Html form
134      */
135     public String getCreateAttribute( HttpServletRequest request )
136     {
137         String strAttributeTypeClassName = request.getParameter( PARAMETER_ATTRIBUTE_TYPE_CLASS_NAME );
138 
139         IAttribute attribute = null;
140 
141         try
142         {
143             attribute = (IAttribute) Class.forName( strAttributeTypeClassName ).newInstance( );
144         }
145         catch( ClassNotFoundException e )
146         {
147             // class doesn't exist
148             AppLogService.error( e );
149         }
150         catch( InstantiationException e )
151         {
152             // Class is abstract or is an interface or haven't accessible
153             // builder
154             AppLogService.error( e );
155         }
156         catch( IllegalAccessException e )
157         {
158             // can't access to rhe class
159             AppLogService.error( e );
160         }
161 
162         setPageTitleProperty( attribute.getPropertyCreatePageTitle( ) );
163 
164         attribute.setAttributeType( getLocale( ) );
165 
166         HtmlTemplate template;
167         Map<String, Object> model = new HashMap<String, Object>( );
168         model.put( MARK_ATTRIBUTE_TYPE, attribute.getAttributeType( ) );
169 
170         template = AppTemplateService.getTemplate( attribute.getTemplateCreateAttribute( ), getLocale( ), model );
171 
172         return getAdminPage( template.getHtml( ) );
173     }
174 
175     /**
176      * Create an user attribute
177      * 
178      * @param request
179      *            HttpServletRequest
180      * @return The Jsp URL of the process result
181      */
182     public String doCreateAttribute( HttpServletRequest request )
183     {
184         String strAttributeTypeClassName = request.getParameter( PARAMETER_ATTRIBUTE_TYPE_CLASS_NAME );
185         String strActionCancel = request.getParameter( PARAMETER_CANCEL );
186         String strActionApply = request.getParameter( PARAMETER_APPLY );
187 
188         if ( strActionCancel == null )
189         {
190             IAttribute attribute = null;
191 
192             try
193             {
194                 attribute = (IAttribute) Class.forName( strAttributeTypeClassName ).newInstance( );
195             }
196             catch( ClassNotFoundException e )
197             {
198                 // class doesn't exist
199                 AppLogService.error( e );
200             }
201             catch( InstantiationException e )
202             {
203                 // Class is abstract or is an interface or haven't accessible
204                 // builder
205                 AppLogService.error( e );
206             }
207             catch( IllegalAccessException e )
208             {
209                 // can't access to the class
210                 AppLogService.error( e );
211             }
212 
213             String strError = attribute.setAttributeData( request );
214 
215             if ( strError != null )
216             {
217                 return strError;
218             }
219 
220             Plugin plugin = PluginService.getPlugin( MyLutecePlugin.PLUGIN_NAME );
221 
222             int nIdAttribute = AttributeHome.create( attribute, plugin );
223             attribute.setIdAttribute( nIdAttribute );
224 
225             if ( attribute.getListAttributeFields( ) != null )
226             {
227                 for ( AttributeField attributeField : attribute.getListAttributeFields( ) )
228                 {
229                     attributeField.setAttribute( attribute );
230                     AttributeFieldHome.create( attributeField, plugin );
231                 }
232             }
233 
234             if ( strActionApply != null )
235             {
236                 return JSP_MODIFY_ATTRIBUTE + "?" + PARAMETER_ID_ATTRIBUTE + "=" + attribute.getIdAttribute( );
237             }
238         }
239 
240         return JSP_MANAGE_ATTRIBUTES;
241     }
242 
243     /**
244      * Get the user attribute modification interface
245      * 
246      * @param request
247      *            HttpServletRequest
248      * @return the html form
249      */
250     public String getModifyAttribute( HttpServletRequest request )
251     {
252         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
253         int nIdAttribute = Integer.parseInt( strIdAttribute );
254         Plugin plugin = PluginService.getPlugin( MyLutecePlugin.PLUGIN_NAME );
255 
256         IAttribute attribute = AttributeHome.findByPrimaryKey( nIdAttribute, getLocale( ), plugin );
257 
258         setPageTitleProperty( attribute.getPropertyModifyPageTitle( ) );
259 
260         List<AttributeField> listAttributeFields = AttributeFieldHome.selectAttributeFieldsByIdAttribute( nIdAttribute, plugin );
261 
262         HtmlTemplate template;
263         Map<String, Object> model = new HashMap<String, Object>( );
264         model.put( MARK_ATTRIBUTE, attribute );
265         model.put( MARK_ATTRIBUTE_FIELDS_LIST, listAttributeFields );
266 
267         template = AppTemplateService.getTemplate( attribute.getTemplateModifyAttribute( ), getLocale( ), model );
268 
269         return getAdminPage( template.getHtml( ) );
270     }
271 
272     /**
273      * Modify the attribute
274      * 
275      * @param request
276      *            HttpServletRequest
277      * @return The Jsp URL of the process result
278      */
279     public String doModifyAttribute( HttpServletRequest request )
280     {
281         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
282         int nIdAttribute = Integer.parseInt( strIdAttribute );
283         String strActionCancel = request.getParameter( PARAMETER_CANCEL );
284         String strActionApply = request.getParameter( PARAMETER_APPLY );
285 
286         if ( strActionCancel == null )
287         {
288             Plugin plugin = PluginService.getPlugin( MyLutecePlugin.PLUGIN_NAME );
289             IAttribute attribute = AttributeHome.findByPrimaryKey( nIdAttribute, getLocale( ), plugin );
290             List<AttributeField> listAttributeFields = AttributeFieldHome.selectAttributeFieldsByIdAttribute( nIdAttribute, plugin );
291             attribute.setListAttributeFields( listAttributeFields );
292 
293             String strError = attribute.setAttributeData( request );
294 
295             if ( strError != null )
296             {
297                 return strError;
298             }
299 
300             AttributeHome.update( attribute, plugin );
301 
302             if ( attribute.getListAttributeFields( ) != null )
303             {
304                 for ( AttributeField attributeField : attribute.getListAttributeFields( ) )
305                 {
306                     attributeField.setAttribute( attribute );
307                     AttributeFieldHome.update( attributeField, plugin );
308                 }
309             }
310 
311             if ( strActionApply != null )
312             {
313                 return JSP_MODIFY_ATTRIBUTE + "?" + PARAMETER_ID_ATTRIBUTE + "=" + attribute.getIdAttribute( );
314             }
315         }
316 
317         return JSP_MANAGE_ATTRIBUTES;
318     }
319 
320     /**
321      * Get the confirmation to remove an user attribute
322      * 
323      * @param request
324      *            HttpServletRequest
325      * @return The Jsp URL of the confirmation window
326      */
327     public String doConfirmRemoveAttribute( HttpServletRequest request )
328     {
329         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
330         String strUrlRemove = JSP_URL_REMOVE_ATTRIBUTE + "?" + PARAMETER_ID_ATTRIBUTE + "=" + strIdAttribute;
331 
332         String strUrl = AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_CONFIRM_REMOVE_ATTRIBUTE, strUrlRemove, AdminMessage.TYPE_CONFIRMATION );
333 
334         return strUrl;
335     }
336 
337     /**
338      * Remove an user attribute
339      * 
340      * @param request
341      *            HttpServletRequest
342      * @return The Jsp URL of the process result
343      */
344     public String doRemoveAttribute( HttpServletRequest request )
345     {
346         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
347         int nIdAttribute = Integer.parseInt( strIdAttribute );
348         Plugin plugin = PluginService.getPlugin( MyLutecePlugin.PLUGIN_NAME );
349 
350         AttributeHome.remove( nIdAttribute, plugin );
351         AttributeFieldHome.removeAttributeFieldsFromIdAttribute( nIdAttribute, plugin );
352         MyLuteceUserFieldHome.removeUserFieldsFromIdAttribute( nIdAttribute, plugin );
353 
354         return JSP_MANAGE_ATTRIBUTES;
355     }
356 
357     /**
358      * Move up the position of the attribute field
359      * 
360      * @param request
361      *            HttpServletRequest
362      * @return The Jsp URL of the process result
363      */
364     public String doMoveUpAttribute( HttpServletRequest request )
365     {
366         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
367         int nIdAttribute = Integer.parseInt( strIdAttribute );
368         Plugin plugin = PluginService.getPlugin( MyLutecePlugin.PLUGIN_NAME );
369 
370         List<IAttribute> listAttributes = AttributeHome.findAll( getLocale( ), plugin );
371         IAttribute previousAttribute = null;
372         IAttribute currentAttribute = null;
373 
374         Iterator<IAttribute> it = listAttributes.iterator( );
375         previousAttribute = it.next( );
376         currentAttribute = it.next( );
377 
378         while ( it.hasNext( ) && ( currentAttribute.getIdAttribute( ) != nIdAttribute ) )
379         {
380             previousAttribute = currentAttribute;
381             currentAttribute = it.next( );
382         }
383 
384         int previousAttributePosition = previousAttribute.getPosition( );
385         int currentAttributePosition = currentAttribute.getPosition( );
386         previousAttribute.setPosition( currentAttributePosition );
387         currentAttribute.setPosition( previousAttributePosition );
388 
389         AttributeHome.update( previousAttribute, plugin );
390         AttributeHome.update( currentAttribute, plugin );
391 
392         return JSP_MANAGE_ATTRIBUTES;
393     }
394 
395     /**
396      * Move down the position of the attribute field
397      * 
398      * @param request
399      *            HttpServletRequest
400      * @return The Jsp URL of the process result
401      */
402     public String doMoveDownAttribute( HttpServletRequest request )
403     {
404         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
405         int nIdAttribute = Integer.parseInt( strIdAttribute );
406         Plugin plugin = PluginService.getPlugin( MyLutecePlugin.PLUGIN_NAME );
407 
408         List<IAttribute> listAttributes = AttributeHome.findAll( getLocale( ), plugin );
409         IAttribute nextAttribute = null;
410         IAttribute currentAttribute = null;
411 
412         Iterator<IAttribute> it = listAttributes.iterator( );
413         currentAttribute = it.next( );
414         nextAttribute = it.next( );
415 
416         while ( it.hasNext( ) && ( currentAttribute.getIdAttribute( ) != nIdAttribute ) )
417         {
418             currentAttribute = nextAttribute;
419             nextAttribute = it.next( );
420         }
421 
422         int nextAttributePosition = nextAttribute.getPosition( );
423         int currentAttributePosition = currentAttribute.getPosition( );
424         nextAttribute.setPosition( currentAttributePosition );
425         currentAttribute.setPosition( nextAttributePosition );
426 
427         AttributeHome.update( nextAttribute, plugin );
428         AttributeHome.update( currentAttribute, plugin );
429 
430         return JSP_MANAGE_ATTRIBUTES;
431     }
432 }