AttributeJspBean.java

  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. import java.util.HashMap;
  36. import java.util.Iterator;
  37. import java.util.List;
  38. import java.util.Map;

  39. import javax.servlet.http.HttpServletRequest;

  40. import org.apache.commons.lang3.StringUtils;

  41. import fr.paris.lutece.portal.business.user.attribute.IAttribute;
  42. import fr.paris.lutece.portal.service.admin.AccessDeniedException;
  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.security.SecurityTokenService;
  46. import fr.paris.lutece.portal.service.template.AppTemplateService;
  47. import fr.paris.lutece.portal.service.user.attribute.AttributeService;
  48. import fr.paris.lutece.portal.service.util.AppLogService;
  49. import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
  50. import fr.paris.lutece.portal.web.dashboard.AdminDashboardJspBean;
  51. import fr.paris.lutece.util.html.HtmlTemplate;

  52. /**
  53.  *
  54.  * AttributeJspBean
  55.  *
  56.  */
  57. public class AttributeJspBean extends AdminFeaturesPageJspBean
  58. {
  59.     /**
  60.      * Generated serial version UID
  61.      */
  62.     private static final long serialVersionUID = 183073111112521149L;

  63.     // CONSTANTS
  64.     private static final String QUESTION_MARK = "?";
  65.     private static final String EQUAL = "=";

  66.     // PARAMETERS
  67.     private static final String PARAMETER_ATTRIBUTE_TYPE_CLASS_NAME = "attribute_type_class_name";
  68.     private static final String PARAMETER_CANCEL = "cancel";
  69.     private static final String PARAMETER_APPLY = "apply";
  70.     private static final String PARAMETER_ID_ATTRIBUTE = "id_attribute";

  71.     // MARKS
  72.     private static final String MARK_ATTRIBUTE_TYPE = "attribute_type";
  73.     private static final String MARK_ATTRIBUTE = "attribute";
  74.     private static final String MARK_ATTRIBUTE_FIELDS_LIST = "attribute_fields_list";

  75.     // PROPERTIES
  76.     private static final String PROPERTY_MESSAGE_CONFIRM_REMOVE_ATTRIBUTE = "portal.users.manage_attributes.message.confirmRemoveAttribute";

  77.     // JSP
  78.     private static final String JSP_URL_REMOVE_ATTRIBUTE = "jsp/admin/user/attribute/DoRemoveAttribute.jsp";
  79.     private static final String ANCHOR_ADMIN_DASHBOARDS = "attributes_management";
  80.     private static final String JSP_MODIFY_ATTRIBUTE = "ModifyAttribute.jsp";
  81.     private static final AttributeService _attributeService = AttributeService.getInstance( );

  82.     /**
  83.      * Get user attribute creation interface
  84.      *
  85.      * @param request
  86.      *            HttpServletRequest
  87.      * @return the Html form
  88.      */
  89.     public String getCreateAttribute( HttpServletRequest request )
  90.     {
  91.         String strAttributeTypeClassName = request.getParameter( PARAMETER_ATTRIBUTE_TYPE_CLASS_NAME );

  92.         IAttribute attribute = null;

  93.         try
  94.         {
  95.             attribute = (IAttribute) Class.forName( strAttributeTypeClassName ).newInstance( );
  96.         }
  97.         catch( IllegalAccessException | InstantiationException | ClassNotFoundException e )
  98.         {
  99.             AppLogService.error( e.getMessage( ), e );
  100.         }

  101.         if ( attribute == null )
  102.         {
  103.             return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
  104.         }

  105.         setPageTitleProperty( attribute.getPropertyCreatePageTitle( ) );

  106.         attribute.setAttributeType( getLocale( ) );

  107.         HtmlTemplate template;
  108.         Map<String, Object> model = new HashMap<>( );
  109.         model.put( MARK_ATTRIBUTE_TYPE, attribute.getAttributeType( ) );
  110.         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, attribute.getTemplateCreateAttribute( ) ) );

  111.         template = AppTemplateService.getTemplate( attribute.getTemplateCreateAttribute( ), getLocale( ), model );

  112.         return getAdminPage( template.getHtml( ) );
  113.     }

  114.     /**
  115.      * Create an user attribute
  116.      *
  117.      * @param request
  118.      *            HttpServletRequest
  119.      * @return The Jsp URL of the process result
  120.      * @throws AccessDeniedException
  121.      *             if the security token is invalid
  122.      */
  123.     public String doCreateAttribute( HttpServletRequest request ) throws AccessDeniedException
  124.     {
  125.         String strAttributeTypeClassName = request.getParameter( PARAMETER_ATTRIBUTE_TYPE_CLASS_NAME );
  126.         String strActionCancel = request.getParameter( PARAMETER_CANCEL );
  127.         String strActionApply = request.getParameter( PARAMETER_APPLY );

  128.         if ( StringUtils.isEmpty( strActionCancel ) )
  129.         {
  130.             IAttribute attribute = null;

  131.             try
  132.             {
  133.                 attribute = (IAttribute) Class.forName( strAttributeTypeClassName ).newInstance( );
  134.             }
  135.             catch( IllegalAccessException | InstantiationException | ClassNotFoundException e )
  136.             {
  137.                 AppLogService.error( e.getMessage( ), e );
  138.             }

  139.             if ( attribute == null )
  140.             {
  141.                 getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
  142.             }
  143.             else
  144.             {
  145.                 String strError = attribute.setAttributeData( request );

  146.                 if ( StringUtils.isNotBlank( strError ) )
  147.                 {
  148.                     return strError;
  149.                 }
  150.                 if ( !SecurityTokenService.getInstance( ).validate( request, attribute.getTemplateCreateAttribute( ) ) )
  151.                 {
  152.                     throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  153.                 }
  154.                 _attributeService.createAttribute( attribute );

  155.                 if ( strActionApply != null )
  156.                 {
  157.                     return JSP_MODIFY_ATTRIBUTE + QUESTION_MARK + PARAMETER_ID_ATTRIBUTE + EQUAL + attribute.getIdAttribute( );
  158.                 }
  159.             }
  160.         }

  161.         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
  162.     }

  163.     /**
  164.      * Get the user attribute modification interface
  165.      *
  166.      * @param request
  167.      *            HttpServletRequest
  168.      * @return the html form
  169.      */
  170.     public String getModifyAttribute( HttpServletRequest request )
  171.     {
  172.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );

  173.         if ( StringUtils.isNotBlank( strIdAttribute ) && StringUtils.isNumeric( strIdAttribute ) )
  174.         {
  175.             // Check if the ID attribute is correct
  176.             int nIdAttribute = Integer.parseInt( strIdAttribute );

  177.             IAttribute attribute = _attributeService.getAttributeWithFields( nIdAttribute, getLocale( ) );

  178.             setPageTitleProperty( attribute.getPropertyModifyPageTitle( ) );

  179.             HtmlTemplate template;
  180.             Map<String, Object> model = new HashMap<>( );
  181.             model.put( MARK_ATTRIBUTE, attribute );
  182.             model.put( MARK_ATTRIBUTE_FIELDS_LIST, attribute.getListAttributeFields( ) );
  183.             model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, attribute.getTemplateModifyAttribute( ) ) );

  184.             template = AppTemplateService.getTemplate( attribute.getTemplateModifyAttribute( ), getLocale( ), model );

  185.             return getAdminPage( template.getHtml( ) );
  186.         }

  187.         // Otherwise, we redirect the user to the attribute management interface
  188.         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
  189.     }

  190.     /**
  191.      * Modify the attribute
  192.      *
  193.      * @param request
  194.      *            HttpServletRequest
  195.      * @return The Jsp URL of the process result
  196.      * @throws AccessDeniedException
  197.      *             if the security token is invalid
  198.      */
  199.     public String doModifyAttribute( HttpServletRequest request ) throws AccessDeniedException
  200.     {
  201.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
  202.         int nIdAttribute = Integer.parseInt( strIdAttribute );
  203.         String strActionCancel = request.getParameter( PARAMETER_CANCEL );
  204.         String strActionApply = request.getParameter( PARAMETER_APPLY );

  205.         if ( StringUtils.isEmpty( strActionCancel ) )
  206.         {
  207.             IAttribute attribute = _attributeService.getAttributeWithFields( nIdAttribute, getLocale( ) );

  208.             if ( attribute != null )
  209.             {
  210.                 String strError = attribute.setAttributeData( request );

  211.                 if ( strError != null )
  212.                 {
  213.                     return strError;
  214.                 }
  215.                 if ( !SecurityTokenService.getInstance( ).validate( request, attribute.getTemplateModifyAttribute( ) ) )
  216.                 {
  217.                     throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  218.                 }

  219.                 _attributeService.updateAttribute( attribute );

  220.                 if ( strActionApply != null )
  221.                 {
  222.                     return JSP_MODIFY_ATTRIBUTE + QUESTION_MARK + PARAMETER_ID_ATTRIBUTE + EQUAL + attribute.getIdAttribute( );
  223.                 }
  224.             }
  225.         }

  226.         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
  227.     }

  228.     /**
  229.      * Get the confirmation to remove an user attribute
  230.      *
  231.      * @param request
  232.      *            HttpServletRequest
  233.      * @return The Jsp URL of the confirmation window
  234.      */
  235.     public String doConfirmRemoveAttribute( HttpServletRequest request )
  236.     {
  237.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );

  238.         Map<String, String> parameters = new HashMap<>( );
  239.         parameters.put( PARAMETER_ID_ATTRIBUTE, strIdAttribute );
  240.         parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_URL_REMOVE_ATTRIBUTE ) );

  241.         return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_CONFIRM_REMOVE_ATTRIBUTE, JSP_URL_REMOVE_ATTRIBUTE, AdminMessage.TYPE_CONFIRMATION,
  242.                 parameters );
  243.     }

  244.     /**
  245.      * Remove an user attribute
  246.      *
  247.      * @param request
  248.      *            HttpServletRequest
  249.      * @return The Jsp URL of the process result
  250.      * @throws AccessDeniedException
  251.      *             if the security token is invalid
  252.      */
  253.     public String doRemoveAttribute( HttpServletRequest request ) throws AccessDeniedException
  254.     {
  255.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );

  256.         if ( StringUtils.isNotBlank( strIdAttribute ) && StringUtils.isNumeric( strIdAttribute ) )
  257.         {
  258.             if ( !SecurityTokenService.getInstance( ).validate( request, JSP_URL_REMOVE_ATTRIBUTE ) )
  259.             {
  260.                 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  261.             }
  262.             int nIdAttribute = Integer.parseInt( strIdAttribute );
  263.             _attributeService.removeAttribute( nIdAttribute );
  264.         }

  265.         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
  266.     }

  267.     /**
  268.      * Move up the position of the attribute field
  269.      *
  270.      * @param request
  271.      *            HttpServletRequest
  272.      * @return The Jsp URL of the process result
  273.      * @throws AccessDeniedException
  274.      *             if the security token is invalid
  275.      */
  276.     public String doMoveUpAttribute( HttpServletRequest request ) throws AccessDeniedException
  277.     {
  278.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );

  279.         if ( StringUtils.isNotBlank( strIdAttribute ) && StringUtils.isNumeric( strIdAttribute ) )
  280.         {
  281.             if ( !SecurityTokenService.getInstance( ).validate( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) )
  282.             {
  283.                 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  284.             }
  285.             int nIdAttribute = Integer.parseInt( strIdAttribute );

  286.             List<IAttribute> listAttributes = _attributeService.getAllAttributesWithoutFields( getLocale( ) );
  287.             IAttribute previousAttribute;
  288.             IAttribute currentAttribute;

  289.             Iterator<IAttribute> it = listAttributes.iterator( );
  290.             previousAttribute = it.next( );
  291.             currentAttribute = it.next( );

  292.             while ( it.hasNext( ) && ( currentAttribute.getIdAttribute( ) != nIdAttribute ) )
  293.             {
  294.                 previousAttribute = currentAttribute;
  295.                 currentAttribute = it.next( );
  296.             }

  297.             int previousAttributePosition = previousAttribute.getPosition( );
  298.             int currentAttributePosition = currentAttribute.getPosition( );
  299.             previousAttribute.setPosition( currentAttributePosition );
  300.             currentAttribute.setPosition( previousAttributePosition );

  301.             _attributeService.updateAttribute( previousAttribute );
  302.             _attributeService.updateAttribute( currentAttribute );
  303.         }

  304.         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
  305.     }

  306.     /**
  307.      * Move down the position of the attribute field
  308.      *
  309.      * @param request
  310.      *            HttpServletRequest
  311.      * @return The Jsp URL of the process result
  312.      * @throws AccessDeniedException
  313.      *             if the security token is invalid
  314.      */
  315.     public String doMoveDownAttribute( HttpServletRequest request ) throws AccessDeniedException
  316.     {
  317.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );

  318.         if ( StringUtils.isNotBlank( strIdAttribute ) && StringUtils.isNumeric( strIdAttribute ) )
  319.         {
  320.             if ( !SecurityTokenService.getInstance( ).validate( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) )
  321.             {
  322.                 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  323.             }
  324.             int nIdAttribute = Integer.parseInt( strIdAttribute );

  325.             List<IAttribute> listAttributes = _attributeService.getAllAttributesWithoutFields( getLocale( ) );
  326.             IAttribute nextAttribute = null;
  327.             IAttribute currentAttribute = null;

  328.             Iterator<IAttribute> it = listAttributes.iterator( );
  329.             currentAttribute = it.next( );
  330.             nextAttribute = it.next( );

  331.             while ( it.hasNext( ) && ( currentAttribute.getIdAttribute( ) != nIdAttribute ) )
  332.             {
  333.                 currentAttribute = nextAttribute;
  334.                 nextAttribute = it.next( );
  335.             }

  336.             int nextAttributePosition = nextAttribute.getPosition( );
  337.             int currentAttributePosition = currentAttribute.getPosition( );
  338.             nextAttribute.setPosition( currentAttributePosition );
  339.             currentAttribute.setPosition( nextAttributePosition );

  340.             _attributeService.updateAttribute( nextAttribute );
  341.             _attributeService.updateAttribute( currentAttribute );
  342.         }

  343.         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
  344.     }
  345. }