AttributeCheckBox.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.business.user.attribute;

  35. import fr.paris.lutece.portal.business.user.AdminUser;
  36. import fr.paris.lutece.portal.service.message.AdminMessage;
  37. import fr.paris.lutece.portal.service.message.AdminMessageService;
  38. import fr.paris.lutece.portal.service.user.attribute.AttributeFieldService;
  39. import fr.paris.lutece.portal.web.constants.Messages;

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

  41. import java.util.ArrayList;
  42. import java.util.List;
  43. import java.util.Locale;

  44. import javax.servlet.http.HttpServletRequest;

  45. /**
  46.  *
  47.  * AttributeComboBox
  48.  *
  49.  */
  50. public class AttributeCheckBox extends AbstractAttribute implements ISimpleValuesAttributes
  51. {
  52.     // Constants
  53.     private static final String CONSTANT_UNDERSCORE = "_";

  54.     // Parameters
  55.     private static final String PARAMETER_TITLE = "title";
  56.     private static final String PARAMETER_HELP_MESSAGE = "help_message";
  57.     private static final String PARAMETER_MANDATORY = "mandatory";
  58.     private static final String PARAMETER_IS_SHOWN_IN_SEARCH = "is_shown_in_search";
  59.     private static final String PARAMETER_IS_SHOWN_IN_RESULT_LIST = "is_shown_in_result_list";
  60.     private static final String PARAMETER_ATTRIBUTE = "attribute";
  61.     private static final String PARAMETER_IS_FIELD_IN_LINE = "is_field_in_line";

  62.     // Properties
  63.     private static final String PROPERTY_TYPE_CHECKBOX = "portal.users.attribute.type.checkBox";
  64.     private static final String PROPERTY_CREATE_CHECKBOX_PAGETITLE = "portal.users.create_attribute.pageTitleAttributeCheckBox";
  65.     private static final String PROPERTY_MODIFY_CHECKBOX_PAGETITLE = "portal.users.modify_attribute.pageTitleAttributeCheckBox";

  66.     // Templates
  67.     private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/user/attribute/checkbox/create_attribute_checkbox.html";
  68.     private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/user/attribute/checkbox/modify_attribute_checkbox.html";
  69.     private static final String TEMPLATE_HTML_FORM_ATTRIBUTE = "admin/user/attribute/checkbox/html_code_form_attribute_checkbox.html";
  70.     private static final String TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE = "admin/user/attribute/checkbox/html_code_form_search_attribute_checkbox.html";
  71.     private static final String TEMPLATE_HTML_VALUE = "admin/user/attribute/checkbox/html_code_value_attribute_checkbox.html";

  72.     /**
  73.      * Constructor
  74.      */
  75.     public AttributeCheckBox( )
  76.     {
  77.         // Ctor
  78.     }

  79.     /**
  80.      * Get the template create an attribute
  81.      *
  82.      * @return The URL of the template
  83.      */
  84.     @Override
  85.     public String getTemplateCreateAttribute( )
  86.     {
  87.         return TEMPLATE_CREATE_ATTRIBUTE;
  88.     }

  89.     /**
  90.      * Get the template modify an attribute
  91.      *
  92.      * @return The URL of the template
  93.      */
  94.     @Override
  95.     public String getTemplateModifyAttribute( )
  96.     {
  97.         return TEMPLATE_MODIFY_ATTRIBUTE;
  98.     }

  99.     /**
  100.      * Get the template html form attribute
  101.      *
  102.      * @return the template
  103.      */
  104.     @Override
  105.     public String getTemplateHtmlFormAttribute( )
  106.     {
  107.         return TEMPLATE_HTML_FORM_ATTRIBUTE;
  108.     }

  109.     /**
  110.      * Get the template html form search attribute
  111.      *
  112.      * @return the template
  113.      */
  114.     @Override
  115.     public String getTemplateHtmlFormSearchAttribute( )
  116.     {
  117.         return TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE;
  118.     }

  119.     /**
  120.      * Get the template html for the value of the attribute
  121.      *
  122.      * @return the template
  123.      */
  124.     @Override
  125.     public String getTemplateHtmlValue( )
  126.     {
  127.         return TEMPLATE_HTML_VALUE;
  128.     }

  129.     /**
  130.      * Get page title for create page
  131.      *
  132.      * @return page title
  133.      */
  134.     @Override
  135.     public String getPropertyCreatePageTitle( )
  136.     {
  137.         return PROPERTY_CREATE_CHECKBOX_PAGETITLE;
  138.     }

  139.     /**
  140.      * Get page title for modify page
  141.      *
  142.      * @return page title
  143.      */
  144.     @Override
  145.     public String getPropertyModifyPageTitle( )
  146.     {
  147.         return PROPERTY_MODIFY_CHECKBOX_PAGETITLE;
  148.     }

  149.     /**
  150.      * Set the data of the attribute
  151.      *
  152.      * @param request
  153.      *            HttpServletRequest
  154.      * @return null if there are no errors
  155.      */
  156.     @Override
  157.     public String setAttributeData( HttpServletRequest request )
  158.     {
  159.         String strTitle = request.getParameter( PARAMETER_TITLE );
  160.         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
  161.         String strIsShownInSearch = request.getParameter( PARAMETER_IS_SHOWN_IN_SEARCH );
  162.         String strIsShownInResultList = request.getParameter( PARAMETER_IS_SHOWN_IN_RESULT_LIST );
  163.         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
  164.         String strFieldInLine = request.getParameter( PARAMETER_IS_FIELD_IN_LINE );

  165.         if ( StringUtils.isNotBlank( strTitle ) )
  166.         {
  167.             setTitle( strTitle );
  168.             setHelpMessage( strHelpMessage );
  169.             setMandatory( strMandatory != null );
  170.             setShownInSearch( strIsShownInSearch != null );
  171.             setShownInResultList( strIsShownInResultList != null );
  172.             setFieldInLine( strFieldInLine != null );

  173.             return null;
  174.         }

  175.         return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  176.     }

  177.     /**
  178.      * Set attribute type
  179.      *
  180.      * @param locale
  181.      *            locale
  182.      */
  183.     @Override
  184.     public void setAttributeType( Locale locale )
  185.     {
  186.         AttributeType attributeType = new AttributeType( );
  187.         attributeType.setLocale( locale );
  188.         attributeType.setClassName( this.getClass( ).getName( ) );
  189.         attributeType.setLabelType( PROPERTY_TYPE_CHECKBOX );
  190.         setAttributeType( attributeType );
  191.     }

  192.     /**
  193.      * Get the data of the user fields
  194.      *
  195.      * @param request
  196.      *            HttpServletRequest
  197.      * @param user
  198.      *            user
  199.      * @return user field data
  200.      */
  201.     @Override
  202.     public List<AdminUserField> getUserFieldsData( HttpServletRequest request, AdminUser user )
  203.     {
  204.         String [ ] strValues = request.getParameterValues( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + getIdAttribute( ) );

  205.         return getUserFieldsData( strValues, user );
  206.     }

  207.     /**
  208.      * Get the data of the user fields
  209.      *
  210.      * @param strValues
  211.      *            Values
  212.      * @param user
  213.      *            user
  214.      * @return user field data
  215.      */
  216.     @Override
  217.     public List<AdminUserField> getUserFieldsData( String [ ] strValues, AdminUser user )
  218.     {
  219.         List<AdminUserField> listUserFields = new ArrayList<>( );

  220.         if ( strValues != null )
  221.         {
  222.             for ( String strValue : strValues )
  223.             {
  224.                 AdminUserField userField = new AdminUserField( );
  225.                 AttributeField attributeField;

  226.                 if ( StringUtils.isNotBlank( strValue ) && StringUtils.isNumeric( strValue ) )
  227.                 {
  228.                     int nIdField = Integer.parseInt( strValue );
  229.                     attributeField = AttributeFieldService.getInstance( ).getAttributeField( nIdField );
  230.                 }
  231.                 else
  232.                 {
  233.                     attributeField = new AttributeField( );
  234.                     attributeField.setAttribute( this );
  235.                     attributeField.setTitle( strValue );
  236.                     attributeField.setValue( strValue );
  237.                 }

  238.                 userField.setUser( user );
  239.                 userField.setAttribute( this );
  240.                 userField.setAttributeField( attributeField );
  241.                 userField.setValue( attributeField.getTitle( ) );

  242.                 listUserFields.add( userField );
  243.             }
  244.         }
  245.         // If no checkbox is selected, the request will have no parameter at all
  246.         else
  247.         {
  248.             AdminUserField userField = new AdminUserField( );
  249.             AttributeField attributeField;
  250.             attributeField = new AttributeField( );
  251.             attributeField.setAttribute( this );
  252.             attributeField.setTitle( "" );
  253.             attributeField.setValue( "" );
  254.             userField.setUser( user );
  255.             userField.setAttribute( this );
  256.             userField.setAttributeField( attributeField );
  257.             userField.setValue( attributeField.getTitle( ) );

  258.             listUserFields.add( userField );
  259.         }

  260.         return listUserFields;
  261.     }

  262.     /**
  263.      * Get whether the attribute is anonymizable.
  264.      *
  265.      * @return True if the attribute can be anonymized, false otherwise.
  266.      */
  267.     @Override
  268.     public boolean isAnonymizable( )
  269.     {
  270.         return false;
  271.     }
  272. }