1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
45
46 public final class LocalUserDAO implements ILocalUserDAO
47 {
48
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
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
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
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
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
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
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
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
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 }