AttributeDAO.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.plugin.PluginService;
  37. import fr.paris.lutece.portal.service.util.AppLogService;
  38. import fr.paris.lutece.util.sql.DAOUtil;

  39. import java.sql.Statement;
  40. import java.util.ArrayList;
  41. import java.util.List;
  42. import java.util.Locale;

  43. /**
  44.  *
  45.  * AttributeDAO
  46.  *
  47.  */
  48. public class AttributeDAO implements IAttributeDAO
  49. {
  50.     // NEW POSITION
  51.     private static final String SQL_QUERY_NEW_POSITION = "SELECT MAX(attribute_position)" + " FROM core_attribute ";

  52.     // SELECT
  53.     private static final String SQL_QUERY_SELECT = " SELECT type_class_name, id_attribute, title, help_message, is_mandatory, is_shown_in_search, is_shown_in_result_list, is_field_in_line, attribute_position, plugin_name "
  54.             + " FROM core_attribute WHERE id_attribute = ? ";
  55.     private static final String SQL_QUERY_SELECT_ALL = " SELECT type_class_name, id_attribute, title, help_message, is_mandatory, is_shown_in_search, is_shown_in_result_list, is_field_in_line, attribute_position, anonymize, plugin_name "
  56.             + " FROM core_attribute ORDER BY attribute_position ASC ";
  57.     private static final String SQL_QUERY_SELECT_PLUGIN_ATTRIBUTES = " SELECT type_class_name, id_attribute, title, help_message, is_mandatory, is_shown_in_search, is_shown_in_result_list, is_field_in_line, attribute_position "
  58.             + " FROM core_attribute WHERE plugin_name = ? ORDER BY attribute_position ASC ";
  59.     private static final String SQL_QUERY_SELECT_CORE_ATTRIBUTES = " SELECT type_class_name, id_attribute, title, help_message, is_mandatory, is_shown_in_search, is_shown_in_result_list, is_field_in_line, attribute_position "
  60.             + " FROM core_attribute WHERE plugin_name IS NULL OR plugin_name = '' ORDER BY attribute_position ASC ";

  61.     // INSERT
  62.     private static final String SQL_QUERY_INSERT = " INSERT INTO core_attribute (type_class_name, title, help_message, is_mandatory, is_shown_in_search, is_shown_in_result_list, is_field_in_line, attribute_position)"
  63.             + " VALUES (?,?,?,?,?,?,?,?) ";

  64.     // UPDATE
  65.     private static final String SQL_QUERY_UPDATE = " UPDATE core_attribute SET title = ?, help_message = ?, is_mandatory = ?, is_shown_in_search = ?, is_shown_in_result_list = ?, is_field_in_line = ?, attribute_position = ? "
  66.             + " WHERE id_attribute = ? ";
  67.     private static final String SQL_QUERY_UPDATE_ANONYMIZATION = " UPDATE core_attribute SET anonymize = ? WHERE id_attribute = ? ";

  68.     // DELETE
  69.     private static final String SQL_QUERY_DELETE = " DELETE FROM core_attribute WHERE id_attribute = ?";

  70.     /**
  71.      * Generates a new field position
  72.      *
  73.      * @return the new entry position
  74.      */
  75.     private int newPosition( )
  76.     {
  77.         int nPos;
  78.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_POSITION ) )
  79.         {
  80.             daoUtil.executeQuery( );

  81.             if ( !daoUtil.next( ) )
  82.             {
  83.                 // if the table is empty
  84.                 nPos = 1;
  85.             }

  86.             nPos = daoUtil.getInt( 1 ) + 1;
  87.         }

  88.         return nPos;
  89.     }

  90.     /**
  91.      * Load attribute
  92.      *
  93.      * @param nIdAttribute
  94.      *            ID
  95.      * @param locale
  96.      *            Locale
  97.      * @return Attribute
  98.      */
  99.     public IAttribute load( int nIdAttribute, Locale locale )
  100.     {
  101.         IAttribute attribute = null;
  102.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  103.         {
  104.             daoUtil.setInt( 1, nIdAttribute );
  105.             daoUtil.executeQuery( );

  106.             if ( daoUtil.next( ) )
  107.             {
  108.                 int nIndex = 1;

  109.                 try
  110.                 {
  111.                     attribute = (IAttribute) Class.forName( daoUtil.getString( nIndex++ ) ).newInstance( );
  112.                 }
  113.                 catch( InstantiationException | IllegalAccessException | ClassNotFoundException e )
  114.                 {
  115.                     AppLogService.error( e.getMessage( ), e );
  116.                 }

  117.                 if ( attribute != null )
  118.                 {
  119.                     attribute.setIdAttribute( daoUtil.getInt( nIndex++ ) );
  120.                     attribute.setTitle( daoUtil.getString( nIndex++ ) );
  121.                     attribute.setHelpMessage( daoUtil.getString( nIndex++ ) );
  122.                     attribute.setMandatory( daoUtil.getBoolean( nIndex++ ) );
  123.                     attribute.setShownInSearch( daoUtil.getBoolean( nIndex++ ) );
  124.                     attribute.setShownInResultList( daoUtil.getBoolean( nIndex++ ) );
  125.                     attribute.setFieldInLine( daoUtil.getBoolean( nIndex++ ) );
  126.                     attribute.setPosition( daoUtil.getInt( nIndex++ ) );
  127.                     attribute.setAttributeType( locale );

  128.                     Plugin plugin = PluginService.getPlugin( daoUtil.getString( nIndex++ ) );
  129.                     attribute.setPlugin( plugin );
  130.                 }
  131.             }

  132.         }

  133.         return attribute;
  134.     }

  135.     /**
  136.      * Insert a new attribute
  137.      *
  138.      * @param attribute
  139.      *            the attribute
  140.      * @return new PK
  141.      */
  142.     public int insert( IAttribute attribute )
  143.     {
  144.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
  145.         {
  146.             int nIndex = 1;
  147.             daoUtil.setString( nIndex++, attribute.getClass( ).getName( ) );
  148.             daoUtil.setString( nIndex++, attribute.getTitle( ) );
  149.             daoUtil.setString( nIndex++, attribute.getHelpMessage( ) );
  150.             daoUtil.setBoolean( nIndex++, attribute.isMandatory( ) );
  151.             daoUtil.setBoolean( nIndex++, attribute.isShownInSearch( ) );
  152.             daoUtil.setBoolean( nIndex++, attribute.isShownInResultList( ) );
  153.             daoUtil.setBoolean( nIndex++, attribute.isFieldInLine( ) );
  154.             daoUtil.setInt( nIndex++, newPosition( ) );

  155.             daoUtil.executeUpdate( );

  156.             if ( daoUtil.nextGeneratedKey( ) )
  157.             {
  158.                 attribute.setIdAttribute( daoUtil.getGeneratedKeyInt( 1 ) );
  159.             }
  160.         }

  161.         return attribute.getIdAttribute( );
  162.     }

  163.     /**
  164.      * Update an attribute
  165.      *
  166.      * @param attribute
  167.      *            the attribute
  168.      */
  169.     public void store( IAttribute attribute )
  170.     {
  171.         int nIndex = 1;
  172.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  173.         {
  174.             daoUtil.setString( nIndex++, attribute.getTitle( ) );
  175.             daoUtil.setString( nIndex++, attribute.getHelpMessage( ) );
  176.             daoUtil.setBoolean( nIndex++, attribute.isMandatory( ) );
  177.             daoUtil.setBoolean( nIndex++, attribute.isShownInSearch( ) );
  178.             daoUtil.setBoolean( nIndex++, attribute.isShownInResultList( ) );
  179.             daoUtil.setBoolean( nIndex++, attribute.isFieldInLine( ) );
  180.             daoUtil.setInt( nIndex++, attribute.getPosition( ) );
  181.             daoUtil.setInt( nIndex++, attribute.getIdAttribute( ) );

  182.             daoUtil.executeUpdate( );
  183.         }
  184.     }

  185.     /**
  186.      * Delete an attribute
  187.      *
  188.      * @param nIdAttribute
  189.      *            The Id of the attribute
  190.      */
  191.     public void delete( int nIdAttribute )
  192.     {
  193.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  194.         {
  195.             daoUtil.setInt( 1, nIdAttribute );

  196.             daoUtil.executeUpdate( );
  197.         }
  198.     }

  199.     /**
  200.      * Load every attributes
  201.      *
  202.      * @param locale
  203.      *            locale
  204.      * @return list of attributes
  205.      */
  206.     public List<IAttribute> selectAll( Locale locale )
  207.     {
  208.         List<IAttribute> listAttributes = new ArrayList<>( );
  209.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL ) )
  210.         {
  211.             daoUtil.executeQuery( );

  212.             while ( daoUtil.next( ) )
  213.             {
  214.                 int nIndex = 1;
  215.                 IAttribute attribute = null;

  216.                 try
  217.                 {
  218.                     attribute = (IAttribute) Class.forName( daoUtil.getString( nIndex++ ) ).newInstance( );
  219.                 }
  220.                 catch( InstantiationException | IllegalAccessException | ClassNotFoundException e )
  221.                 {
  222.                     AppLogService.error( e.getMessage( ), e );
  223.                 }

  224.                 if ( attribute != null )
  225.                 {
  226.                     attribute.setIdAttribute( daoUtil.getInt( nIndex++ ) );
  227.                     attribute.setTitle( daoUtil.getString( nIndex++ ) );
  228.                     attribute.setHelpMessage( daoUtil.getString( nIndex++ ) );
  229.                     attribute.setMandatory( daoUtil.getBoolean( nIndex++ ) );
  230.                     attribute.setShownInSearch( daoUtil.getBoolean( nIndex++ ) );
  231.                     attribute.setShownInResultList( daoUtil.getBoolean( nIndex++ ) );
  232.                     attribute.setFieldInLine( daoUtil.getBoolean( nIndex++ ) );
  233.                     attribute.setPosition( daoUtil.getInt( nIndex++ ) );
  234.                     attribute.setAnonymize( daoUtil.getBoolean( nIndex++ ) );
  235.                     attribute.setAttributeType( locale );

  236.                     Plugin plugin = PluginService.getPlugin( daoUtil.getString( nIndex++ ) );
  237.                     attribute.setPlugin( plugin );

  238.                     listAttributes.add( attribute );
  239.                 }
  240.             }

  241.         }

  242.         return listAttributes;
  243.     }

  244.     /**
  245.      * Load every attributes from plugin name
  246.      *
  247.      * @param strPluginName
  248.      *            plugin name
  249.      * @param locale
  250.      *            locale
  251.      * @return list of attributes
  252.      */
  253.     public List<IAttribute> selectPluginAttributes( String strPluginName, Locale locale )
  254.     {
  255.         int nIndex = 1;
  256.         List<IAttribute> listAttributes = new ArrayList<>( );
  257.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PLUGIN_ATTRIBUTES ) )
  258.         {
  259.             daoUtil.setString( 1, strPluginName );
  260.             daoUtil.executeQuery( );

  261.             while ( daoUtil.next( ) )
  262.             {
  263.                 nIndex = 1;

  264.                 IAttribute attribute = null;

  265.                 try
  266.                 {
  267.                     attribute = (IAttribute) Class.forName( daoUtil.getString( nIndex++ ) ).newInstance( );
  268.                 }
  269.                 catch( InstantiationException | IllegalAccessException | ClassNotFoundException e )
  270.                 {
  271.                     AppLogService.error( e.getMessage( ), e );
  272.                 }

  273.                 if ( attribute != null )
  274.                 {
  275.                     attribute.setIdAttribute( daoUtil.getInt( nIndex++ ) );
  276.                     attribute.setTitle( daoUtil.getString( nIndex++ ) );
  277.                     attribute.setHelpMessage( daoUtil.getString( nIndex++ ) );
  278.                     attribute.setMandatory( daoUtil.getBoolean( nIndex++ ) );
  279.                     attribute.setShownInSearch( daoUtil.getBoolean( nIndex++ ) );
  280.                     attribute.setShownInResultList( daoUtil.getBoolean( nIndex++ ) );
  281.                     attribute.setFieldInLine( daoUtil.getBoolean( nIndex++ ) );
  282.                     attribute.setPosition( daoUtil.getInt( nIndex++ ) );
  283.                     attribute.setAttributeType( locale );

  284.                     Plugin plugin = PluginService.getPlugin( strPluginName );
  285.                     attribute.setPlugin( plugin );

  286.                     listAttributes.add( attribute );
  287.                 }
  288.             }

  289.         }

  290.         return listAttributes;
  291.     }

  292.     /**
  293.      * Load every attributes that do not come from a plugin
  294.      *
  295.      * @param locale
  296.      *            locale
  297.      * @return list of attributes
  298.      */
  299.     public List<IAttribute> selectCoreAttributes( Locale locale )
  300.     {
  301.         List<IAttribute> listAttributes = new ArrayList<>( );
  302.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CORE_ATTRIBUTES ) )
  303.         {
  304.             daoUtil.executeQuery( );

  305.             while ( daoUtil.next( ) )
  306.             {
  307.                 int nIndex = 1;
  308.                 IAttribute attribute = null;

  309.                 try
  310.                 {
  311.                     attribute = (IAttribute) Class.forName( daoUtil.getString( nIndex++ ) ).newInstance( );
  312.                 }
  313.                 catch( InstantiationException | IllegalAccessException | ClassNotFoundException e )
  314.                 {
  315.                     AppLogService.error( e.getMessage( ), e );
  316.                 }

  317.                 if ( attribute != null )
  318.                 {
  319.                     attribute.setIdAttribute( daoUtil.getInt( nIndex++ ) );
  320.                     attribute.setTitle( daoUtil.getString( nIndex++ ) );
  321.                     attribute.setHelpMessage( daoUtil.getString( nIndex++ ) );
  322.                     attribute.setMandatory( daoUtil.getBoolean( nIndex++ ) );
  323.                     attribute.setShownInSearch( daoUtil.getBoolean( nIndex++ ) );
  324.                     attribute.setShownInResultList( daoUtil.getBoolean( nIndex++ ) );
  325.                     attribute.setFieldInLine( daoUtil.getBoolean( nIndex++ ) );
  326.                     attribute.setPosition( daoUtil.getInt( nIndex++ ) );
  327.                     attribute.setAttributeType( locale );

  328.                     listAttributes.add( attribute );
  329.                 }
  330.             }

  331.         }

  332.         return listAttributes;
  333.     }

  334.     /**
  335.      * Update the anonymization status of the attribute.
  336.      *
  337.      * @param nIdAttribute
  338.      *            Id of the attribute
  339.      * @param bAnonymize
  340.      *            New value of the anonymization status. True means the attribute should be anonymize, false means it doesn't.
  341.      */
  342.     public void updateAttributeAnonymization( int nIdAttribute, boolean bAnonymize )
  343.     {
  344.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_ANONYMIZATION ) )
  345.         {
  346.             daoUtil.setBoolean( 1, bAnonymize );
  347.             daoUtil.setInt( 2, nIdAttribute );
  348.             daoUtil.executeUpdate( );
  349.         }
  350.     }
  351. }