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