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.plugins.mylutece.modules.wssodatabase.authentication.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  
42  
43  /**
44   * This class provides Data Access methods for WssoUser objects
45   */
46  public final class WssoUserDAO implements IWssoUserDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = " SELECT max( mylutece_wsso_user_id ) FROM mylutece_wsso_user ";
50      private static final String SQL_QUERY_SELECT = " SELECT mylutece_wsso_user_id, guid, last_name, first_name, email, date_last_login FROM mylutece_wsso_user WHERE mylutece_wsso_user_id = ? ";
51      private static final String SQL_QUERY_INSERT = " INSERT INTO mylutece_wsso_user ( mylutece_wsso_user_id, guid, last_name, first_name, email ) VALUES ( ?, ?, ?, ?, ? ) ";
52      private static final String SQL_QUERY_DELETE = " DELETE FROM mylutece_wsso_user WHERE mylutece_wsso_user_id = ?  ";
53      private static final String SQL_QUERY_UPDATE = " UPDATE mylutece_wsso_user SET mylutece_wsso_user_id = ?, guid = ?, last_name = ?, first_name = ?, email = ? WHERE mylutece_wsso_user_id = ?  ";
54      private static final String SQL_QUERY_SELECTALL = " SELECT mylutece_wsso_user_id, guid, last_name, first_name, email, date_last_login FROM mylutece_wsso_user ORDER BY last_name, first_name, email ";
55      private static final String SQL_QUERY_SELECTALL_FOR_ROLE = " SELECT u.mylutece_wsso_user_id, u.guid, u.last_name, u.first_name, u.email, u.date_last_login FROM mylutece_wsso_user u, mylutece_wsso_user_role ur WHERE u.mylutece_wsso_user_id = ur.mylutece_wsso_user_id AND ur.mylutece_wsso_role_id = ? ORDER BY u.last_name, u.first_name, u.email ";
56      private static final String SQL_QUERY_SELECTALL_FOR_GUID = " SELECT mylutece_wsso_user_id, guid, last_name, first_name, email, date_last_login FROM mylutece_wsso_user WHERE guid = ? ORDER BY last_name, first_name, email ";
57  
58      /** This class implements the Singleton design pattern. */
59      private static WssoUserDAO _dao = new WssoUserDAO(  );
60  
61      /**
62       * Creates a new WssoUserDAO object.
63       */
64      private WssoUserDAO(  )
65      {
66      }
67  
68      /**
69       * Returns the unique instance of the singleton.
70       *
71       * @return the instance
72       */
73      static WssoUserDAO getInstance(  )
74      {
75          return _dao;
76      }
77  
78      /**
79       * Generates a new primary key
80       * @param plugin The Plugin using this data access service
81       * @return The new primary key
82       */
83      public int newPrimaryKey( Plugin plugin )
84      {
85          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
86          daoUtil.executeQuery(  );
87  
88          int nKey;
89  
90          if ( !daoUtil.next(  ) )
91          {
92              // if the table is empty
93              nKey = 1;
94          }
95  
96          nKey = daoUtil.getInt( 1 ) + 1;
97  
98          daoUtil.free(  );
99  
100         return nKey;
101     }
102 
103     /**
104      * Insert a new record in the table.
105      *
106      * @param wssoUser The wssoUser object
107      * @param plugin The Plugin using this data access service
108      */
109     public void insert( WssoUser wssoUser, Plugin plugin )
110     {
111         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
112         wssoUser.setMyluteceWssoUserId( newPrimaryKey( plugin ) );
113         daoUtil.setInt( 1, wssoUser.getMyluteceWssoUserId(  ) );
114         daoUtil.setString( 2, wssoUser.getGuid(  ) );
115         daoUtil.setString( 3, wssoUser.getLastName(  ) );
116         daoUtil.setString( 4, wssoUser.getFirstName(  ) );
117         daoUtil.setString( 5, wssoUser.getEmail(  ) );
118 
119         daoUtil.executeUpdate(  );
120         daoUtil.free(  );
121     }
122 
123     /**
124      * Load the data of WssoUser from the table
125      *
126      * @param nWssoUserId The identifier of WssoUser
127      * @param plugin The Plugin using this data access service
128      * @return the instance of the WssoUser
129      */
130     public WssoUser load( int nWssoUserId, Plugin plugin )
131     {
132         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
133         daoUtil.setInt( 1, nWssoUserId );
134         daoUtil.executeQuery(  );
135 
136         WssoUser wssoUser = null;
137 
138         if ( daoUtil.next(  ) )
139         {
140             wssoUser = new WssoUser(  );
141             wssoUser.setMyluteceWssoUserId( daoUtil.getInt( 1 ) );
142             wssoUser.setGuid( daoUtil.getString( 2 ) );
143             wssoUser.setLastName( daoUtil.getString( 3 ) );
144             wssoUser.setFirstName( daoUtil.getString( 4 ) );
145             wssoUser.setEmail( daoUtil.getString( 5 ) );
146             wssoUser.setDateLastLogin( daoUtil.getDate( 6 ) );
147         }
148 
149         daoUtil.free(  );
150 
151         return wssoUser;
152     }
153 
154     /**
155      * Delete a record from the table
156      * @param wssoUser The WssoUser object
157      * @param plugin The Plugin using this data access service
158      */
159     public void delete( WssoUser wssoUser, Plugin plugin )
160     {
161         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
162         daoUtil.setInt( 1, wssoUser.getMyluteceWssoUserId(  ) );
163 
164         daoUtil.executeUpdate(  );
165         daoUtil.free(  );
166     }
167 
168     /**
169      * Update the record in the table
170      * @param wssoUser The reference of wssoUser
171      * @param plugin The Plugin using this data access service
172      */
173     public void store( WssoUser wssoUser, Plugin plugin )
174     {
175         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
176         daoUtil.setInt( 1, wssoUser.getMyluteceWssoUserId(  ) );
177         daoUtil.setString( 2, wssoUser.getGuid(  ) );
178         daoUtil.setString( 3, wssoUser.getLastName(  ) );
179         daoUtil.setString( 4, wssoUser.getFirstName(  ) );
180         daoUtil.setString( 5, wssoUser.getEmail(  ) );
181         daoUtil.setInt( 6, wssoUser.getMyluteceWssoUserId(  ) );
182 
183         daoUtil.executeUpdate(  );
184         daoUtil.free(  );
185     }
186 
187     /**
188      * Load the list of wssoUsers
189      * @param plugin The Plugin using this data access service
190      * @return The Collection of the WssoUsers
191      */
192     public Collection<WssoUser> selectWssoUserList( Plugin plugin )
193     {
194         Collection<WssoUser> listWssoUsers = new ArrayList<WssoUser>(  );
195         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
196         daoUtil.executeQuery(  );
197 
198         while ( daoUtil.next(  ) )
199         {
200             WssoUser wssoUser = new WssoUser(  );
201             wssoUser.setMyluteceWssoUserId( daoUtil.getInt( 1 ) );
202             wssoUser.setGuid( daoUtil.getString( 2 ) );
203             wssoUser.setLastName( daoUtil.getString( 3 ) );
204             wssoUser.setFirstName( daoUtil.getString( 4 ) );
205             wssoUser.setEmail( daoUtil.getString( 5 ) );
206             wssoUser.setDateLastLogin( daoUtil.getDate( 6 ) );
207 
208             listWssoUsers.add( wssoUser );
209         }
210 
211         daoUtil.free(  );
212 
213         return listWssoUsers;
214     }
215 
216     /**
217      * Load the list of wssoUsers for a role
218      * @param nIdRole The role of WssoUser
219      * @param plugin The Plugin using this data access service
220      * @return The Collection of the WssoUsers
221      */
222     public Collection<WssoUser> selectWssoUsersListForRole( int nIdRole, Plugin plugin )
223     {
224         Collection<WssoUser> listWssoUsers = new ArrayList<WssoUser>(  );
225         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_FOR_ROLE, plugin );
226         daoUtil.setInt( 1, nIdRole );
227         daoUtil.executeQuery(  );
228 
229         while ( daoUtil.next(  ) )
230         {
231             WssoUser wssoUser = new WssoUser(  );
232             wssoUser.setMyluteceWssoUserId( daoUtil.getInt( 1 ) );
233             wssoUser.setGuid( daoUtil.getString( 2 ) );
234             wssoUser.setLastName( daoUtil.getString( 3 ) );
235             wssoUser.setFirstName( daoUtil.getString( 4 ) );
236             wssoUser.setEmail( daoUtil.getString( 5 ) );
237             wssoUser.setDateLastLogin( daoUtil.getDate( 6 ) );
238 
239             listWssoUsers.add( wssoUser );
240         }
241 
242         daoUtil.free(  );
243 
244         return listWssoUsers;
245     }
246 
247     /**
248      * Load the list of wssoUsers for a guid
249      * @param strGuid The guid of WssoUser
250      * @param plugin The Plugin using this data access service
251      * @return The Collection of the WssoUsers
252      */
253     public Collection<WssoUser> selectWssoUserListForGuid( String strGuid, Plugin plugin )
254     {
255         Collection<WssoUser> listWssoUsers = new ArrayList<WssoUser>(  );
256         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_FOR_GUID, plugin );
257         daoUtil.setString( 1, strGuid );
258         daoUtil.executeQuery(  );
259 
260         while ( daoUtil.next(  ) )
261         {
262             WssoUser wssoUser = new WssoUser(  );
263             wssoUser.setMyluteceWssoUserId( daoUtil.getInt( 1 ) );
264             wssoUser.setGuid( daoUtil.getString( 2 ) );
265             wssoUser.setLastName( daoUtil.getString( 3 ) );
266             wssoUser.setFirstName( daoUtil.getString( 4 ) );
267             wssoUser.setEmail( daoUtil.getString( 5 ) );
268             wssoUser.setDateLastLogin( daoUtil.getDate( 6 ) );
269 
270             listWssoUsers.add( wssoUser );
271         }
272 
273         daoUtil.free(  );
274 
275         return listWssoUsers;
276     }
277 }