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.role;
35  
36  import fr.paris.lutece.util.ReferenceList;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  
42  /**
43   * This class provides Data Access methods for Role objects
44   */
45  public final class RoleDAO implements IRoleDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_SELECT = " SELECT role, role_description, workgroup_key FROM core_role WHERE role = ?";
49      private static final String SQL_QUERY_SELECTALL = " SELECT role , role_description, workgroup_key FROM core_role ORDER BY role";
50      private static final String SQL_QUERY_INSERT = " INSERT INTO core_role ( role, role_description, workgroup_key ) VALUES ( ?, ?, ? )";
51      private static final String SQL_QUERY_DELETE = " DELETE FROM core_role WHERE role = ? ";
52      private static final String SQL_QUERY_UPDATE = " UPDATE core_role SET role_description = ?, workgroup_key = ? WHERE role = ?";
53  
54      // /////////////////////////////////////////////////////////////////////////////////////
55      // Access methods to data
56  
57      /**
58       * Insert a new record in the table.
59       * 
60       * @param role
61       *            The Instance of the object Role
62       */
63      public void insert( Role role )
64      {
65          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
66          {
67  
68              daoUtil.setString( 1, role.getRole( ) );
69              daoUtil.setString( 2, role.getRoleDescription( ) );
70              daoUtil.setString( 3, role.getWorkgroup( ) );
71  
72              daoUtil.executeUpdate( );
73          }
74      }
75  
76      /**
77       * load the data of Role from the table
78       * 
79       * @param strRole
80       *            The indentifier of the object Role
81       * @return The Instance of the object Role
82       */
83      public Role load( String strRole )
84      {
85          Role role = null;
86          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
87          {
88              daoUtil.setString( 1, strRole );
89  
90              daoUtil.executeQuery( );
91  
92              if ( daoUtil.next( ) )
93              {
94                  role = new Role( );
95                  role.setRole( daoUtil.getString( 1 ) );
96                  role.setRoleDescription( daoUtil.getString( 2 ) );
97                  role.setWorkgroup( daoUtil.getString( 3 ) );
98              }
99  
100         }
101 
102         return role;
103     }
104 
105     /**
106      * Delete a record from the table
107      * 
108      * @param strRole
109      *            The indentifier of the object Role
110      */
111     public void delete( String strRole )
112     {
113         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
114         {
115             daoUtil.setString( 1, strRole );
116             daoUtil.executeUpdate( );
117         }
118     }
119 
120     /**
121      * Update the record in the table
122      * 
123      * @param role
124      *            The instance of the Role to update
125      */
126     public void store( Role role )
127     {
128         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
129         {
130 
131             daoUtil.setString( 1, role.getRoleDescription( ) );
132             daoUtil.setString( 2, role.getWorkgroup( ) );
133             daoUtil.setString( 3, role.getRole( ) );
134 
135             daoUtil.executeUpdate( );
136         }
137     }
138 
139     /**
140      * Returns a list of all the right role
141      * 
142      * @return A ReferenceList of role objects
143      */
144     public ReferenceList selectRolesList( )
145     {
146         ReferenceListist.html#ReferenceList">ReferenceList roleList = new ReferenceList( );
147         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
148         {
149             daoUtil.executeQuery( );
150 
151             while ( daoUtil.next( ) )
152             {
153                 Rolertal/business/role/Role.html#Role">Role role = new Role( );
154                 role.setRole( daoUtil.getString( 1 ) );
155                 role.setRoleDescription( daoUtil.getString( 2 ) );
156 
157                 roleList.addItem( role.getRole( ), role.getRoleDescription( ) );
158             }
159 
160         }
161 
162         return roleList;
163     }
164 
165     /**
166      * Load the list of roles
167      * 
168      * @return The Collection of the Roles
169      */
170     public Collection<Role> selectAll( )
171     {
172         Collection<Role> listRoles = new ArrayList<>( );
173         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
174         {
175             daoUtil.executeQuery( );
176 
177             while ( daoUtil.next( ) )
178             {
179                 Rolertal/business/role/Role.html#Role">Role role = new Role( );
180                 role.setRole( daoUtil.getString( 1 ) );
181                 role.setRoleDescription( daoUtil.getString( 2 ) );
182                 role.setWorkgroup( daoUtil.getString( 3 ) );
183 
184                 listRoles.add( role );
185             }
186 
187         }
188 
189         return listRoles;
190     }
191 }