RBACRoleDAO.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.rbac;

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

  36. import java.util.ArrayList;
  37. import java.util.Collection;

  38. /**
  39.  * This class provides Data Access methods for RBACRole objects
  40.  */
  41. public final class RBACRoleDAO implements IRBACRoleDAO
  42. {
  43.     // Constants
  44.     private static final String SQL_QUERY_SELECT = " SELECT role_key, role_description FROM core_admin_role WHERE role_key = ?  ";
  45.     private static final String SQL_QUERY_INSERT = " INSERT INTO core_admin_role ( role_key, role_description ) VALUES ( ?, ? ) ";
  46.     private static final String SQL_QUERY_DELETE = " DELETE FROM core_admin_role WHERE role_key = ?  ";
  47.     private static final String SQL_QUERY_UPDATE = " UPDATE core_admin_role SET role_key = ?, role_description = ? WHERE role_key = ?  ";
  48.     private static final String SQL_QUERY_SELECTALL = " SELECT role_key, role_description FROM core_admin_role ORDER BY role_key";

  49.     /**
  50.      * Insert a new record in the table.
  51.      *
  52.      * @param role
  53.      *            The role object
  54.      */
  55.     public void insert( RBACRole role )
  56.     {
  57.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
  58.         {
  59.             daoUtil.setString( 1, role.getKey( ) );
  60.             daoUtil.setString( 2, role.getDescription( ) );

  61.             daoUtil.executeUpdate( );
  62.         }
  63.     }

  64.     /**
  65.      * Load the data of RBACRole from the table
  66.      *
  67.      * @param strRoleKey
  68.      *            The identifier of RBACRole
  69.      * @return the instance of the RBACRole
  70.      */
  71.     public RBACRole load( String strRoleKey )
  72.     {
  73.         RBACRole role = null;
  74.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  75.         {
  76.             daoUtil.setString( 1, strRoleKey );
  77.             daoUtil.executeQuery( );

  78.             if ( daoUtil.next( ) )
  79.             {
  80.                 role = new RBACRole( );
  81.                 role.setKey( daoUtil.getString( 1 ) );
  82.                 role.setDescription( daoUtil.getString( 2 ) );
  83.             }

  84.         }

  85.         return role;
  86.     }

  87.     /**
  88.      * Delete a record from the table
  89.      *
  90.      * @param strRoleKey
  91.      *            The role key
  92.      */
  93.     public void delete( String strRoleKey )
  94.     {
  95.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  96.         {
  97.             daoUtil.setString( 1, strRoleKey );

  98.             daoUtil.executeUpdate( );
  99.         }
  100.     }

  101.     /**
  102.      * Update the record identified by the given role key with the given role in the table
  103.      *
  104.      * @param strRoleKey
  105.      *            the key of the role to modify
  106.      * @param role
  107.      *            The reference of role to be the new one
  108.      */
  109.     public void store( String strRoleKey, RBACRole role )
  110.     {
  111.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  112.         {
  113.             daoUtil.setString( 1, role.getKey( ) );
  114.             daoUtil.setString( 2, role.getDescription( ) );
  115.             daoUtil.setString( 3, strRoleKey );

  116.             daoUtil.executeUpdate( );
  117.         }
  118.     }

  119.     /**
  120.      * Load the list of roles
  121.      *
  122.      * @return The Collection of the Roles
  123.      */
  124.     public Collection<RBACRole> selectRoleList( )
  125.     {
  126.         Collection<RBACRole> listRoles = new ArrayList<>( );
  127.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
  128.         {
  129.             daoUtil.executeQuery( );

  130.             while ( daoUtil.next( ) )
  131.             {
  132.                 RBACRole role = new RBACRole( );
  133.                 role.setKey( daoUtil.getString( 1 ) );
  134.                 role.setDescription( daoUtil.getString( 2 ) );

  135.                 listRoles.add( role );
  136.             }

  137.         }

  138.         return listRoles;
  139.     }

  140.     /**
  141.      * Check that the given key points to an existing role
  142.      *
  143.      * @param strRoleKey
  144.      *            the role key
  145.      * @return true if the role exists, false otherwise
  146.      */
  147.     public boolean checkExistRole( String strRoleKey )
  148.     {
  149.         boolean check = false;
  150.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  151.         {
  152.             daoUtil.setString( 1, strRoleKey );
  153.             daoUtil.executeQuery( );

  154.             if ( daoUtil.next( ) )
  155.             {
  156.                 check = true;
  157.             }
  158.         }

  159.         return check;
  160.     }
  161. }