AbstractAttribute.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.service.plugin.Plugin;
  36. import fr.paris.lutece.portal.service.template.AppTemplateService;
  37. import fr.paris.lutece.util.html.HtmlTemplate;

  38. import java.util.ArrayList;
  39. import java.util.HashMap;
  40. import java.util.List;
  41. import java.util.Locale;
  42. import java.util.Map;

  43. /**
  44.  *
  45.  * Attribute
  46.  *
  47.  */
  48. public abstract class AbstractAttribute implements IAttribute
  49. {
  50.     // MARKS
  51.     private static final String MARK_ATTRIBUTE = "attribute";
  52.     private static final String MARK_DEFAULT_VALUES_LIST = "default_values_list";
  53.     private static final String MARK_USER_FIELD = "user_field";
  54.     private int _nIdAttribute;
  55.     private boolean _bMandatory;
  56.     private String _strTitle;
  57.     private String _strHelpMessage;
  58.     private int _nPosition;
  59.     private AttributeType _attributeType;
  60.     private List<AttributeField> _listAttributeFields;
  61.     private Plugin _plugin;
  62.     private boolean _bIsShownInSearch;
  63.     private boolean _bIsFieldInLine;
  64.     private boolean _bIsAttributeImage;
  65.     private boolean _bIsShownInResultList;
  66.     private boolean _bAnonymize;

  67.     /**
  68.      * Constructor
  69.      */
  70.     public AbstractAttribute( )
  71.     {
  72.     }

  73.     /**
  74.      * Get ID Attribute
  75.      *
  76.      * @return ID attribute
  77.      */
  78.     @Override
  79.     public int getIdAttribute( )
  80.     {
  81.         return _nIdAttribute;
  82.     }

  83.     /**
  84.      * Set ID Attribute
  85.      *
  86.      * @param nIdAttribute
  87.      *            ID Attribute
  88.      */
  89.     @Override
  90.     public void setIdAttribute( int nIdAttribute )
  91.     {
  92.         _nIdAttribute = nIdAttribute;
  93.     }

  94.     /**
  95.      * Get Mandatory
  96.      *
  97.      * @return true if it's mandatory, false otherwise
  98.      */
  99.     @Override
  100.     public boolean isMandatory( )
  101.     {
  102.         return _bMandatory;
  103.     }

  104.     /**
  105.      * Set mandatory
  106.      *
  107.      * @param bMandatory
  108.      *            true if it's mandatory, false otherwise
  109.      */
  110.     @Override
  111.     public void setMandatory( boolean bMandatory )
  112.     {
  113.         _bMandatory = bMandatory;
  114.     }

  115.     /**
  116.      * Get list fields
  117.      *
  118.      * @return list fields
  119.      */
  120.     @Override
  121.     public List<AttributeField> getListAttributeFields( )
  122.     {
  123.         return _listAttributeFields;
  124.     }

  125.     /**
  126.      * Set list fields
  127.      *
  128.      * @param listAttributeFields
  129.      *            list fields
  130.      */
  131.     @Override
  132.     public void setListAttributeFields( List<AttributeField> listAttributeFields )
  133.     {
  134.         _listAttributeFields = listAttributeFields;
  135.     }

  136.     /**
  137.      * Get title
  138.      *
  139.      * @return title
  140.      */
  141.     @Override
  142.     public String getTitle( )
  143.     {
  144.         return _strTitle;
  145.     }

  146.     /**
  147.      * Set title
  148.      *
  149.      * @param strTitle
  150.      *            title
  151.      */
  152.     @Override
  153.     public void setTitle( String strTitle )
  154.     {
  155.         _strTitle = strTitle;
  156.     }

  157.     /**
  158.      * Get help Message
  159.      *
  160.      * @return help message
  161.      */
  162.     @Override
  163.     public String getHelpMessage( )
  164.     {
  165.         return _strHelpMessage;
  166.     }

  167.     /**
  168.      * Set help message
  169.      *
  170.      * @param strHelpMessage
  171.      *            help message
  172.      */
  173.     @Override
  174.     public void setHelpMessage( String strHelpMessage )
  175.     {
  176.         _strHelpMessage = strHelpMessage;
  177.     }

  178.     /**
  179.      * Get position
  180.      *
  181.      * @return position
  182.      */
  183.     @Override
  184.     public int getPosition( )
  185.     {
  186.         return _nPosition;
  187.     }

  188.     /**
  189.      * Set position
  190.      *
  191.      * @param nPosition
  192.      *            position
  193.      */
  194.     @Override
  195.     public void setPosition( int nPosition )
  196.     {
  197.         _nPosition = nPosition;
  198.     }

  199.     /**
  200.      * Get attribute type
  201.      *
  202.      * @return attribute type
  203.      */
  204.     @Override
  205.     public AttributeType getAttributeType( )
  206.     {
  207.         return _attributeType;
  208.     }

  209.     /**
  210.      * Set attribute Type
  211.      *
  212.      * @param attributeType
  213.      *            attribute type
  214.      */
  215.     @Override
  216.     public void setAttributeType( AttributeType attributeType )
  217.     {
  218.         _attributeType = attributeType;
  219.     }

  220.     /**
  221.      * Get the anonymize status of the attribute
  222.      *
  223.      * @return True if the attribute should be anonymize, false otherwise.
  224.      */
  225.     @Override
  226.     public boolean getAnonymize( )
  227.     {
  228.         return _bAnonymize;
  229.     }

  230.     /**
  231.      * Set the anonymize status of the attribute
  232.      *
  233.      * @param bAnonymize
  234.      *            New anonymize status. True if the attribute should be anonymize, false otherwise.
  235.      */
  236.     @Override
  237.     public void setAnonymize( boolean bAnonymize )
  238.     {
  239.         _bAnonymize = bAnonymize;
  240.     }

  241.     /**
  242.      * Get Html form
  243.      *
  244.      * @param locale
  245.      *            locale
  246.      * @return html form
  247.      */
  248.     public String getHtmlFormAttribute( Locale locale )
  249.     {
  250.         Map<String, Object> model = new HashMap<>( );
  251.         model.put( MARK_ATTRIBUTE, this );

  252.         HtmlTemplate template = AppTemplateService.getTemplate( getTemplateHtmlFormAttribute( ), locale, model );

  253.         return template.getHtml( );
  254.     }

  255.     /**
  256.      * Get Html form
  257.      *
  258.      * @param locale
  259.      *            locale
  260.      * @param listDefaultValues
  261.      *            the list of default values
  262.      * @return html form
  263.      */
  264.     public String getHtmlFormAttribute( Locale locale, Object listDefaultValues )
  265.     {
  266.         Map<String, Object> model = new HashMap<>( );
  267.         model.put( MARK_ATTRIBUTE, this );
  268.         model.put( MARK_DEFAULT_VALUES_LIST, listDefaultValues );

  269.         HtmlTemplate template = AppTemplateService.getTemplate( getTemplateHtmlFormAttribute( ), locale, model );

  270.         return template.getHtml( );
  271.     }

  272.     /**
  273.      * Get Html form
  274.      *
  275.      * @param auFieldFilter
  276.      *            The admin user field filter
  277.      * @param locale
  278.      *            locale
  279.      * @return html form
  280.      */
  281.     public String getHtmlFormSearchAttribute( AdminUserFieldFilter auFieldFilter, Locale locale )
  282.     {
  283.         Map<String, Object> model = new HashMap<>( );
  284.         List<AdminUserField> listUserFields = auFieldFilter.getListUserFields( );
  285.         List<AdminUserField> selectedUserFields = null;

  286.         if ( ( listUserFields != null ) && ( !listUserFields.isEmpty( ) ) )
  287.         {
  288.             selectedUserFields = new ArrayList<>( );

  289.             for ( AdminUserField userField : listUserFields )
  290.             {
  291.                 if ( userField.getAttribute( ).getIdAttribute( ) == _nIdAttribute )
  292.                 {
  293.                     selectedUserFields.add( userField );
  294.                 }
  295.             }
  296.         }

  297.         model.put( MARK_DEFAULT_VALUES_LIST, selectedUserFields );
  298.         model.put( MARK_ATTRIBUTE, this );

  299.         HtmlTemplate template = AppTemplateService.getTemplate( getTemplateHtmlFormSearchAttribute( ), locale, model );

  300.         return template.getHtml( );
  301.     }

  302.     /**
  303.      * Get Html value
  304.      *
  305.      * @param locale
  306.      *            Locale
  307.      * @param userField
  308.      *            User field
  309.      * @return the html
  310.      */
  311.     public String getHtmlValue( Locale locale, AdminUserField userField )
  312.     {
  313.         Map<String, Object> model = new HashMap<>( );

  314.         model.put( MARK_ATTRIBUTE, this );
  315.         model.put( MARK_USER_FIELD, userField );

  316.         HtmlTemplate template = AppTemplateService.getTemplate( getTemplateHtmlValue( ), locale, model );

  317.         return template.getHtml( );
  318.     }

  319.     /**
  320.      * Get plugin
  321.      *
  322.      * @return plugin
  323.      */
  324.     @Override
  325.     public Plugin getPlugin( )
  326.     {
  327.         return _plugin;
  328.     }

  329.     /**
  330.      * Set plugin
  331.      *
  332.      * @param plugin
  333.      *            plugin
  334.      */
  335.     @Override
  336.     public void setPlugin( Plugin plugin )
  337.     {
  338.         _plugin = plugin;
  339.     }

  340.     /**
  341.      * Check if the attribute is shown in search
  342.      *
  343.      * @return true if it is, false otherwise
  344.      */
  345.     @Override
  346.     public boolean isShownInSearch( )
  347.     {
  348.         return _bIsShownInSearch;
  349.     }

  350.     /**
  351.      * Set isShownInSearch
  352.      *
  353.      * @param bIsShownInSearch
  354.      *            shown in search
  355.      */
  356.     @Override
  357.     public void setShownInSearch( boolean bIsShownInSearch )
  358.     {
  359.         _bIsShownInSearch = bIsShownInSearch;
  360.     }

  361.     /**
  362.      * Check if the attribute is to be shown in line
  363.      *
  364.      * @return true if it is shown in line, false otherwise
  365.      */
  366.     @Override
  367.     public boolean isFieldInLine( )
  368.     {
  369.         return _bIsFieldInLine;
  370.     }

  371.     /**
  372.      * Set isFieldInLine
  373.      *
  374.      * @param bIsFieldInLine
  375.      *            shown in line
  376.      */
  377.     @Override
  378.     public void setFieldInLine( boolean bIsFieldInLine )
  379.     {
  380.         _bIsFieldInLine = bIsFieldInLine;
  381.     }

  382.     /**
  383.      * Check if it is an attribute image
  384.      *
  385.      * @return true if it is, false otherwise
  386.      */
  387.     @Override
  388.     public boolean isAttributeImage( )
  389.     {
  390.         return _bIsAttributeImage;
  391.     }

  392.     /**
  393.      * Set the attribute as an attribute image
  394.      *
  395.      * @param bIsAttributeImage
  396.      *            true if it is an image, false otherwise
  397.      */
  398.     @Override
  399.     public void setAttributeImage( boolean bIsAttributeImage )
  400.     {
  401.         _bIsAttributeImage = bIsAttributeImage;
  402.     }

  403.     /**
  404.      * Check if the attribute is shown in result list
  405.      *
  406.      * @return true if it is, false otherwise
  407.      */
  408.     @Override
  409.     public boolean isShownInResultList( )
  410.     {
  411.         return _bIsShownInResultList;
  412.     }

  413.     /**
  414.      * Set isShownInResultList
  415.      *
  416.      * @param bIsShownInResultList
  417.      *            shown in result list
  418.      */
  419.     @Override
  420.     public void setShownInResultList( boolean bIsShownInResultList )
  421.     {
  422.         _bIsShownInResultList = bIsShownInResultList;
  423.     }
  424. }