View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.ldapdatabase.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 ldapUser objects
45   */
46  public final class LdapUserDAO implements ILdapUserDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = " SELECT max( mylutece_ldapdatabase_user_id ) FROM mylutece_ldapdatabase_user ";
50      private static final String SQL_QUERY_SELECT = " SELECT mylutece_ldapdatabase_user_id, ldap_guid, name_family, name_given, email FROM mylutece_ldapdatabase_user WHERE mylutece_ldapdatabase_user_id = ? ";
51      private static final String SQL_QUERY_INSERT = " INSERT INTO mylutece_ldapdatabase_user ( mylutece_ldapdatabase_user_id, ldap_guid, name_family, name_given, email ) VALUES ( ?, ?, ?, ?, ? ) ";
52      private static final String SQL_QUERY_DELETE = " DELETE FROM mylutece_ldapdatabase_user WHERE mylutece_ldapdatabase_user_id = ?  ";
53      private static final String SQL_QUERY_UPDATE = " UPDATE mylutece_ldapdatabase_user SET mylutece_ldapdatabase_user_id = ?, ldap_guid = ? name_family = ?, name_given = ?, email = ? WHERE mylutece_ldapdatabase_user_id = ?  ";
54      private static final String SQL_QUERY_SELECTALL = " SELECT mylutece_ldapdatabase_user_id, ldap_guid, name_family, name_given, email FROM mylutece_ldapdatabase_user ";
55      private static final String SQL_QUERY_SELECTALL_FOR_LDAP_GUID = " SELECT mylutece_ldapdatabase_user_id, ldap_guid, name_family, name_given, email FROM mylutece_ldapdatabase_user WHERE ldap_guid = ? ";
56  
57      /** This class implements the Singleton design pattern. */
58      private static LdapUserDAO _dao = new LdapUserDAO(  );
59  
60      /**
61       * Creates a new ldapUserDAO object.
62       */
63      private LdapUserDAO(  )
64      {
65      }
66  
67      /**
68       * Returns the unique instance of the singleton.
69       *
70       * @return the instance
71       */
72      static LdapUserDAO getInstance(  )
73      {
74          return _dao;
75      }
76  
77      /**
78       * Generates a new primary key
79       * @param plugin The Plugin using this data access service
80       * @return The new primary key
81       */
82      public int newPrimaryKey( Plugin plugin )
83      {
84          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
85          daoUtil.executeQuery(  );
86  
87          int nKey;
88  
89          if ( !daoUtil.next(  ) )
90          {
91              // if the table is empty
92              nKey = 1;
93          }
94  
95          nKey = daoUtil.getInt( 1 ) + 1;
96  
97          daoUtil.free(  );
98  
99          return nKey;
100     }
101 
102     /**
103      * Insert a new record in the table.
104      *
105      * @param ldapUser The ldapUser object
106      * @param plugin The Plugin using this data access service
107      */
108     public void insert( LdapUser ldapUser, Plugin plugin )
109     {
110         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
111         ldapUser.setUserId( newPrimaryKey( plugin ) );
112         daoUtil.setInt( 1, ldapUser.getUserId(  ) );
113         daoUtil.setString( 2, ldapUser.getLdapGuid(  ) );
114         daoUtil.setString( 3, ldapUser.getLastName(  ) );
115         daoUtil.setString( 4, ldapUser.getFirstName(  ) );
116         daoUtil.setString( 5, ldapUser.getEmail(  ) );
117 
118         daoUtil.executeUpdate(  );
119         daoUtil.free(  );
120     }
121 
122     /**
123      * Load the data of LdapUser from the table
124      *
125      * @param nUserId The identifier of User
126      * @param plugin The Plugin using this data access service
127      * @return the instance of the LdapUser
128      */
129     public LdapUser load( int nUserId, Plugin plugin )
130     {
131         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
132         daoUtil.setInt( 1, nUserId );
133         daoUtil.executeQuery(  );
134 
135         LdapUser ldapUser = null;
136 
137         if ( daoUtil.next(  ) )
138         {
139             ldapUser = new LdapUser(  );
140             ldapUser.setUserId( daoUtil.getInt( 1 ) );
141             ldapUser.setLdapGuid( daoUtil.getString( 2 ) );
142             ldapUser.setLastName( daoUtil.getString( 3 ) );
143             ldapUser.setFirstName( daoUtil.getString( 4 ) );
144             ldapUser.setEmail( daoUtil.getString( 5 ) );
145         }
146 
147         daoUtil.free(  );
148 
149         return ldapUser;
150     }
151 
152     /**
153      * Delete a record from the table
154      * @param ldapUser The LdapUser object
155      * @param plugin The Plugin using this data access service
156      */
157     public void delete( LdapUser ldapUser, Plugin plugin )
158     {
159         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
160         daoUtil.setInt( 1, ldapUser.getUserId(  ) );
161 
162         daoUtil.executeUpdate(  );
163         daoUtil.free(  );
164     }
165 
166     /**
167      * Update the record in the table
168      * @param ldapUser The reference of ldapUser
169      * @param plugin The Plugin using this data access service
170      */
171     public void store( LdapUser ldapUser, Plugin plugin )
172     {
173         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
174         daoUtil.setInt( 1, ldapUser.getUserId(  ) );
175         daoUtil.setString( 2, ldapUser.getLdapGuid(  ) );
176         daoUtil.setString( 3, ldapUser.getLastName(  ) );
177         daoUtil.setString( 4, ldapUser.getFirstName(  ) );
178         daoUtil.setString( 5, ldapUser.getEmail(  ) );
179         daoUtil.setInt( 6, ldapUser.getUserId(  ) );
180 
181         daoUtil.executeUpdate(  );
182         daoUtil.free(  );
183     }
184 
185     /**
186      * Load the list of ldapUsers
187      * @param plugin The Plugin using this data access service
188      * @return The Collection of the ldapUsers
189      */
190     public Collection<LdapUser> selectLdapUserList( Plugin plugin )
191     {
192         Collection<LdapUser> listLdapUsers = new ArrayList<LdapUser>(  );
193         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
194         daoUtil.executeQuery(  );
195 
196         while ( daoUtil.next(  ) )
197         {
198             LdapUser ldapUser = new LdapUser(  );
199             ldapUser.setUserId( daoUtil.getInt( 1 ) );
200             ldapUser.setLdapGuid( daoUtil.getString( 2 ) );
201             ldapUser.setLastName( daoUtil.getString( 3 ) );
202             ldapUser.setFirstName( daoUtil.getString( 4 ) );
203             ldapUser.setEmail( daoUtil.getString( 5 ) );
204 
205             listLdapUsers.add( ldapUser );
206         }
207 
208         daoUtil.free(  );
209 
210         return listLdapUsers;
211     }
212 
213     /**
214      * Load the list of ldapUsers for a ldap guid
215      * @param strGuid The guid of ldapUser
216      * @param plugin The Plugin using this data access service
217      * @return The Collection of the ldapUsers
218      */
219     public Collection<LdapUser> selectLdapUserListForGuid( String strGuid, Plugin plugin )
220     {
221         Collection<LdapUser> listLdapUsers = new ArrayList<LdapUser>(  );
222         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_FOR_LDAP_GUID, plugin );
223         daoUtil.setString( 1, strGuid );
224         daoUtil.executeQuery(  );
225 
226         while ( daoUtil.next(  ) )
227         {
228             LdapUser ldapUser = new LdapUser(  );
229             ldapUser.setUserId( daoUtil.getInt( 1 ) );
230             ldapUser.setLdapGuid( daoUtil.getString( 2 ) );
231             ldapUser.setLastName( daoUtil.getString( 3 ) );
232             ldapUser.setFirstName( daoUtil.getString( 4 ) );
233             ldapUser.setEmail( daoUtil.getString( 5 ) );
234 
235             listLdapUsers.add( ldapUser );
236         }
237 
238         daoUtil.free(  );
239 
240         return listLdapUsers;
241     }
242 }