AttributeService.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.service.user.attribute;

  35. import fr.paris.lutece.portal.business.user.attribute.AttributeField;
  36. import fr.paris.lutece.portal.business.user.attribute.AttributeFieldHome;
  37. import fr.paris.lutece.portal.business.user.attribute.AttributeHome;
  38. import fr.paris.lutece.portal.business.user.attribute.IAttribute;

  39. import java.util.List;
  40. import java.util.Locale;

  41. /**
  42.  *
  43.  * AttributeService
  44.  *
  45.  */
  46. public final class AttributeService
  47. {
  48.     private static AttributeService _singleton;

  49.     /**
  50.      * Private constructor
  51.      */
  52.     private AttributeService( )
  53.     {
  54.     }

  55.     /**
  56.      * Get the instance of {@link AttributeService}
  57.      *
  58.      * @return an instance of {@link AttributeService}
  59.      */
  60.     public static synchronized AttributeService getInstance( )
  61.     {
  62.         if ( _singleton == null )
  63.         {
  64.             _singleton = new AttributeService( );
  65.         }

  66.         return _singleton;
  67.     }

  68.     /**
  69.      * Get an attribute without its attribute fields
  70.      *
  71.      * @param nIdAttribute
  72.      *            the id attribute
  73.      * @param locale
  74.      *            {@link Locale}
  75.      * @return a {@link IAttribute}
  76.      */
  77.     public IAttribute getAttributeWithoutFields( int nIdAttribute, Locale locale )
  78.     {
  79.         return AttributeHome.findByPrimaryKey( nIdAttribute, locale );
  80.     }

  81.     /**
  82.      * Get the attribute with its attribute fields
  83.      *
  84.      * @param nIdAttribute
  85.      *            the id attribute
  86.      * @param locale
  87.      *            the {@link Locale}
  88.      * @return a {@link IAttribute}
  89.      */
  90.     public IAttribute getAttributeWithFields( int nIdAttribute, Locale locale )
  91.     {
  92.         IAttribute attribute = getAttributeWithoutFields( nIdAttribute, locale );
  93.         setAttributeField( attribute );

  94.         return attribute;
  95.     }

  96.     /**
  97.      * Get all user attribute without its attribute fields.
  98.      *
  99.      * @param locale
  100.      *            the {@link Locale}
  101.      * @return a list of {@link IAttribute}
  102.      */
  103.     public List<IAttribute> getAllAttributesWithoutFields( Locale locale )
  104.     {
  105.         return AttributeHome.findAll( locale );
  106.     }

  107.     /**
  108.      * Get core user attribute without its attribute fields
  109.      *
  110.      * @param locale
  111.      *            the {@link Locale}
  112.      * @return a list of {@link IAttribute}
  113.      */
  114.     public List<IAttribute> getCoreAttributesWithoutFields( Locale locale )
  115.     {
  116.         return AttributeHome.findCoreAttributes( locale );
  117.     }

  118.     /**
  119.      * Get plugin user attribute without its attribute fields
  120.      *
  121.      * @param strPluginName
  122.      *            the plugin name
  123.      * @param locale
  124.      *            the {@link Locale}
  125.      * @return a list of {@link IAttribute}
  126.      */
  127.     public List<IAttribute> getPluginAttributesWithoutFields( String strPluginName, Locale locale )
  128.     {
  129.         return AttributeHome.findPluginAttributes( strPluginName, locale );
  130.     }

  131.     /**
  132.      * Get all user attributes with its attribute fields
  133.      *
  134.      * @param locale
  135.      *            the {@link Locale}
  136.      * @return a list of {@link IAttribute}
  137.      */
  138.     public List<IAttribute> getAllAttributesWithFields( Locale locale )
  139.     {
  140.         List<IAttribute> listAttributes = getAllAttributesWithoutFields( locale );
  141.         setAttributeFields( listAttributes );

  142.         return listAttributes;
  143.     }

  144.     /**
  145.      * Get core user attributes with its attribute fields
  146.      *
  147.      * @param locale
  148.      *            the {@link Locale}
  149.      * @return a list of {@link IAttribute}
  150.      */
  151.     public List<IAttribute> getCoreAttributesWithFields( Locale locale )
  152.     {
  153.         List<IAttribute> listAttributes = getCoreAttributesWithoutFields( locale );
  154.         setAttributeFields( listAttributes );

  155.         return listAttributes;
  156.     }

  157.     /**
  158.      * Get plugin user attributes with its attribute fields
  159.      *
  160.      * @param strPluginName
  161.      *            the plugin name
  162.      * @param locale
  163.      *            the {@link Locale}
  164.      * @return a list of {@link IAttribute}
  165.      */
  166.     public List<IAttribute> getPluginAttributesWithFields( String strPluginName, Locale locale )
  167.     {
  168.         List<IAttribute> listAttributes = getPluginAttributesWithoutFields( strPluginName, locale );
  169.         setAttributeFields( listAttributes );

  170.         return listAttributes;
  171.     }

  172.     /**
  173.      * Set the attribute fields from a given list of {@link IAttribute}
  174.      *
  175.      * @param listAttributes
  176.      *            the list of {@link IAttribute}
  177.      */
  178.     public void setAttributeFields( List<IAttribute> listAttributes )
  179.     {
  180.         for ( IAttribute attribute : listAttributes )
  181.         {
  182.             setAttributeField( attribute );
  183.         }
  184.     }

  185.     /**
  186.      * Set the attribute field from a given {@link IAttribute}
  187.      *
  188.      * @param attribute
  189.      *            the {@link IAttribute}
  190.      */
  191.     public void setAttributeField( IAttribute attribute )
  192.     {
  193.         if ( attribute != null )
  194.         {
  195.             List<AttributeField> listAttributeFields = AttributeFieldHome.selectAttributeFieldsByIdAttribute( attribute.getIdAttribute( ) );
  196.             attribute.setListAttributeFields( listAttributeFields );
  197.         }
  198.     }

  199.     /**
  200.      * Create a new attribute and its attribute field.
  201.      *
  202.      * @param attribute
  203.      *            the {@link IAttribute} to create
  204.      */
  205.     public void createAttribute( IAttribute attribute )
  206.     {
  207.         if ( attribute != null )
  208.         {
  209.             int nIdAttribute = AttributeHome.create( attribute );
  210.             attribute.setIdAttribute( nIdAttribute );

  211.             if ( attribute.getListAttributeFields( ) != null )
  212.             {
  213.                 for ( AttributeField attributeField : attribute.getListAttributeFields( ) )
  214.                 {
  215.                     attributeField.setAttribute( attribute );
  216.                     AttributeFieldService.getInstance( ).createAttributeField( attributeField );
  217.                 }
  218.             }
  219.         }
  220.     }

  221.     /**
  222.      * Update the attribute
  223.      *
  224.      * @param attribute
  225.      *            the {@link IAttribute} to update
  226.      */
  227.     public void updateAttribute( IAttribute attribute )
  228.     {
  229.         if ( attribute != null )
  230.         {
  231.             AttributeHome.update( attribute );

  232.             if ( attribute.getListAttributeFields( ) != null )
  233.             {
  234.                 for ( AttributeField attributeField : attribute.getListAttributeFields( ) )
  235.                 {
  236.                     attributeField.setAttribute( attribute );
  237.                     AttributeFieldService.getInstance( ).updateAttributeField( attributeField );
  238.                 }
  239.             }
  240.         }
  241.     }

  242.     /**
  243.      * Remove the attribute from a given attribute ID
  244.      *
  245.      * @param nIdAttribute
  246.      *            the ID attribute
  247.      */
  248.     public void removeAttribute( int nIdAttribute )
  249.     {
  250.         // Remove the AdminUserField associated to the attribute
  251.         AdminUserFieldService.doRemoveUserFieldsByIdAttribute( nIdAttribute );
  252.         // Remove the AttributeField associated to the attribute
  253.         AttributeFieldService.getInstance( ).removeAttributeFieldsFromIdAttribute( nIdAttribute );
  254.         // Remove the Attribute
  255.         AttributeHome.remove( nIdAttribute );
  256.     }

  257.     /**
  258.      * Update the anonymization status of the attribute.
  259.      *
  260.      * @param nIdAttribute
  261.      *            Id of the attribute
  262.      * @param bAnonymize
  263.      *            New value of the anonymization status. True means the attribute should be anonymize, false means it doesn't.
  264.      */
  265.     public void updateAnonymizationStatusUserField( int nIdAttribute, boolean bAnonymize )
  266.     {
  267.         AttributeHome.updateAttributeAnonymization( nIdAttribute, bAnonymize );
  268.     }
  269. }