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.List;
41  
42  
43  /**
44   * This class provides Data Access methods for WssoProfil objects
45   */
46  public final class WssoProfilDAO implements IWssoProfilDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_INSERT = " INSERT INTO mylutece_wsso_profil ( code, description ) VALUES ( ?, ? ) ";
50      private static final String SQL_QUERY_DELETE = " DELETE FROM mylutece_wsso_profil WHERE code = ?  ";
51      private static final String SQL_QUERY_UPDATE_DESCRIPTION = " UPDATE mylutece_wsso_profil SET description = ? WHERE code = ?  ";
52      private static final String SQL_QUERY_FIND_ALL = " SELECT code, description FROM mylutece_wsso_profil ORDER BY code, description";
53      private static final String SQL_QUERY_FIND_BY_CODE_AND_DESCRIPTION = " SELECT code, description FROM mylutece_wsso_profil WHERE code = ? and description = ?";
54      private static final String SQL_QUERY_FIND_BY_DESCRIPTION = " SELECT code, description FROM mylutece_wsso_profil WHERE description = ? ";
55      private static final String SQL_QUERY_FIND_BY_CODE = " SELECT code, description FROM mylutece_wsso_profil WHERE code = ? ";
56      private static final String SQL_SELECT_WSSO_PROFILS_CODE_FROM_PASSWORD = "SELECT b.mylutece_wsso_profil_code FROM mylutece_wsso_user a INNER JOIN mylutece_wsso_profil_user b on a.mylutece_wsso_user_id = b.mylutece_wsso_user_id AND b.mylutece_wsso_user_id = ? ";
57      private static final String SQL_QUERY_CHECK_PROFIL_ASSIGNED_TO_USER = " SELECT count(*) FROM mylutece_wsso_profil_user WHERE mylutece_wsso_profil_code = ?";
58  
59      /** This class implements the Singleton design pattern. */
60      private static WssoProfilDAO _dao = new WssoProfilDAO(  );
61  
62      /**
63       * Creates a new WssoUserDAO object.
64       */
65      private WssoProfilDAO(  )
66      {
67      }
68  
69      /**
70       * Returns the unique instance of the singleton.
71       *
72       * @return the instance
73       */
74      static WssoProfilDAO getInstance(  )
75      {
76          return _dao;
77      }
78  
79      /**
80       * Insert a new record in the table.
81       *
82       * @param wssoProfil The wssoProfil object
83       * @param plugin The Plugin using this data access service
84       */
85      public void insert( WssoProfil wssoProfil, Plugin plugin )
86      {
87          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
88          daoUtil.setString( 1, wssoProfil.getCode(  ) );
89          daoUtil.setString( 2, wssoProfil.getDescription(  ) );
90  
91          daoUtil.executeUpdate(  );
92          daoUtil.free(  );
93      }
94  
95      //	/**
96      //	 * Load the data of WssoProfil from the table
97      //	 * 
98      //	 * @param nWssoProfilId The identifier of WssoUser
99      //	 * @param plugin The Plugin using this data access service
100     //	 * @return the instance of the WssoUser
101     //	 */
102     //	public WssoProfil load( int nWssoProfilId, Plugin plugin )
103     //	{
104     //		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_ID, plugin );
105     //		daoUtil.setInt( 1, nWssoProfilId );
106     //		daoUtil.executeQuery( );
107     //
108     //		WssoProfil wssoProfil = null;
109     //
110     //		if ( daoUtil.next( ) )
111     //		{
112     //			wssoProfil = new WssoProfil( );
113     //			wssoProfil.setMyluteceWssoProfilId( daoUtil.getInt( 1 ) );
114     //			wssoProfil.setCode(daoUtil.getString( 2 ));
115     //			wssoProfil.setDescription(daoUtil.getString( 3 ));
116     //		}
117     //
118     //		daoUtil.free( );
119     //
120     //		return wssoProfil;
121     //	}
122 
123     /**
124      * Delete a record from the table
125      * @param wssoProfil The WssoProfil object
126      * @param plugin The Plugin using this data access service
127      */
128     public void delete( WssoProfil wssoProfil, Plugin plugin )
129     {
130         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
131         daoUtil.setString( 1, wssoProfil.getCode(  ) );
132 
133         daoUtil.executeUpdate(  );
134         daoUtil.free(  );
135     }
136 
137     /**
138      * Update the record in the table
139      * @param wssoProfil The reference of wssoProfil
140      * @param plugin The Plugin using this data access service
141      */
142     public void store( WssoProfil wssoProfil, Plugin plugin )
143     {
144         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_DESCRIPTION, plugin );
145         daoUtil.setString( 1, wssoProfil.getDescription(  ) );
146         daoUtil.setString( 2, wssoProfil.getCode(  ) );
147 
148         daoUtil.executeUpdate(  );
149         daoUtil.free(  );
150     }
151 
152     /**
153      * Load the list of wssoProfils
154      * @param plugin The Plugin using this data access service
155      * @return The Collection of the WssoProfils
156      */
157     public List<WssoProfil> selectWssoProfilList( Plugin plugin )
158     {
159         List<WssoProfil> listWssoProfils = new ArrayList<WssoProfil>(  );
160         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_ALL, plugin );
161         daoUtil.executeQuery(  );
162 
163         while ( daoUtil.next(  ) )
164         {
165             WssoProfil wssoProfil = new WssoProfil(  );
166             wssoProfil.setCode( daoUtil.getString( 1 ) );
167             wssoProfil.setDescription( daoUtil.getString( 2 ) );
168 
169             listWssoProfils.add( wssoProfil );
170         }
171 
172         daoUtil.free(  );
173 
174         return listWssoProfils;
175     }
176 
177     //	/**
178     //	 * Load the list of wssoUsers for a role
179     //	 * @param nIdRole The role of WssoUser
180     //	 * @param plugin The Plugin using this data access service
181     //	 * @return The Collection of the WssoUsers
182     //	 */
183     //	public Collection<WssoUser> selectWssoUsersListForRole( int nIdRole, Plugin plugin )
184     //	{
185     //		Collection<WssoUser> listWssoUsers = new ArrayList<WssoUser>( );
186     //		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_FOR_ROLE, plugin );
187     //		daoUtil.setInt( 1, nIdRole );
188     //		daoUtil.executeQuery( );
189     //
190     //		while ( daoUtil.next( ) )
191     //		{
192     //			WssoUser wssoUser = new WssoUser( );
193     //			wssoUser.setMyluteceWssoUserId( daoUtil.getInt( 1 ) );
194     //			wssoUser.setGuid( daoUtil.getString( 2 ) );
195     //			wssoUser.setLastName( daoUtil.getString( 3 ) );
196     //			wssoUser.setFirstName( daoUtil.getString( 4 ) );
197     //			wssoUser.setEmail( daoUtil.getString( 5 ) );
198     //			wssoUser.setDateLastLogin( daoUtil.getDate( 6 ) );
199     //
200     //			listWssoUsers.add( wssoUser );
201     //		}
202     //
203     //		daoUtil.free( );
204     //
205     //		return listWssoUsers;
206     //	}
207 
208     /**
209      * Find the data of WssoProfil from the table
210      *
211      * @param strCode The code of WssoProfil
212      * @param plugin The Plugin using this data access service
213      * @return the instance of the WssoProfil
214      */
215     public WssoProfil load( String strCode, Plugin plugin )
216     {
217         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_CODE, plugin );
218         daoUtil.setString( 1, strCode );
219         daoUtil.executeQuery(  );
220 
221         WssoProfil wssoProfil = null;
222 
223         if ( daoUtil.next(  ) )
224         {
225             wssoProfil = new WssoProfil(  );
226             wssoProfil.setCode( daoUtil.getString( 1 ) );
227             wssoProfil.setDescription( daoUtil.getString( 2 ) );
228         }
229 
230         daoUtil.free(  );
231 
232         return wssoProfil;
233     }
234 
235     /**
236      *
237      * {@inheritDoc}
238      */
239     @Override
240     public WssoProfil findWssoProfilByCodeAndDescription( String strCode, String strDescription, Plugin plugin )
241     {
242         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_CODE_AND_DESCRIPTION, plugin );
243         daoUtil.setString( 1, strCode );
244         daoUtil.setString( 2, strDescription );
245         daoUtil.executeQuery(  );
246 
247         WssoProfil wssoProfil = null;
248 
249         while ( daoUtil.next(  ) )
250         {
251             wssoProfil = new WssoProfil(  );
252             wssoProfil.setCode( daoUtil.getString( 1 ) );
253             wssoProfil.setDescription( daoUtil.getString( 2 ) );
254         }
255 
256         daoUtil.free(  );
257 
258         return wssoProfil;
259     }
260 
261     /**
262      *
263      * {@inheritDoc}
264      */
265     @Override
266     public List<WssoProfil> findWssoProfilsByDescription( String strDescription, Plugin plugin )
267     {
268         List<WssoProfil> listWssoProfils = new ArrayList<WssoProfil>(  );
269         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_DESCRIPTION, plugin );
270         daoUtil.setString( 1, strDescription );
271         daoUtil.executeQuery(  );
272 
273         while ( daoUtil.next(  ) )
274         {
275             WssoProfil wssoProfil = new WssoProfil(  );
276             wssoProfil.setCode( daoUtil.getString( 1 ) );
277             wssoProfil.setDescription( daoUtil.getString( 2 ) );
278 
279             listWssoProfils.add( wssoProfil );
280         }
281 
282         daoUtil.free(  );
283 
284         return listWssoProfils;
285     }
286 
287     /**
288      *
289      * {@inheritDoc}
290      */
291     @Override
292     public List<String> findWssoProfilsForUser( int nWssoUserId, Plugin plugin )
293     {
294         List<String> listCodeProfils = new ArrayList<String>(  );
295         DAOUtil daoUtil = new DAOUtil( SQL_SELECT_WSSO_PROFILS_CODE_FROM_PASSWORD, plugin );
296         daoUtil.setInt( 1, nWssoUserId );
297         daoUtil.executeQuery(  );
298 
299         while ( daoUtil.next(  ) )
300         {
301             listCodeProfils.add( daoUtil.getString( 1 ) );
302         }
303 
304         daoUtil.free(  );
305 
306         return listCodeProfils;
307     }
308 
309     /**
310      *
311      * {@inheritDoc}
312      */
313     @Override
314     public boolean checkProfilAssigned( String strCode, Plugin plugin )
315     {
316         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PROFIL_ASSIGNED_TO_USER, plugin );
317         daoUtil.setString( 1, strCode );
318         daoUtil.executeQuery(  );
319 
320         WssoProfil wssoProfil = null;
321 
322         int nbrAssignation = 0;
323 
324         while ( daoUtil.next(  ) )
325         {
326             nbrAssignation = daoUtil.getInt( 1 );
327         }
328 
329         daoUtil.free(  );
330 
331         if ( nbrAssignation > 0 )
332         {
333             return true;
334         }
335 
336         return false;
337     }
338 }