View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.plugins.mylutece.modules.users.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.ReferenceList;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  import java.sql.Statement;
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * This class provides Data Access methods for LocalUser objects
45   */
46  public final class LocalUserDAO implements ILocalUserDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_SELECT = "SELECT connect_id, login, given_name, last_name, email, connect_id_provider FROM mylutece_users_localuser WHERE connect_id = ?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO mylutece_users_localuser ( login, given_name, last_name, email, connect_id_provider ) VALUES ( ?, ?, ?, ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM mylutece_users_localuser WHERE connect_id = ? ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE mylutece_users_localuser SET connect_id = ?, login = ?, given_name = ?, last_name = ?, email = ?, connect_id_provider = ? WHERE connect_id = ?";
53      private static final String SQL_QUERY_SELECTALL = "SELECT connect_id, login, given_name, last_name, email, connect_id_provider FROM mylutece_users_localuser";
54      private static final String SQL_QUERY_SELECTALL_ID = "SELECT connect_id FROM mylutece_users_localuser";
55      private static final String SQL_QUERY_SELECT_BY_CONNECT_ID = "SELECT connect_id, login, given_name, last_name, email, connect_id_provider FROM mylutece_users_localuser WHERE connect_id_provider = ?";
56  
57      /**
58       * {@inheritDoc }
59       */
60      @Override
61      public void insert( LocalUser localUser, Plugin plugin )
62      {
63          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
64          {
65              int nIndex = 1;
66              daoUtil.setString( nIndex++, localUser.getLogin( ) );
67              daoUtil.setString( nIndex++, localUser.getGivenName( ) );
68              daoUtil.setString( nIndex++, localUser.getLastName( ) );
69              daoUtil.setString( nIndex++, localUser.getEmail( ) );
70              daoUtil.setString( nIndex++, localUser.getProviderUserId( ) );
71              daoUtil.executeUpdate( );
72              if ( daoUtil.nextGeneratedKey( ) )
73              {
74                  localUser.setId( daoUtil.getGeneratedKeyInt( 1 ) );
75              }
76          }
77      }
78  
79      /**
80       * {@inheritDoc }
81       */
82      @Override
83      public LocalUser load( int nKey, Plugin plugin )
84      {
85          LocalUser localUser = null;
86          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
87          {
88              daoUtil.setInt( 1, nKey );
89              daoUtil.executeQuery( );
90              if ( daoUtil.next( ) )
91              {
92                  localUser = new LocalUser( );
93                  int nIndex = 1;
94                  localUser.setId( daoUtil.getInt( nIndex++ ) );
95                  localUser.setLogin( daoUtil.getString( nIndex++ ) );
96                  localUser.setGivenName( daoUtil.getString( nIndex++ ) );
97                  localUser.setLastName( daoUtil.getString( nIndex++ ) );
98                  localUser.setEmail( daoUtil.getString( nIndex++ ) );
99                  localUser.setProviderUserId( daoUtil.getString( nIndex++ ) );
100             }
101         }
102         return localUser;
103     }
104 
105     /**
106      * {@inheritDoc }
107      */
108     @Override
109     public void delete( int nKey, Plugin plugin )
110     {
111         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
112         {
113             daoUtil.setInt( 1, nKey );
114             daoUtil.executeUpdate( );
115         }
116     }
117 
118     /**
119      * {@inheritDoc }
120      */
121     @Override
122     public void store( LocalUser localUser, Plugin plugin )
123     {
124         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
125         {
126             int nIndex = 1;
127             daoUtil.setInt( nIndex++, localUser.getId( ) );
128             daoUtil.setString( nIndex++, localUser.getLogin( ) );
129             daoUtil.setString( nIndex++, localUser.getGivenName( ) );
130             daoUtil.setString( nIndex++, localUser.getLastName( ) );
131             daoUtil.setString( nIndex++, localUser.getEmail( ) );
132             daoUtil.setString( nIndex++, localUser.getProviderUserId( ) );
133             daoUtil.setInt( nIndex, localUser.getId( ) );
134             daoUtil.executeUpdate( );
135         }
136     }
137 
138     /**
139      * {@inheritDoc }
140      */
141     @Override
142     public List<LocalUser> selectLocalUsersList( Plugin plugin )
143     {
144         List<LocalUser> localUserList = new ArrayList<>( );
145         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
146         {
147             daoUtil.executeQuery( );
148             while ( daoUtil.next( ) )
149             {
150                 LocalUsers/mylutece/modules/users/business/LocalUser.html#LocalUser">LocalUser localUser = new LocalUser( );
151                 int nIndex = 1;
152                 localUser.setId( daoUtil.getInt( nIndex++ ) );
153                 localUser.setLogin( daoUtil.getString( nIndex++ ) );
154                 localUser.setGivenName( daoUtil.getString( nIndex++ ) );
155                 localUser.setLastName( daoUtil.getString( nIndex++ ) );
156                 localUser.setEmail( daoUtil.getString( nIndex++ ) );
157                 localUser.setProviderUserId( daoUtil.getString( nIndex++ ) );
158                 localUserList.add( localUser );
159             }
160         }
161         return localUserList;
162     }
163 
164     /**
165      * {@inheritDoc }
166      */
167     @Override
168     public List<Integer> selectIdLocalUsersList( Plugin plugin )
169     {
170         List<Integer> localUserList = new ArrayList<>( );
171         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin ) )
172         {
173             daoUtil.executeQuery( );
174             while ( daoUtil.next( ) )
175             {
176                 localUserList.add( daoUtil.getInt( 1 ) );
177             }
178         }
179         return localUserList;
180     }
181 
182     /**
183      * {@inheritDoc }
184      */
185     @Override
186     public ReferenceList selectLocalUsersReferenceList( Plugin plugin )
187     {
188         ReferenceList localUserList = new ReferenceList( );
189         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
190         {
191             daoUtil.executeQuery( );
192             while ( daoUtil.next( ) )
193             {
194                 localUserList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
195             }
196         }
197         return localUserList;
198     }
199 
200     /**
201      * {@inheritDoc }
202      */
203     @Override
204     public LocalUser loadByConnectId( String strUserName, Plugin plugin )
205     {
206 
207         LocalUser localUser = null;
208         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_CONNECT_ID, plugin ) )
209         {
210             daoUtil.setString( 1, strUserName );
211             daoUtil.executeQuery( );
212             if ( daoUtil.next( ) )
213             {
214                 localUser = new LocalUser( );
215                 int nIndex = 1;
216                 localUser.setId( daoUtil.getInt( nIndex++ ) );
217                 localUser.setLogin( daoUtil.getString( nIndex++ ) );
218                 localUser.setGivenName( daoUtil.getString( nIndex++ ) );
219                 localUser.setLastName( daoUtil.getString( nIndex++ ) );
220                 localUser.setEmail( daoUtil.getString( nIndex++ ) );
221                 localUser.setProviderUserId( daoUtil.getString( nIndex++ ) );
222             }
223         }
224         return localUser;
225     }
226 }