AttributeFieldJspBean.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 fr.paris.lutece.portal.business.user.attribute.AttributeField;
  36. import fr.paris.lutece.portal.business.user.attribute.IAttribute;
  37. import fr.paris.lutece.portal.service.admin.AccessDeniedException;
  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.security.SecurityTokenService;
  41. import fr.paris.lutece.portal.service.template.AppTemplateService;
  42. import fr.paris.lutece.portal.service.user.attribute.AdminUserFieldService;
  43. import fr.paris.lutece.portal.service.user.attribute.AttributeFieldService;
  44. import fr.paris.lutece.portal.service.user.attribute.AttributeService;
  45. import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
  46. import fr.paris.lutece.portal.web.constants.Messages;
  47. import fr.paris.lutece.util.html.HtmlTemplate;

  48. import org.apache.commons.collections.CollectionUtils;
  49. import org.apache.commons.lang3.StringUtils;

  50. import java.util.HashMap;
  51. import java.util.Iterator;
  52. import java.util.List;
  53. import java.util.Map;

  54. import javax.servlet.http.HttpServletRequest;

  55. /**
  56.  * AttributeFieldJspBean
  57.  */
  58. public class AttributeFieldJspBean extends AdminFeaturesPageJspBean
  59. {
  60.     /**
  61.      * Generated serial version UID
  62.      */
  63.     private static final long serialVersionUID = 3304151197655135630L;

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

  67.     // PROPERTIES
  68.     private static final String PROPERTY_CREATE_ATTRIBUTE_FIELDS_PAGETITLE = "portal.users.create_attribute_field.pageTitle";
  69.     private static final String PROPERTY_MODIFY_ATTRIBUTE_FIELDS_PAGETITLE = "portal.users.modify_attribute_field.pageTitle";
  70.     private static final String PROPERTY_MESSAGE_CONFIRM_REMOVE_ATTRIBUTE_FIELD = "portal.users.modify_attribute.message.removeAttributeField";

  71.     // TEMPLATES
  72.     private static final String TEMPLATE_CREATE_ATTRIBUTE_FIELD = "admin/user/attribute/create_attribute_field.html";
  73.     private static final String TEMPLATE_MODIFY_ATTRIBUTE_FIELD = "admin/user/attribute/modify_attribute_field.html";

  74.     // PARAMETERS
  75.     private static final String PARAMETER_CANCEL = "cancel";
  76.     private static final String PARAMETER_ID_ATTRIBUTE = "id_attribute";
  77.     private static final String PARAMETER_TITLE = "title";
  78.     private static final String PARAMETER_VALUE = "value";
  79.     private static final String PARAMETER_DEFAULT_VALUE = "default_value";
  80.     private static final String PARAMETER_ID_FIELD = "id_field";

  81.     // MARKS
  82.     private static final String MARK_ATTRIBUTE_FIELD = "attribute_field";
  83.     private static final String MARK_ATTRIBUTE = "attribute";

  84.     // JSP
  85.     private static final String JSP_MODIFY_ATTRIBUTE = "ModifyAttribute.jsp";
  86.     private static final String JSP_URL_REMOVE_ATTRIBUTE_FIELD = "jsp/admin/user/attribute/DoRemoveAttributeField.jsp";
  87.     private static final AttributeService _attributeService = AttributeService.getInstance( );
  88.     private static final AttributeFieldService _attributeFieldService = AttributeFieldService.getInstance( );

  89.     /**
  90.      * Create attribute field
  91.      *
  92.      * @param request
  93.      *            HttpServletRequest
  94.      * @return the html form
  95.      */
  96.     public String getCreateAttributeField( HttpServletRequest request )
  97.     {
  98.         setPageTitleProperty( PROPERTY_CREATE_ATTRIBUTE_FIELDS_PAGETITLE );

  99.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
  100.         int nIdAttribute = Integer.parseInt( strIdAttribute );

  101.         IAttribute attribute = _attributeService.getAttributeWithoutFields( nIdAttribute, getLocale( ) );

  102.         HtmlTemplate template;
  103.         Map<String, Object> model = new HashMap<>( );
  104.         model.put( MARK_ATTRIBUTE, attribute );
  105.         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_ATTRIBUTE_FIELD ) );

  106.         template = AppTemplateService.getTemplate( TEMPLATE_CREATE_ATTRIBUTE_FIELD, getLocale( ), model );

  107.         return getAdminPage( template.getHtml( ) );
  108.     }

  109.     /**
  110.      *
  111.      * @param request
  112.      *            the HttpServletRequest
  113.      * @return Url
  114.      * @throws AccessDeniedException
  115.      *             if the security token is invalid
  116.      */
  117.     public String doCreateAttributeField( HttpServletRequest request ) throws AccessDeniedException
  118.     {
  119.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
  120.         int nIdAttribute = Integer.parseInt( strIdAttribute );
  121.         String strTitle = request.getParameter( PARAMETER_TITLE );
  122.         String strValue = request.getParameter( PARAMETER_VALUE );
  123.         String strDefaultValue = request.getParameter( PARAMETER_DEFAULT_VALUE );
  124.         String strCancel = request.getParameter( PARAMETER_CANCEL );

  125.         if ( StringUtils.isEmpty( strCancel ) )
  126.         {
  127.             if ( StringUtils.isBlank( strTitle ) )
  128.             {
  129.                 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  130.             }

  131.             if ( StringUtils.isBlank( strValue ) )
  132.             {
  133.                 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  134.             }

  135.             if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_ATTRIBUTE_FIELD ) )
  136.             {
  137.                 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  138.             }
  139.             AttributeField attributeField = new AttributeField( );
  140.             attributeField.setTitle( strTitle );
  141.             attributeField.setValue( strValue );
  142.             attributeField.setDefaultValue( strDefaultValue != null );

  143.             IAttribute attribute = _attributeService.getAttributeWithoutFields( nIdAttribute, getLocale( ) );
  144.             attributeField.setAttribute( attribute );
  145.             _attributeFieldService.createAttributeField( attributeField );
  146.         }

  147.         return JSP_MODIFY_ATTRIBUTE + QUESTION_MARK + PARAMETER_ID_ATTRIBUTE + EQUAL + nIdAttribute;
  148.     }

  149.     /**
  150.      * Modify an attribute field
  151.      *
  152.      * @param request
  153.      *            HttpServletRequest
  154.      * @return the html form
  155.      */
  156.     public String getModifyAttributeField( HttpServletRequest request )
  157.     {
  158.         setPageTitleProperty( PROPERTY_MODIFY_ATTRIBUTE_FIELDS_PAGETITLE );

  159.         String strIdField = request.getParameter( PARAMETER_ID_FIELD );
  160.         int nIdField = Integer.parseInt( strIdField );
  161.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
  162.         int nIdAttribute = Integer.parseInt( strIdAttribute );

  163.         IAttribute attribute = _attributeService.getAttributeWithoutFields( nIdAttribute, getLocale( ) );

  164.         AttributeField attributeField = _attributeFieldService.getAttributeField( nIdField );

  165.         HtmlTemplate template;
  166.         Map<String, Object> model = new HashMap<>( );
  167.         model.put( MARK_ATTRIBUTE_FIELD, attributeField );
  168.         model.put( MARK_ATTRIBUTE, attribute );
  169.         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_ATTRIBUTE_FIELD ) );

  170.         template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_ATTRIBUTE_FIELD, getLocale( ), model );

  171.         return getAdminPage( template.getHtml( ) );
  172.     }

  173.     /**
  174.      * Modify an attribute field
  175.      *
  176.      * @param request
  177.      *            HttpServletRequest
  178.      * @return The Jsp URL of the process result
  179.      * @throws AccessDeniedException
  180.      *             if the security token is invalid
  181.      */
  182.     public String doModifyAttributeField( HttpServletRequest request ) throws AccessDeniedException
  183.     {
  184.         String strTitle = request.getParameter( PARAMETER_TITLE );
  185.         String strValue = request.getParameter( PARAMETER_VALUE );
  186.         String strDefaultValue = request.getParameter( PARAMETER_DEFAULT_VALUE );
  187.         String strIdField = request.getParameter( PARAMETER_ID_FIELD );
  188.         int nIdField = Integer.parseInt( strIdField );
  189.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
  190.         String strCancel = request.getParameter( PARAMETER_CANCEL );

  191.         if ( StringUtils.isEmpty( strCancel ) )
  192.         {
  193.             if ( StringUtils.isBlank( strTitle ) )
  194.             {
  195.                 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  196.             }

  197.             if ( StringUtils.isBlank( strValue ) )
  198.             {
  199.                 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  200.             }

  201.             if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_ATTRIBUTE_FIELD ) )
  202.             {
  203.                 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  204.             }
  205.             AttributeField currentAttributeField = _attributeFieldService.getAttributeField( nIdField );
  206.             int nPosition = currentAttributeField.getPosition( );

  207.             AttributeField attributeField = new AttributeField( );
  208.             attributeField.setIdField( nIdField );
  209.             attributeField.setTitle( strTitle );
  210.             attributeField.setValue( strValue );
  211.             attributeField.setDefaultValue( strDefaultValue != null );
  212.             attributeField.setPosition( nPosition );
  213.             _attributeFieldService.updateAttributeField( attributeField );
  214.         }

  215.         return JSP_MODIFY_ATTRIBUTE + QUESTION_MARK + PARAMETER_ID_ATTRIBUTE + EQUAL + strIdAttribute;
  216.     }

  217.     /**
  218.      * Confirm the removal of the attribute field
  219.      *
  220.      * @param request
  221.      *            HttpServletRequest
  222.      * @return the html form
  223.      */
  224.     public String doConfirmRemoveAttributeField( HttpServletRequest request )
  225.     {
  226.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
  227.         String strIdField = request.getParameter( PARAMETER_ID_FIELD );

  228.         Map<String, String> parameters = new HashMap<>( );
  229.         parameters.put( PARAMETER_ID_ATTRIBUTE, strIdAttribute );
  230.         parameters.put( PARAMETER_ID_FIELD, strIdField );
  231.         parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_URL_REMOVE_ATTRIBUTE_FIELD ) );

  232.         return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_CONFIRM_REMOVE_ATTRIBUTE_FIELD, JSP_URL_REMOVE_ATTRIBUTE_FIELD,
  233.                 AdminMessage.TYPE_CONFIRMATION, parameters );
  234.     }

  235.     /**
  236.      * Remove the attribute field
  237.      *
  238.      * @param request
  239.      *            HttpServletRequest
  240.      * @return The Jsp URL of the process result
  241.      * @throws AccessDeniedException
  242.      *             if the security token is invalid
  243.      */
  244.     public String doRemoveAttributeField( HttpServletRequest request ) throws AccessDeniedException
  245.     {
  246.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
  247.         String strIdField = request.getParameter( PARAMETER_ID_FIELD );

  248.         if ( !SecurityTokenService.getInstance( ).validate( request, JSP_URL_REMOVE_ATTRIBUTE_FIELD ) )
  249.         {
  250.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  251.         }
  252.         if ( StringUtils.isNotBlank( strIdField ) && StringUtils.isNumeric( strIdField ) )
  253.         {
  254.             int nIdField = Integer.parseInt( strIdField );

  255.             _attributeFieldService.removeAttributeFieldFromIdField( nIdField );
  256.             AdminUserFieldService.doRemoveUserFieldsByIdField( nIdField );
  257.         }

  258.         return JSP_MODIFY_ATTRIBUTE + QUESTION_MARK + PARAMETER_ID_ATTRIBUTE + EQUAL + strIdAttribute;
  259.     }

  260.     /**
  261.      * Move up the position of the attribute field
  262.      *
  263.      * @param request
  264.      *            HttpServletRequest
  265.      * @return The Jsp URL of the process result
  266.      * @throws AccessDeniedException
  267.      *             if the security token is invalid
  268.      */
  269.     public String doMoveUpAttributeField( HttpServletRequest request ) throws AccessDeniedException
  270.     {
  271.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
  272.         String strIdField = request.getParameter( PARAMETER_ID_FIELD );

  273.         if ( StringUtils.isNotBlank( strIdField ) && StringUtils.isNumeric( strIdField ) && StringUtils.isNotBlank( strIdAttribute )
  274.                 && StringUtils.isNumeric( strIdAttribute ) )
  275.         {
  276.             int nIdAttribute = Integer.parseInt( strIdAttribute );
  277.             int nIdField = Integer.parseInt( strIdField );

  278.             IAttribute attribute = _attributeService.getAttributeWithFields( nIdAttribute, getLocale( ) );
  279.             List<AttributeField> listAttributeFields = attribute.getListAttributeFields( );

  280.             if ( !SecurityTokenService.getInstance( ).validate( request, attribute.getTemplateModifyAttribute( ) ) )
  281.             {
  282.                 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  283.             }
  284.             if ( CollectionUtils.isNotEmpty( listAttributeFields ) )
  285.             {
  286.                 AttributeField previousField = null;
  287.                 AttributeField currentField = null;

  288.                 Iterator<AttributeField> it = listAttributeFields.iterator( );
  289.                 previousField = it.next( );
  290.                 currentField = it.next( );

  291.                 while ( it.hasNext( ) && ( currentField.getIdField( ) != nIdField ) )
  292.                 {
  293.                     previousField = currentField;
  294.                     currentField = it.next( );
  295.                 }

  296.                 int previousFieldPosition = previousField.getPosition( );
  297.                 int currentFieldPosition = currentField.getPosition( );
  298.                 previousField.setPosition( currentFieldPosition );
  299.                 currentField.setPosition( previousFieldPosition );
  300.                 _attributeFieldService.updateAttributeField( previousField );
  301.                 _attributeFieldService.updateAttributeField( currentField );
  302.             }
  303.         }

  304.         return JSP_MODIFY_ATTRIBUTE + "?" + PARAMETER_ID_ATTRIBUTE + "=" + strIdAttribute;
  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 doMoveDownAttributeField( HttpServletRequest request ) throws AccessDeniedException
  316.     {
  317.         String strIdAttribute = request.getParameter( PARAMETER_ID_ATTRIBUTE );
  318.         String strIdField = request.getParameter( PARAMETER_ID_FIELD );

  319.         if ( StringUtils.isNotBlank( strIdField ) && StringUtils.isNumeric( strIdField ) && StringUtils.isNotBlank( strIdAttribute )
  320.                 && StringUtils.isNumeric( strIdAttribute ) )
  321.         {
  322.             int nIdAttribute = Integer.parseInt( strIdAttribute );
  323.             int nIdField = Integer.parseInt( strIdField );

  324.             IAttribute attribute = _attributeService.getAttributeWithFields( nIdAttribute, getLocale( ) );
  325.             List<AttributeField> listAttributeFields = attribute.getListAttributeFields( );
  326.             if ( !SecurityTokenService.getInstance( ).validate( request, attribute.getTemplateModifyAttribute( ) ) )
  327.             {
  328.                 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  329.             }
  330.             if ( CollectionUtils.isNotEmpty( listAttributeFields ) )
  331.             {
  332.                 AttributeField currentField = null;
  333.                 AttributeField nextField = null;

  334.                 Iterator<AttributeField> it = listAttributeFields.iterator( );
  335.                 currentField = it.next( );
  336.                 nextField = it.next( );

  337.                 while ( it.hasNext( ) && ( currentField.getIdField( ) != nIdField ) )
  338.                 {
  339.                     currentField = nextField;
  340.                     nextField = it.next( );
  341.                 }

  342.                 int nextFieldPosition = nextField.getPosition( );
  343.                 int currentFieldPosition = currentField.getPosition( );
  344.                 nextField.setPosition( currentFieldPosition );
  345.                 currentField.setPosition( nextFieldPosition );

  346.                 _attributeFieldService.updateAttributeField( nextField );
  347.                 _attributeFieldService.updateAttributeField( currentField );
  348.             }
  349.         }

  350.         return JSP_MODIFY_ATTRIBUTE + "?" + PARAMETER_ID_ATTRIBUTE + "=" + strIdAttribute;
  351.     }
  352. }