RoleDAO.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.role;

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

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

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

  50.     // /////////////////////////////////////////////////////////////////////////////////////
  51.     // Access methods to data

  52.     /**
  53.      * Insert a new record in the table.
  54.      *
  55.      * @param role
  56.      *            The Instance of the object Role
  57.      */
  58.     public void insert( Role role )
  59.     {
  60.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
  61.         {

  62.             daoUtil.setString( 1, role.getRole( ) );
  63.             daoUtil.setString( 2, role.getRoleDescription( ) );
  64.             daoUtil.setString( 3, role.getWorkgroup( ) );

  65.             daoUtil.executeUpdate( );
  66.         }
  67.     }

  68.     /**
  69.      * load the data of Role from the table
  70.      *
  71.      * @param strRole
  72.      *            The indentifier of the object Role
  73.      * @return The Instance of the object Role
  74.      */
  75.     public Role load( String strRole )
  76.     {
  77.         Role role = null;
  78.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  79.         {
  80.             daoUtil.setString( 1, strRole );

  81.             daoUtil.executeQuery( );

  82.             if ( daoUtil.next( ) )
  83.             {
  84.                 role = new Role( );
  85.                 role.setRole( daoUtil.getString( 1 ) );
  86.                 role.setRoleDescription( daoUtil.getString( 2 ) );
  87.                 role.setWorkgroup( daoUtil.getString( 3 ) );
  88.             }

  89.         }

  90.         return role;
  91.     }

  92.     /**
  93.      * Delete a record from the table
  94.      *
  95.      * @param strRole
  96.      *            The indentifier of the object Role
  97.      */
  98.     public void delete( String strRole )
  99.     {
  100.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  101.         {
  102.             daoUtil.setString( 1, strRole );
  103.             daoUtil.executeUpdate( );
  104.         }
  105.     }

  106.     /**
  107.      * Update the record in the table
  108.      *
  109.      * @param role
  110.      *            The instance of the Role to update
  111.      */
  112.     public void store( Role role )
  113.     {
  114.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  115.         {

  116.             daoUtil.setString( 1, role.getRoleDescription( ) );
  117.             daoUtil.setString( 2, role.getWorkgroup( ) );
  118.             daoUtil.setString( 3, role.getRole( ) );

  119.             daoUtil.executeUpdate( );
  120.         }
  121.     }

  122.     /**
  123.      * Returns a list of all the right role
  124.      *
  125.      * @return A ReferenceList of role objects
  126.      */
  127.     public ReferenceList selectRolesList( )
  128.     {
  129.         ReferenceList roleList = new ReferenceList( );
  130.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
  131.         {
  132.             daoUtil.executeQuery( );

  133.             while ( daoUtil.next( ) )
  134.             {
  135.                 Role role = new Role( );
  136.                 role.setRole( daoUtil.getString( 1 ) );
  137.                 role.setRoleDescription( daoUtil.getString( 2 ) );

  138.                 roleList.addItem( role.getRole( ), role.getRoleDescription( ) );
  139.             }

  140.         }

  141.         return roleList;
  142.     }

  143.     /**
  144.      * Load the list of roles
  145.      *
  146.      * @return The Collection of the Roles
  147.      */
  148.     public Collection<Role> selectAll( )
  149.     {
  150.         Collection<Role> listRoles = new ArrayList<>( );
  151.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
  152.         {
  153.             daoUtil.executeQuery( );

  154.             while ( daoUtil.next( ) )
  155.             {
  156.                 Role role = new Role( );
  157.                 role.setRole( daoUtil.getString( 1 ) );
  158.                 role.setRoleDescription( daoUtil.getString( 2 ) );
  159.                 role.setWorkgroup( daoUtil.getString( 3 ) );

  160.                 listRoles.add( role );
  161.             }

  162.         }

  163.         return listRoles;
  164.     }
  165. }