View Javadoc
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  
36  import fr.paris.lutece.util.sql.DAOUtil;
37  
38  import java.util.ArrayList;
39  import java.util.Collection;
40  
41  /**
42   * This class provides Data Access methods for RBACRole objects
43   */
44  public final class RBACRoleDAO implements IRBACRoleDAO
45  {
46      // Constants
47      private static final String SQL_QUERY_SELECT = " SELECT role_key, role_description FROM core_admin_role WHERE role_key = ?  ";
48      private static final String SQL_QUERY_INSERT = " INSERT INTO core_admin_role ( role_key, role_description ) VALUES ( ?, ? ) ";
49      private static final String SQL_QUERY_DELETE = " DELETE FROM core_admin_role WHERE role_key = ?  ";
50      private static final String SQL_QUERY_UPDATE = " UPDATE core_admin_role SET role_key = ?, role_description = ? WHERE role_key = ?  ";
51      private static final String SQL_QUERY_SELECTALL = " SELECT role_key, role_description FROM core_admin_role ORDER BY role_key";
52  
53      /**
54       * Insert a new record in the table.
55       *
56       * @param role
57       *            The role object
58       */
59      public void insert( RBACRole role )
60      {
61          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
62          {
63              daoUtil.setString( 1, role.getKey( ) );
64              daoUtil.setString( 2, role.getDescription( ) );
65  
66              daoUtil.executeUpdate( );
67          }
68      }
69  
70      /**
71       * Load the data of RBACRole from the table
72       *
73       * @param strRoleKey
74       *            The identifier of RBACRole
75       * @return the instance of the RBACRole
76       */
77      public RBACRole load( String strRoleKey )
78      {
79          RBACRole role = null;
80          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
81          {
82              daoUtil.setString( 1, strRoleKey );
83              daoUtil.executeQuery( );
84  
85              if ( daoUtil.next( ) )
86              {
87                  role = new RBACRole( );
88                  role.setKey( daoUtil.getString( 1 ) );
89                  role.setDescription( daoUtil.getString( 2 ) );
90              }
91  
92          }
93  
94          return role;
95      }
96  
97      /**
98       * Delete a record from the table
99       * 
100      * @param strRoleKey
101      *            The role key
102      */
103     public void delete( String strRoleKey )
104     {
105         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
106         {
107             daoUtil.setString( 1, strRoleKey );
108 
109             daoUtil.executeUpdate( );
110         }
111     }
112 
113     /**
114      * Update the record identified by the given role key with the given role in the table
115      * 
116      * @param strRoleKey
117      *            the key of the role to modify
118      * @param role
119      *            The reference of role to be the new one
120      */
121     public void store( String strRoleKey, RBACRole role )
122     {
123         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
124         {
125             daoUtil.setString( 1, role.getKey( ) );
126             daoUtil.setString( 2, role.getDescription( ) );
127             daoUtil.setString( 3, strRoleKey );
128 
129             daoUtil.executeUpdate( );
130         }
131     }
132 
133     /**
134      * Load the list of roles
135      * 
136      * @return The Collection of the Roles
137      */
138     public Collection<RBACRole> selectRoleList( )
139     {
140         Collection<RBACRole> listRoles = new ArrayList<>( );
141         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
142         {
143             daoUtil.executeQuery( );
144 
145             while ( daoUtil.next( ) )
146             {
147                 RBACRole/business/rbac/RBACRole.html#RBACRole">RBACRole role = new RBACRole( );
148                 role.setKey( daoUtil.getString( 1 ) );
149                 role.setDescription( daoUtil.getString( 2 ) );
150 
151                 listRoles.add( role );
152             }
153 
154         }
155 
156         return listRoles;
157     }
158 
159     /**
160      * Check that the given key points to an existing role
161      * 
162      * @param strRoleKey
163      *            the role key
164      * @return true if the role exists, false otherwise
165      */
166     public boolean checkExistRole( String strRoleKey )
167     {
168         boolean check = false;
169         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
170         {
171             daoUtil.setString( 1, strRoleKey );
172             daoUtil.executeQuery( );
173 
174             if ( daoUtil.next( ) )
175             {
176                 check = true;
177             }
178         }
179 
180         return check;
181     }
182 }