SecurityHeaderDAO.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.securityheader;

  35. import java.sql.Statement;
  36. import java.util.ArrayList;
  37. import java.util.List;

  38. import fr.paris.lutece.util.sql.DAOUtil;

  39. /**
  40.  * This class provides Data Access methods for SecurityHeader objects
  41.  */
  42. public final class SecurityHeaderDAO implements ISecurityHeaderDAO
  43. {
  44.       // Constants
  45.       private static final String SQL_QUERY_SELECT = "SELECT id_security_header, name, value, description, type, page_category, is_active FROM core_admin_security_header WHERE id_security_header = ?";
  46.       private static final String SQL_QUERY_INSERT = "INSERT INTO core_admin_security_header ( name, value, description, type, page_category ) VALUES ( ?, ?, ?, ?, ? ) ";
  47.       private static final String SQL_QUERY_DELETE = "DELETE FROM core_admin_security_header WHERE id_security_header = ? ";
  48.       private static final String SQL_QUERY_UPDATE = "UPDATE core_admin_security_header SET value = ?, description = ?, type = ?, page_category = ? WHERE id_security_header = ?";
  49.       private static final String SQL_QUERY_UPDATE_IS_ACTIVE = "UPDATE core_admin_security_header SET is_active = ? WHERE id_security_header = ?";
  50.       private static final String SQL_QUERY_SELECTALL = "SELECT id_security_header, name, value, description, type, page_category, is_active FROM core_admin_security_header";
  51.       private static final String SQL_QUERY_SELECT_ACTIVE_BY_TYPE = "SELECT id_security_header, name, value, description, type, page_category, is_active FROM core_admin_security_header WHERE is_active = 'true' and type = ? ";

  52.     /**
  53.      * Insert a new record in the table.
  54.      *
  55.      * @param securityHeader
  56.      *            instance of the SecurityHeader object to insert
  57.      */
  58.     @Override
  59.     public void insert( SecurityHeader securityHeader )
  60.     {
  61.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
  62.         {
  63.             int nIndex = 1;
  64.             daoUtil.setString( nIndex++, securityHeader.getName( ) );
  65.             daoUtil.setString( nIndex++, securityHeader.getValue( ) );
  66.             daoUtil.setString( nIndex++, securityHeader.getDescription( ) );
  67.             daoUtil.setString( nIndex++, securityHeader.getType( ) );
  68.             daoUtil.setString( nIndex, securityHeader.getPageCategory() );

  69.             daoUtil.executeUpdate( );

  70.             if ( daoUtil.nextGeneratedKey( ) )
  71.             {
  72.                 securityHeader.setId( daoUtil.getGeneratedKeyInt( 1 ) );
  73.             }
  74.         }
  75.     }

  76.     /**
  77.      * Loads the data of the security header from the table
  78.      *
  79.      * @param nId
  80.      *            The identifier of the security header
  81.      * @return the instance of the Security header
  82.      */
  83.     @Override
  84.     public SecurityHeader load( int nId )
  85.     {
  86.         SecurityHeader securityHeader = null;
  87.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  88.         {
  89.             daoUtil.setInt( 1, nId );
  90.             daoUtil.executeQuery( );

  91.             if ( daoUtil.next( ) )
  92.             {
  93.                 securityHeader = new SecurityHeader( );

  94.                 securityHeader.setId( daoUtil.getInt( 1 ) );
  95.                 securityHeader.setName( daoUtil.getString( 2 ) );              
  96.                 securityHeader.setValue( daoUtil.getString( 3 ) );
  97.                 securityHeader.setDescription( daoUtil.getString( 4 ) );
  98.                 securityHeader.setType( daoUtil.getString( 5 ) );
  99.                 securityHeader.setPageCategory(daoUtil.getString( 6 ));
  100.                 securityHeader.setActive( daoUtil.getBoolean( 7 ) );
  101.             }

  102.         }

  103.         return securityHeader;
  104.     }

  105.     /**
  106.      * Delete a record from the table
  107.      *
  108.      * @param nSecurityHeaderId
  109.      *            The identifier of the security header
  110.      */
  111.     @Override
  112.     public void delete( int nSecurityHeaderId )
  113.     {
  114.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  115.         {
  116.             daoUtil.setInt( 1, nSecurityHeaderId );
  117.             daoUtil.executeUpdate( );
  118.         }
  119.     }

  120.     /**
  121.      * Update the record in the table
  122.      *
  123.      * @param securityHeader
  124.      *            The reference of the security header
  125.      */
  126.     @Override
  127.     public void store( SecurityHeader securityHeader )
  128.     {
  129.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  130.         {

  131.             daoUtil.setString( 1, securityHeader.getValue( ) );
  132.             daoUtil.setString( 2, securityHeader.getDescription( ) );
  133.             daoUtil.setString( 3, securityHeader.getType( ) );
  134.             daoUtil.setString( 4, securityHeader.getPageCategory( ) );
  135.             daoUtil.setInt( 5, securityHeader.getId( ) );

  136.             daoUtil.executeUpdate( );
  137.         }
  138.     }
  139.    
  140.     /**
  141.      * Update of "is_active" column value of security header which is specified in parameter
  142.      *
  143.      * @param nSecurityHeaderId
  144.      *            The securityHeader Id
  145.      * @param isActiveValue
  146.      *            The value to set to is_active column
  147.      */
  148.     @Override
  149.     public void updateIsActive( int nSecurityHeaderId, boolean isActiveValue )
  150.     {
  151.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_IS_ACTIVE ) )
  152.         {

  153.             daoUtil.setBoolean( 1, isActiveValue );
  154.             daoUtil.setInt( 2, nSecurityHeaderId );

  155.             daoUtil.executeUpdate( );
  156.         }
  157.     }
  158.    
  159.     /**
  160.      * Loads the data of all the security headers and returns them in form of a collection
  161.      *
  162.      * @return The Collection which contains the data of all the security headers
  163.      */
  164.     @Override
  165.     public List<SecurityHeader> selectAll( )
  166.     {
  167.         List<SecurityHeader> securityHeadersList = new ArrayList<SecurityHeader>( );
  168.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
  169.         {
  170.             daoUtil.executeQuery( );

  171.             while ( daoUtil.next( ) )
  172.             {
  173.                 SecurityHeader securityHeader = new SecurityHeader( );

  174.                 securityHeader.setId( daoUtil.getInt( 1 ) );
  175.                 securityHeader.setName( daoUtil.getString( 2 ) );              
  176.                 securityHeader.setValue( daoUtil.getString( 3 ) );
  177.                 securityHeader.setDescription( daoUtil.getString( 4 ) );
  178.                 securityHeader.setType( daoUtil.getString( 5 ) );
  179.                 securityHeader.setPageCategory(daoUtil.getString( 6 ) );
  180.                 securityHeader.setActive( daoUtil.getBoolean( 7 ) );

  181.                 securityHeadersList.add( securityHeader );
  182.             }

  183.         }

  184.         return securityHeadersList;
  185.     }

  186.     /**
  187.      * Returns all active security headers from a specified type (page or REST api)
  188.      *
  189.      * @param strType
  190.      *            The type
  191.      * @return the list which contains the data of all the securityHeaders
  192.      */
  193.     @Override
  194.     public List<SecurityHeader> selectActiveByType( String strType )
  195.     {
  196.         List<SecurityHeader> securityHeadersList = new ArrayList<>( );
  197.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ACTIVE_BY_TYPE ) )
  198.         {
  199.             daoUtil.setString( 1, strType );
  200.             daoUtil.executeQuery( );

  201.             while ( daoUtil.next( ) )
  202.             {
  203.                 SecurityHeader securityHeader = new SecurityHeader( );

  204.                 securityHeader.setId( daoUtil.getInt( 1 ) );
  205.                 securityHeader.setName( daoUtil.getString( 2 ) );
  206.                 securityHeader.setDescription( daoUtil.getString( 3 ) );
  207.                 securityHeader.setValue( daoUtil.getString( 4 ) );
  208.                 securityHeader.setType( daoUtil.getString( 5 ) );
  209.                 securityHeader.setPageCategory(daoUtil.getString( 6 ) );
  210.                 securityHeader.setActive( daoUtil.getBoolean( 7 ) );

  211.                 securityHeadersList.add( securityHeader );
  212.             }
  213.         }

  214.         return securityHeadersList;
  215.     }
  216. }