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.identityimport.business;
35
36 import fr.paris.lutece.plugins.workflowcore.business.action.Action;
37 import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
38 import fr.paris.lutece.portal.service.plugin.Plugin;
39 import fr.paris.lutece.util.sql.DAOUtil;
40 import org.apache.commons.collections4.CollectionUtils;
41 import org.apache.commons.lang3.StringUtils;
42
43 import java.sql.Statement;
44 import java.util.ArrayList;
45 import java.util.List;
46 import java.util.Optional;
47 import java.util.stream.Collectors;
48
49
50
51
52 public final class CandidateIdentityDAO implements ICandidateIdentityDAO
53 {
54
55 private static final String ID_LIST = "%{id_list}";
56 private static final String SQL_QUERY_SELECT_ALL = "WITH filtered_date AS (SELECT max(rh.creation_date) AS creation_date, rh.id_resource FROM workflow_resource_history rh GROUP BY rh.id_resource), "
57 + " filtered_history AS (SELECT rh.id_resource, icih.status, icih.comment, rh.creation_date FROM workflow_resource_history rh LEFT JOIN identityimport_candidate_identity_history icih ON icih.id_wf_resource_history = rh.id_history JOIN filtered_date ON filtered_date.id_resource = rh.id_resource AND filtered_date.creation_date = rh.creation_date) "
58 + " SELECT i.id_candidate_identity, i.id_batch, i.connection_id, i.customer_id, i.client_id, ib.client_code, filtered_history.status, filtered_history.comment FROM identityimport_candidate_identity i JOIN identityimport_batch ib ON i.id_batch = ib.id_batch LEFT JOIN filtered_history ON filtered_history.id_resource = i.id_candidate_identity ";
59 private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECT_ALL + " WHERE i.id_candidate_identity = ?";
60 private static final String SQL_QUERY_INSERT = "INSERT INTO identityimport_candidate_identity ( id_batch, connection_id, customer_id, client_id) VALUES ( ?, ?, ?, ?) ";
61 private static final String SQL_QUERY_DELETE = "DELETE FROM identityimport_candidate_identity WHERE id_candidate_identity = ? ";
62 private static final String SQL_QUERY_DELETE_LISTS = "DELETE FROM identityimport_candidate_identity WHERE id_candidate_identity IN ( " + ID_LIST + " )";
63 private static final String SQL_QUERY_UPDATE = "UPDATE identityimport_candidate_identity SET id_candidate_identity = ?, id_batch = ?, connection_id = ?, customer_id = ?, client_id = ? WHERE id_candidate_identity = ?";
64 private static final String SQL_QUERY_SELECTALL_ID = "SELECT i.id_candidate_identity FROM identityimport_candidate_identity i JOIN workflow_resource_workflow r on r.id_resource = i.id_candidate_identity AND r.resource_type = 'IDENTITYIMPORT_CANDIDATE_RESOURCE' WHERE i.id_batch = ? ORDER BY FIELD(r.id_state, 4, 7, 5, 6, 8, 9)";
65 private static final String SQL_QUERY_SELECTALL_ID_BY_STATE = "SELECT i.id_candidate_identity FROM identityimport_candidate_identity i JOIN workflow_resource_workflow r on r.id_resource = i.id_candidate_identity AND r.resource_type = 'IDENTITYIMPORT_CANDIDATE_RESOURCE' WHERE i.id_batch = ? AND r.id_state = ?";
66 private static final String SQL_QUERY_SELECTALL_BY_IDS = SQL_QUERY_SELECT_ALL + " WHERE i.id_candidate_identity IN ( ";
67 private static final String EXTERNAL_IDS = "{external_ids}";
68 private static final String SQL_QUERY_EXISTS_BY_IDS = "SELECT EXISTS(SELECT 1 FROM identityimport_candidate_identity ci JOIN identityimport_batch b ON ci.id_batch = b.id_batch WHERE b.reference = ? AND ci.client_id IN ("
69 + EXTERNAL_IDS + "))";
70 private static final String SQL_QUERY_SELECTALL_BY_IDS_END = ")";
71
72 private static final String SQL_QUERY_SELECTSTATES = "SELECT ws.id_state, ws.name, ws.description, COUNT(wr.id_resource) as batch_count FROM workflow_state ws LEFT JOIN workflow_resource_workflow wr ON wr.id_state = ws.id_state AND wr.resource_type = 'IDENTITYIMPORT_CANDIDATE_RESOURCE' AND wr.id_external_parent = ? WHERE ws.id_workflow = 2 GROUP BY ws.id_state, ws.name, ws.description";
73 private static final String SQL_QUERY_SELECTSTATE_BY_IDENTITY_ID = "SELECT ws.id_state, ws.name, ws.description, 1 as identity_count FROM workflow_resource_workflow wr LEFT JOIN workflow_state ws ON ws.id_state = wr.id_state LEFT JOIN identityimport_candidate_identity b ON b.id_candidate_identity = wr.id_resource WHERE ws.id_workflow = 2 AND wr.resource_type = 'IDENTITYIMPORT_CANDIDATE_RESOURCE' AND b.id_candidate_identity = ?";
74 private static final String SQL_QUERY_SELECT_HISTORY = "SELECT h.id_history, a.name, a.description, h.creation_date, h.user_access_code FROM workflow_resource_history h JOIN workflow_action a ON a.id_action = h.id_action WHERE h.resource_type = 'IDENTITYIMPORT_CANDIDATE_RESOURCE' AND h.id_resource = ?";
75
76
77
78
79 @Override
80 public void insert( CandidateIdentity candidateIdentity, Plugin plugin )
81 {
82 try ( final DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
83 {
84 int nIndex = 1;
85 daoUtil.setInt( nIndex++, candidateIdentity.getIdBatch( ) );
86 daoUtil.setString( nIndex++, candidateIdentity.getConnectionId( ) );
87 daoUtil.setString( nIndex++, candidateIdentity.getCustomerId( ) );
88 daoUtil.setString( nIndex, candidateIdentity.getExternalCustomerId( ) );
89
90 daoUtil.executeUpdate( );
91 if ( daoUtil.nextGeneratedKey( ) )
92 {
93 candidateIdentity.setId( daoUtil.getGeneratedKeyInt( 1 ) );
94 }
95 }
96
97 }
98
99
100
101
102 @Override
103 public Optional<CandidateIdentity> load( int nKey, Plugin plugin )
104 {
105 try ( final DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
106 {
107 daoUtil.setInt( 1, nKey );
108 daoUtil.executeQuery( );
109 CandidateIdentity candidateIdentity = null;
110
111 if ( daoUtil.next( ) )
112 {
113 candidateIdentity = this.getCandidateIdentity( daoUtil );
114 }
115
116 return Optional.ofNullable( candidateIdentity );
117 }
118 }
119
120
121
122
123 @Override
124 public void delete( int nKey, Plugin plugin )
125 {
126 try ( final DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
127 {
128 daoUtil.setInt( 1, nKey );
129 daoUtil.executeUpdate( );
130 }
131 }
132
133
134
135
136 @Override
137 public void deleteList( final List<Integer> idList, Plugin plugin )
138 {
139 if ( CollectionUtils.isNotEmpty( idList ) )
140 {
141 final String query = SQL_QUERY_DELETE_LISTS.replace( ID_LIST, idList.stream( ).map( String::valueOf ).collect( Collectors.joining( "," ) ) );
142 try ( final DAOUtil daoUtil = new DAOUtil( query, plugin ) )
143 {
144 daoUtil.executeUpdate( );
145 }
146 }
147 }
148
149
150
151
152 @Override
153 public void store( CandidateIdentity candidateIdentity, Plugin plugin )
154 {
155 try ( final DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
156 {
157 int nIndex = 1;
158
159 daoUtil.setInt( nIndex++, candidateIdentity.getId( ) );
160 daoUtil.setInt( nIndex++, candidateIdentity.getIdBatch( ) );
161 daoUtil.setString( nIndex++, candidateIdentity.getConnectionId( ) );
162 daoUtil.setString( nIndex++, candidateIdentity.getCustomerId( ) );
163 daoUtil.setString( nIndex++, candidateIdentity.getExternalCustomerId( ) );
164 daoUtil.setInt( nIndex, candidateIdentity.getId( ) );
165
166 daoUtil.executeUpdate( );
167 }
168 }
169
170
171
172
173 @Override
174 public List<CandidateIdentity> selectCandidateIdentitiesList( Plugin plugin )
175 {
176 List<CandidateIdentity> candidateIdentityList = new ArrayList<>( );
177 try ( final DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin ) )
178 {
179 daoUtil.executeQuery( );
180
181 while ( daoUtil.next( ) )
182 {
183 candidateIdentityList.add( this.getCandidateIdentity( daoUtil ) );
184 }
185
186 return candidateIdentityList;
187 }
188 }
189
190
191
192
193 @Override
194 public List<Integer> selectIdCandidateIdentitiesList( int nBatchId, Integer stateId, Plugin plugin )
195 {
196 final List<Integer> candidateIdentityList = new ArrayList<>( );
197
198 try ( final DAOUtil daoUtil = new DAOUtil( stateId != null ? SQL_QUERY_SELECTALL_ID_BY_STATE : SQL_QUERY_SELECTALL_ID, plugin ) )
199 {
200 daoUtil.setInt( 1, nBatchId );
201 if( stateId != null )
202 {
203 daoUtil.setInt( 2, stateId );
204 }
205 daoUtil.executeQuery( );
206
207 while ( daoUtil.next( ) )
208 {
209 candidateIdentityList.add( daoUtil.getInt( 1 ) );
210 }
211
212 return candidateIdentityList;
213 }
214 }
215
216
217
218
219 @Override
220 public List<CandidateIdentity> selectCandidateIdentitiesListByIds( Plugin plugin, List<Integer> listIds )
221 {
222 final List<CandidateIdentity> candidateIdentityList = new ArrayList<>( );
223
224 StringBuilder builder = new StringBuilder( );
225
226 if ( !listIds.isEmpty( ) )
227 {
228 for ( int i = 0; i < listIds.size( ); i++ )
229 {
230 builder.append( "?," );
231 }
232
233 String placeHolders = builder.deleteCharAt( builder.length( ) - 1 ).toString( );
234 String stmt = SQL_QUERY_SELECTALL_BY_IDS + placeHolders + SQL_QUERY_SELECTALL_BY_IDS_END;
235
236 try ( final DAOUtil daoUtil = new DAOUtil( stmt, plugin ) )
237 {
238 int index = 1;
239 for ( Integer n : listIds )
240 {
241 daoUtil.setInt( index++, n );
242 }
243
244 daoUtil.executeQuery( );
245 while ( daoUtil.next( ) )
246 {
247 candidateIdentityList.add( this.getCandidateIdentity( daoUtil ) );
248 }
249
250 daoUtil.free( );
251
252 }
253 }
254 return candidateIdentityList;
255
256 }
257
258 private CandidateIdentity getCandidateIdentity( DAOUtil daoUtil )
259 {
260 CandidateIdentityess/CandidateIdentity.html#CandidateIdentity">CandidateIdentity candidateIdentity = new CandidateIdentity( );
261 int nIndex = 1;
262
263 candidateIdentity.setId( daoUtil.getInt( nIndex++ ) );
264 candidateIdentity.setIdBatch( daoUtil.getInt( nIndex++ ) );
265 candidateIdentity.setConnectionId( daoUtil.getString( nIndex++ ) );
266 candidateIdentity.setCustomerId( daoUtil.getString( nIndex++ ) );
267 candidateIdentity.setExternalCustomerId( daoUtil.getString( nIndex++ ) );
268 candidateIdentity.setClientCode( daoUtil.getString( nIndex++ ) );
269 candidateIdentity.setStatus( StringUtils.defaultString( daoUtil.getString( nIndex ), "" ) );
270 return candidateIdentity;
271 }
272
273 @Override
274 public boolean checkIfOneExists( final Plugin plugin, final String batchReference, List<String> externalIds )
275 {
276 if ( !externalIds.isEmpty( ) )
277 {
278 final String query = SQL_QUERY_EXISTS_BY_IDS.replace( EXTERNAL_IDS,
279 externalIds.stream( ).map( s -> "'" + s + "'" ).collect( Collectors.joining( "," ) ) );
280 try ( final DAOUtil daoUtil = new DAOUtil( query, plugin ) )
281 {
282 daoUtil.setString( 1, batchReference );
283 daoUtil.executeQuery( );
284 if ( daoUtil.next( ) )
285 {
286 return daoUtil.getBoolean( 1 );
287 }
288 }
289 }
290 return false;
291 }
292
293 @Override
294 public List<ResourceState> selectIdentityStates( final int batchId, final Plugin plugin )
295 {
296 final List<ResourceState> states = new ArrayList<>( );
297
298 try ( final DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTSTATES, plugin ) )
299 {
300 daoUtil.setInt( 1, batchId );
301 daoUtil.executeQuery( );
302
303 while ( daoUtil.next( ) )
304 {
305 final ResourceStatetityimport/business/ResourceState.html#ResourceState">ResourceState state = new ResourceState( );
306 state.setId( daoUtil.getInt( 1 ) );
307 state.setName( daoUtil.getString( 2 ) );
308 state.setDescription( daoUtil.getString( 3 ) );
309 state.setResourceCount( daoUtil.getInt( 4 ) );
310 states.add( state );
311 }
312
313 return states;
314 }
315 }
316
317 @Override
318 public ResourceState selectIdentityState( final int batchId, final Plugin plugin )
319 {
320 try ( final DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTSTATE_BY_IDENTITY_ID, plugin ) )
321 {
322 daoUtil.setInt( 1, batchId );
323 daoUtil.executeQuery( );
324
325 if ( daoUtil.next( ) )
326 {
327 final ResourceStatetityimport/business/ResourceState.html#ResourceState">ResourceState state = new ResourceState( );
328 state.setId( daoUtil.getInt( 1 ) );
329 state.setName( daoUtil.getString( 2 ) );
330 state.setDescription( daoUtil.getString( 3 ) );
331 state.setResourceCount( daoUtil.getInt( 4 ) );
332 return state;
333 }
334 }
335 return null;
336 }
337
338 @Override
339 public List<ResourceHistory> getHistory( final int identityId, final Plugin plugin )
340 {
341 final List<ResourceHistory> list = new ArrayList<>( );
342 try ( final DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_HISTORY, plugin ) )
343 {
344 daoUtil.setInt( 1, identityId );
345 daoUtil.executeQuery( );
346
347 while ( daoUtil.next( ) )
348 {
349 final ResourceHistory history = new ResourceHistory( );
350 history.setId( daoUtil.getInt( 1 ) );
351 final Action action = new Action( );
352 action.setName( daoUtil.getString( 2 ) );
353 action.setDescription( daoUtil.getString( 3 ) );
354 history.setAction( action );
355 history.setCreationDate( daoUtil.getTimestamp( 4 ) );
356 history.setUserAccessCode( daoUtil.getString( 5 ) );
357 list.add( history );
358 }
359 }
360 return list;
361 }
362 }