AttributeText.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.AttributeService;
  39. import fr.paris.lutece.portal.web.constants.Messages;

  40. import org.apache.commons.collections.CollectionUtils;
  41. import org.apache.commons.lang3.StringUtils;

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

  45. import javax.servlet.http.HttpServletRequest;

  46. /**
  47.  *
  48.  * AttributeText
  49.  *
  50.  */
  51. public class AttributeText extends AbstractAttribute implements ISimpleValuesAttributes
  52. {
  53.     // CONSTANTS
  54.     private static final String EMPTY_STRING = "";
  55.     private static final String CONSTANT_UNDERSCORE = "_";

  56.     // PARAMETERS
  57.     private static final String PARAMETER_TITLE = "title";
  58.     private static final String PARAMETER_HELP_MESSAGE = "help_message";
  59.     private static final String PARAMETER_MANDATORY = "mandatory";
  60.     private static final String PARAMETER_WIDTH = "width";
  61.     private static final String PARAMETER_MAX_SIZE_ENTER = "max_size_enter";
  62.     private static final String PARAMETER_VALUE = "value";
  63.     private static final String PARAMETER_IS_SHOWN_IN_SEARCH = "is_shown_in_search";
  64.     private static final String PARAMETER_IS_SHOWN_IN_RESULT_LIST = "is_shown_in_result_list";
  65.     private static final String PARAMETER_ATTRIBUTE = "attribute";

  66.     // PROPERTY
  67.     private static final String PROPERTY_TYPE_TEXT = "portal.users.attribute.type.text";
  68.     private static final String PROPERTY_CREATE_TEXT_PAGETITLE = "portal.users.create_attribute.pageTitleAttributeText";
  69.     private static final String PROPERTY_MODIFY_TEXT_PAGETITLE = "portal.users.modify_attribute.pageTitleAttributeText";
  70.     private static final String PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS = "portal.users.message.noArithmeticalCharacters";

  71.     // TEMPLATES
  72.     private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/user/attribute/text/create_attribute_text.html";
  73.     private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/user/attribute/text/modify_attribute_text.html";
  74.     private static final String TEMPLATE_HTML_FORM_ATTRIBUTE = "admin/user/attribute/text/html_code_form_attribute_text.html";
  75.     private static final String TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE = "admin/user/attribute/text/html_code_form_search_attribute_text.html";
  76.     private static final String TEMPLATE_HTML_VALUE = "admin/user/attribute/text/html_code_value_attribute_text.html";
  77.     private static final String REGEX_ID = "-?[0-9]+";

  78.     /**
  79.      * Constructor
  80.      */
  81.     public AttributeText( )
  82.     {
  83.         // Ctor
  84.     }

  85.     /**
  86.      * Get the template create an attribute
  87.      *
  88.      * @return The URL of the template
  89.      */
  90.     @Override
  91.     public String getTemplateCreateAttribute( )
  92.     {
  93.         return TEMPLATE_CREATE_ATTRIBUTE;
  94.     }

  95.     /**
  96.      * Get the template modify an attribute
  97.      *
  98.      * @return The URL of the template
  99.      */
  100.     @Override
  101.     public String getTemplateModifyAttribute( )
  102.     {
  103.         return TEMPLATE_MODIFY_ATTRIBUTE;
  104.     }

  105.     /**
  106.      * Get the template html form attribute
  107.      *
  108.      * @return the template
  109.      */
  110.     @Override
  111.     public String getTemplateHtmlFormAttribute( )
  112.     {
  113.         return TEMPLATE_HTML_FORM_ATTRIBUTE;
  114.     }

  115.     /**
  116.      * Get the template html form search attribute
  117.      *
  118.      * @return the template
  119.      */
  120.     @Override
  121.     public String getTemplateHtmlFormSearchAttribute( )
  122.     {
  123.         return TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE;
  124.     }

  125.     /**
  126.      * Get the template html for the value of the attribute
  127.      *
  128.      * @return the template
  129.      */
  130.     @Override
  131.     public String getTemplateHtmlValue( )
  132.     {
  133.         return TEMPLATE_HTML_VALUE;
  134.     }

  135.     /**
  136.      * Get the page title for create page
  137.      *
  138.      * @return page title
  139.      */
  140.     @Override
  141.     public String getPropertyCreatePageTitle( )
  142.     {
  143.         return PROPERTY_CREATE_TEXT_PAGETITLE;
  144.     }

  145.     /**
  146.      * Get the page title for modify page
  147.      *
  148.      * @return page title
  149.      */
  150.     @Override
  151.     public String getPropertyModifyPageTitle( )
  152.     {
  153.         return PROPERTY_MODIFY_TEXT_PAGETITLE;
  154.     }

  155.     /**
  156.      * Set the data of the attribute
  157.      *
  158.      * @param request
  159.      *            HttpServletRequest
  160.      * @return null if there are no errors
  161.      */
  162.     @Override
  163.     public String setAttributeData( HttpServletRequest request )
  164.     {
  165.         String strTitle = request.getParameter( PARAMETER_TITLE );
  166.         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
  167.         String strIsShownInSearch = request.getParameter( PARAMETER_IS_SHOWN_IN_SEARCH );
  168.         String strIsShownInResultList = request.getParameter( PARAMETER_IS_SHOWN_IN_RESULT_LIST );
  169.         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
  170.         String strWidth = request.getParameter( PARAMETER_WIDTH );
  171.         String strMaxSizeEnter = request.getParameter( PARAMETER_MAX_SIZE_ENTER );
  172.         String strValue = request.getParameter( PARAMETER_VALUE );

  173.         if ( StringUtils.isBlank( strTitle ) || StringUtils.isBlank( strWidth ) )
  174.         {
  175.             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  176.         }

  177.         if ( !strWidth.matches( REGEX_ID ) )
  178.         {
  179.             return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS, AdminMessage.TYPE_STOP );
  180.         }

  181.         int nWidth = Integer.parseInt( strWidth );

  182.         int nMaxSizeEnter;

  183.         if ( StringUtils.isNotBlank( strMaxSizeEnter ) && !strMaxSizeEnter.matches( REGEX_ID ) )
  184.         {
  185.             return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS, AdminMessage.TYPE_STOP );
  186.         }
  187.         else
  188.             if ( ( strMaxSizeEnter == null ) || strMaxSizeEnter.equals( EMPTY_STRING ) )
  189.             {
  190.                 nMaxSizeEnter = -1;
  191.             }
  192.             else
  193.             {
  194.                 nMaxSizeEnter = Integer.parseInt( strMaxSizeEnter );
  195.             }

  196.         setTitle( strTitle );
  197.         setHelpMessage( strHelpMessage );
  198.         setMandatory( strMandatory != null );
  199.         setShownInSearch( strIsShownInSearch != null );
  200.         setShownInResultList( strIsShownInResultList != null );

  201.         if ( getListAttributeFields( ) == null )
  202.         {
  203.             List<AttributeField> listAttributeFields = new ArrayList<>( );
  204.             AttributeField attributeField = new AttributeField( );
  205.             listAttributeFields.add( attributeField );
  206.             setListAttributeFields( listAttributeFields );
  207.         }

  208.         getListAttributeFields( ).get( 0 ).setValue( strValue );
  209.         getListAttributeFields( ).get( 0 ).setWidth( nWidth );
  210.         getListAttributeFields( ).get( 0 ).setMaxSizeEnter( nMaxSizeEnter );

  211.         return null;
  212.     }

  213.     /**
  214.      * Set attribute type
  215.      *
  216.      * @param locale
  217.      *            locale
  218.      */
  219.     @Override
  220.     public void setAttributeType( Locale locale )
  221.     {
  222.         AttributeType attributeType = new AttributeType( );
  223.         attributeType.setLocale( locale );
  224.         attributeType.setClassName( this.getClass( ).getName( ) );
  225.         attributeType.setLabelType( PROPERTY_TYPE_TEXT );
  226.         setAttributeType( attributeType );
  227.     }

  228.     /**
  229.      * Get the data of the user fields
  230.      *
  231.      * @param request
  232.      *            HttpServletRequest
  233.      * @param user
  234.      *            user
  235.      * @return user field data
  236.      */
  237.     @Override
  238.     public List<AdminUserField> getUserFieldsData( HttpServletRequest request, AdminUser user )
  239.     {
  240.         String [ ] strValues = request.getParameterValues( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + getIdAttribute( ) );

  241.         return getUserFieldsData( strValues, user );
  242.     }

  243.     /**
  244.      * Get the data of the user fields
  245.      *
  246.      * @param strValues
  247.      *            Values
  248.      * @param user
  249.      *            user
  250.      * @return user field data
  251.      */
  252.     @Override
  253.     public List<AdminUserField> getUserFieldsData( String [ ] strValues, AdminUser user )
  254.     {
  255.         List<AdminUserField> listUserFields = new ArrayList<>( );
  256.         AdminUserField userField = new AdminUserField( );
  257.         AttributeService.getInstance( ).setAttributeField( this );

  258.         if ( strValues != null )
  259.         {
  260.             for ( String strValue : strValues )
  261.             {
  262.                 if ( strValue == null )
  263.                 {
  264.                     strValue = EMPTY_STRING;
  265.                 }

  266.                 userField.setUser( user );
  267.                 userField.setAttribute( this );

  268.                 if ( CollectionUtils.isNotEmpty( getListAttributeFields( ) ) )
  269.                 {
  270.                     userField.setAttributeField( getListAttributeFields( ).get( 0 ) );
  271.                 }

  272.                 userField.setValue( strValue );

  273.                 listUserFields.add( userField );
  274.             }
  275.         }

  276.         return listUserFields;
  277.     }

  278.     /**
  279.      * Get whether the attribute is anonymizable.
  280.      *
  281.      * @return True if the attribute can be anonymized, false otherwise.
  282.      */
  283.     @Override
  284.     public boolean isAnonymizable( )
  285.     {
  286.         return true;
  287.     }
  288. }