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.portal.business.user.attribute;
35
36 import fr.paris.lutece.portal.business.file.File;
37 import fr.paris.lutece.portal.business.user.AdminUser;
38 import fr.paris.lutece.portal.service.util.AppLogService;
39 import fr.paris.lutece.util.sql.DAOUtil;
40
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.Locale;
44
45
46
47
48
49
50
51 public class AdminUserFieldDAO implements IAdminUserFieldDAO
52 {
53
54 private static final String CONSTANT_PERCENT = "%";
55 private static final String CONSTANT_OPEN_BRACKET = "(";
56 private static final String CONSTANT_CLOSED_BRACKET = ")";
57
58
59 private static final String SQL_QUERY_NEW_PK = " SELECT max(id_user_field) FROM core_admin_user_field ";
60
61
62 private static final String SQL_QUERY_SELECT = " SELECT auf.id_user_field, auf.id_user, auf.id_attribute, auf.id_field, auf.id_file, auf.user_field_value, " +
63 " au.access_code, au.last_name, au.first_name, au.email, au.status, au.locale, au.level_user, " +
64 " a.type_class_name, a.title, a.help_message, a.is_mandatory, a.attribute_position, " +
65 " af.title, af.DEFAULT_value, af.is_DEFAULT_value, af.height, af.width, af.max_size_enter, af.is_multiple, af.field_position " +
66 " FROM core_admin_user_field auf " + " INNER JOIN core_admin_user au ON auf.id_user = au.id_user " +
67 " INNER JOIN core_attribute a ON auf.id_attribute = a.id_attribute " +
68 " INNER JOIN core_attribute_field af ON auf.id_field = af.id_field ";
69 private static final String SQL_QUERY_SELECT_USER_FIELDS_BY_ID_USER_ID_ATTRIBUTE = " SELECT auf.id_user_field, auf.id_user, auf.id_attribute, auf.id_field, auf.id_file, auf.user_field_value, " +
70 " a.type_class_name, a.title, a.help_message, a.is_mandatory, a.attribute_position " +
71 " FROM core_admin_user_field auf " + " INNER JOIN core_attribute a ON a.id_attribute = auf.id_attribute " +
72 " WHERE auf.id_user = ? AND auf.id_attribute = ? ";
73 private static final String SQL_QUERY_SELECT_USERS_BY_FILTER = " SELECT DISTINCT u.id_user, u.access_code, u.last_name, u.first_name, u.email, u.status, u.locale, u.level_user " +
74 " FROM core_admin_user u INNER JOIN core_admin_user_field uf ON u.id_user = uf.id_user ";
75 private static final String SQL_QUERY_SELECT_ID_USER = " SELECT id_user FROM core_admin_user_field WHERE id_attribute = ? AND id_field = ? AND user_field_value LIKE ? ";
76
77
78 private static final String SQL_QUERY_INSERT = " INSERT INTO core_admin_user_field (id_user_field, id_user, id_attribute, id_field, id_file, user_field_value) " +
79 " VALUES (?,?,?,?,?,?) ";
80
81
82 private static final String SQL_QUERY_UPDATE = " UPDATE core_admin_user_field SET user_field_value = ? WHERE id_user_field = ? ";
83
84
85 private static final String SQL_QUERY_DELETE = " DELETE FROM core_admin_user_field WHERE id_user_field = ? ";
86 private static final String SQL_QUERY_DELETE_FROM_ID_FIELD = " DELETE FROM core_admin_user_field WHERE id_field = ? ";
87 private static final String SQL_QUERY_DELETE_FROM_ID_USER = " DELETE FROM core_admin_user_field WHERE id_user = ? ";
88 private static final String SQL_QUERY_DELETE_FROM_ID_ATTRIBUTE = " DELETE FROM core_admin_user_field WHERE id_attribute = ? ";
89
90
91 private static final String SQL_ID_ATTRIBUTE_AND_USER_FIELD_VALUE = " WHERE id_attribute = ? AND id_field = ? AND user_field_value LIKE ? ";
92 private static final String SQL_AND_ID_USER_IN = " AND id_user IN ";
93 private static final String SQL_AND_ID_USER_IN_FIRST = " AND uf.id_user IN ";
94 private static final String SQL_WHERE = " WHERE ";
95 private static final String SQL_AND = " AND ";
96 private static final String SQL_FILTER_ID_USER_FIELD = " WHERE auf.id_user_field = ? ";
97 private static final String SQL_FILTER_ID_USER = " auf.id_user = ? ";
98 private static final String SQL_FILTER_ID_ATTRIBUTE = " auf.id_attribute = ? ";
99 private static final String SQL_FILTER_ID_FIELD = " auf.id_field = ? ";
100
101
102
103
104
105 private int newPrimaryKey( )
106 {
107 StringBuilder sbSQL = new StringBuilder( SQL_QUERY_NEW_PK );
108 DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ) );
109 daoUtil.executeQuery( );
110
111 int nKey = 1;
112
113 if ( daoUtil.next( ) )
114 {
115 nKey = daoUtil.getInt( 1 ) + 1;
116 }
117
118 daoUtil.free( );
119
120 return nKey;
121 }
122
123
124
125
126
127
128 @Override
129 public AdminUserField load( int nIdUserField )
130 {
131 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT + SQL_WHERE + SQL_FILTER_ID_USER_FIELD );
132 daoUtil.setInt( 1, nIdUserField );
133 daoUtil.executeQuery( );
134
135 AdminUserField userField = null;
136
137 if ( daoUtil.next( ) )
138 {
139 userField = new AdminUserField( );
140 userField.setIdUserField( daoUtil.getInt( 1 ) );
141 userField.setValue( daoUtil.getString( 6 ) );
142
143
144 AdminUser user = new AdminUser( );
145 user.setUserId( daoUtil.getInt( 2 ) );
146 user.setAccessCode( daoUtil.getString( 7 ) );
147 user.setLastName( daoUtil.getString( 8 ) );
148 user.setFirstName( daoUtil.getString( 9 ) );
149 user.setEmail( daoUtil.getString( 10 ) );
150 user.setStatus( daoUtil.getInt( 11 ) );
151 user.setLocale( new Locale( daoUtil.getString( 12 ) ) );
152 user.setUserLevel( daoUtil.getInt( 13 ) );
153 userField.setUser( user );
154
155
156 IAttribute attribute = null;
157
158 try
159 {
160 attribute = (IAttribute) Class.forName( daoUtil.getString( 14 ) ).newInstance( );
161 }
162 catch ( ClassNotFoundException e )
163 {
164
165 AppLogService.error( e );
166 }
167 catch ( InstantiationException e )
168 {
169
170
171 AppLogService.error( e );
172 }
173 catch ( IllegalAccessException e )
174 {
175
176 AppLogService.error( e );
177 }
178
179 attribute.setIdAttribute( daoUtil.getInt( 3 ) );
180 attribute.setTitle( daoUtil.getString( 15 ) );
181 attribute.setHelpMessage( daoUtil.getString( 16 ) );
182 attribute.setMandatory( daoUtil.getBoolean( 17 ) );
183 attribute.setPosition( daoUtil.getInt( 18 ) );
184 attribute.setAttributeType( new Locale( daoUtil.getString( 12 ) ) );
185 userField.setAttribute( attribute );
186
187
188 AttributeField attributeField = new AttributeField( );
189 attributeField.setIdField( daoUtil.getInt( 4 ) );
190 attributeField.setTitle( daoUtil.getString( 19 ) );
191 attributeField.setValue( daoUtil.getString( 20 ) );
192 attributeField.setDefaultValue( daoUtil.getBoolean( 21 ) );
193 attributeField.setHeight( daoUtil.getInt( 22 ) );
194 attributeField.setWidth( daoUtil.getInt( 23 ) );
195 attributeField.setMaxSizeEnter( daoUtil.getInt( 24 ) );
196 attributeField.setMultiple( daoUtil.getBoolean( 25 ) );
197 attributeField.setPosition( daoUtil.getInt( 26 ) );
198 userField.setAttributeField( attributeField );
199
200
201 if ( daoUtil.getObject( 5 ) != null )
202 {
203 File file = new File( );
204 file.setIdFile( daoUtil.getInt( 5 ) );
205 userField.setFile( file );
206 }
207 }
208
209 daoUtil.free( );
210
211 return userField;
212 }
213
214
215
216
217
218 @Override
219 public void insert( AdminUserField userField )
220 {
221 userField.setIdUserField( newPrimaryKey( ) );
222
223 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
224 daoUtil.setInt( 1, userField.getIdUserField( ) );
225 daoUtil.setInt( 2, userField.getUser( ).getUserId( ) );
226 daoUtil.setInt( 3, userField.getAttribute( ).getIdAttribute( ) );
227 daoUtil.setInt( 4, userField.getAttributeField( ).getIdField( ) );
228
229 if ( userField.getFile( ) != null )
230 {
231 daoUtil.setInt( 5, userField.getFile( ).getIdFile( ) );
232 }
233 else
234 {
235 daoUtil.setIntNull( 5 );
236 }
237
238 daoUtil.setString( 6, userField.getValue( ) );
239
240 daoUtil.executeUpdate( );
241 daoUtil.free( );
242 }
243
244
245
246
247
248 @Override
249 public void store( AdminUserField userField )
250 {
251 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
252 daoUtil.setString( 1, userField.getValue( ) );
253 daoUtil.setInt( 2, userField.getIdUserField( ) );
254
255 daoUtil.executeUpdate( );
256 daoUtil.free( );
257 }
258
259
260
261
262
263 @Override
264 public void delete( int nIdUserField )
265 {
266 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
267 daoUtil.setInt( 1, nIdUserField );
268
269 daoUtil.executeUpdate( );
270 daoUtil.free( );
271 }
272
273
274
275
276
277 @Override
278 public void deleteUserFieldsFromIdField( int nIdField )
279 {
280 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_FROM_ID_FIELD );
281 daoUtil.setInt( 1, nIdField );
282
283 daoUtil.executeUpdate( );
284 daoUtil.free( );
285 }
286
287
288
289
290
291 @Override
292 public void deleteUserFieldsFromIdUser( int nIdUser )
293 {
294 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_FROM_ID_USER );
295 daoUtil.setInt( 1, nIdUser );
296
297 daoUtil.executeUpdate( );
298 daoUtil.free( );
299 }
300
301
302
303
304
305 @Override
306 public void deleteUserFieldsFromIdAttribute( int nIdAttribute )
307 {
308 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_FROM_ID_ATTRIBUTE );
309 daoUtil.setInt( 1, nIdAttribute );
310
311 daoUtil.executeUpdate( );
312 daoUtil.free( );
313 }
314
315
316
317
318
319
320
321 @Override
322 public List<AdminUserField> selectUserFieldsByIdUserIdAttribute( int nIdUser, int nIdAttribute )
323 {
324 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_USER_FIELDS_BY_ID_USER_ID_ATTRIBUTE );
325 daoUtil.setInt( 1, nIdUser );
326 daoUtil.setInt( 2, nIdAttribute );
327 daoUtil.executeQuery( );
328
329 List<AdminUserField> listUserFields = new ArrayList<AdminUserField>( );
330
331 while ( daoUtil.next( ) )
332 {
333 AdminUserField userField = new AdminUserField( );
334 userField.setIdUserField( daoUtil.getInt( 1 ) );
335 userField.setValue( daoUtil.getString( 6 ) );
336
337
338 if ( daoUtil.getObject( 5 ) != null )
339 {
340 File file = new File( );
341 file.setIdFile( daoUtil.getInt( 5 ) );
342 userField.setFile( file );
343 }
344
345
346 AdminUser user = new AdminUser( );
347 user.setUserId( nIdUser );
348 userField.setUser( user );
349
350
351 IAttribute attribute = null;
352
353 try
354 {
355 attribute = (IAttribute) Class.forName( daoUtil.getString( 7 ) ).newInstance( );
356 }
357 catch ( ClassNotFoundException e )
358 {
359
360 AppLogService.error( e );
361 }
362 catch ( InstantiationException e )
363 {
364
365
366 AppLogService.error( e );
367 }
368 catch ( IllegalAccessException e )
369 {
370
371 AppLogService.error( e );
372 }
373
374 attribute.setIdAttribute( nIdAttribute );
375 attribute.setTitle( daoUtil.getString( 8 ) );
376 attribute.setHelpMessage( daoUtil.getString( 9 ) );
377 attribute.setMandatory( daoUtil.getBoolean( 10 ) );
378 attribute.setPosition( daoUtil.getInt( 11 ) );
379 userField.setAttribute( attribute );
380
381
382 AttributeField attributeField = new AttributeField( );
383 attributeField.setIdField( daoUtil.getInt( 4 ) );
384 userField.setAttributeField( attributeField );
385
386 listUserFields.add( userField );
387 }
388
389 daoUtil.free( );
390
391 return listUserFields;
392 }
393
394
395
396
397
398
399 @Override
400 public List<AdminUser> selectUsersByFilter( AdminUserFieldFilter auFieldFilter )
401 {
402 List<AdminUserField> listUserFields = auFieldFilter.getListUserFields( );
403
404 if ( ( listUserFields == null ) || ( listUserFields.size( ) == 0 ) )
405 {
406 return null;
407 }
408
409 List<AdminUser> listUsers = new ArrayList<AdminUser>( );
410 StringBuilder sbSQL = new StringBuilder( SQL_QUERY_SELECT_USERS_BY_FILTER );
411
412 for ( int i = 1; i <= listUserFields.size( ); i++ )
413 {
414 if ( i == 1 )
415 {
416 sbSQL.append( SQL_ID_ATTRIBUTE_AND_USER_FIELD_VALUE );
417 }
418 else
419 {
420 sbSQL.append( CONSTANT_OPEN_BRACKET + SQL_QUERY_SELECT_ID_USER );
421 }
422
423 if ( ( i != listUserFields.size( ) ) && ( i != 1 ) )
424 {
425 sbSQL.append( SQL_AND_ID_USER_IN );
426 }
427 else if ( ( i != listUserFields.size( ) ) && ( i == 1 ) )
428 {
429 sbSQL.append( SQL_AND_ID_USER_IN_FIRST );
430 }
431 }
432
433 for ( int i = 2; i <= listUserFields.size( ); i++ )
434 {
435 sbSQL.append( CONSTANT_CLOSED_BRACKET );
436 }
437
438 DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ) );
439
440 int nbCount = 1;
441
442 for ( AdminUserField userField : listUserFields )
443 {
444 daoUtil.setInt( nbCount++, userField.getAttribute( ).getIdAttribute( ) );
445 daoUtil.setInt( nbCount++, userField.getAttributeField( ).getIdField( ) );
446 daoUtil.setString( nbCount++, CONSTANT_PERCENT + userField.getValue( ) + CONSTANT_PERCENT );
447 }
448
449 daoUtil.executeQuery( );
450
451 while ( daoUtil.next( ) )
452 {
453 AdminUser user = new AdminUser( );
454 user.setUserId( daoUtil.getInt( 1 ) );
455 user.setAccessCode( daoUtil.getString( 2 ) );
456 user.setLastName( daoUtil.getString( 3 ) );
457 user.setFirstName( daoUtil.getString( 4 ) );
458 user.setEmail( daoUtil.getString( 5 ) );
459 user.setStatus( daoUtil.getInt( 6 ) );
460
461 Locale locale = new Locale( daoUtil.getString( 7 ) );
462 user.setLocale( locale );
463 listUsers.add( user );
464 }
465
466 daoUtil.free( );
467
468 return listUsers;
469 }
470
471
472
473
474
475
476
477 @Override
478 public List<AdminUserField> selectByFilter( AdminUserFieldFilter auFieldFilter )
479 {
480 List<AdminUserField> listUserFields = new ArrayList<AdminUserField>( );
481 List<String> listFilter = new ArrayList<String>( );
482
483 if ( auFieldFilter.containsIdAttribute( ) )
484 {
485 listFilter.add( SQL_FILTER_ID_ATTRIBUTE );
486 }
487
488 if ( auFieldFilter.containsIdUser( ) )
489 {
490 listFilter.add( SQL_FILTER_ID_USER );
491 }
492
493 if ( auFieldFilter.containsIdField( ) )
494 {
495 listFilter.add( SQL_FILTER_ID_FIELD );
496 }
497
498 StringBuilder sbSQL = new StringBuilder( SQL_QUERY_SELECT );
499
500 if ( listFilter.size( ) > 0 )
501 {
502 boolean bIsFirst = true;
503
504 for ( String filter : listFilter )
505 {
506 if ( bIsFirst )
507 {
508 sbSQL.append( SQL_WHERE );
509 bIsFirst = false;
510 }
511 else
512 {
513 sbSQL.append( SQL_AND );
514 }
515
516 sbSQL.append( filter );
517 }
518 }
519
520 DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ) );
521 int nIndex = 1;
522
523 if ( auFieldFilter.containsIdAttribute( ) )
524 {
525 daoUtil.setInt( nIndex++, auFieldFilter.getIdAttribute( ) );
526 }
527
528 if ( auFieldFilter.containsIdUser( ) )
529 {
530 daoUtil.setInt( nIndex++, auFieldFilter.getIdUser( ) );
531 }
532
533 if ( auFieldFilter.containsIdField( ) )
534 {
535 daoUtil.setInt( nIndex++, auFieldFilter.getIdField( ) );
536 }
537
538 daoUtil.executeQuery( );
539
540 while ( daoUtil.next( ) )
541 {
542 AdminUserField userField = new AdminUserField( );
543 userField.setIdUserField( daoUtil.getInt( 1 ) );
544 userField.setValue( daoUtil.getString( 6 ) );
545
546
547 AdminUser user = new AdminUser( );
548 user.setUserId( daoUtil.getInt( 2 ) );
549 user.setAccessCode( daoUtil.getString( 7 ) );
550 user.setLastName( daoUtil.getString( 8 ) );
551 user.setFirstName( daoUtil.getString( 9 ) );
552 user.setEmail( daoUtil.getString( 10 ) );
553 user.setStatus( daoUtil.getInt( 11 ) );
554 user.setLocale( new Locale( daoUtil.getString( 12 ) ) );
555 user.setUserLevel( daoUtil.getInt( 13 ) );
556 userField.setUser( user );
557
558
559 IAttribute attribute = null;
560
561 try
562 {
563 attribute = (IAttribute) Class.forName( daoUtil.getString( 14 ) ).newInstance( );
564 }
565 catch ( ClassNotFoundException e )
566 {
567
568 AppLogService.error( e );
569 }
570 catch ( InstantiationException e )
571 {
572
573
574 AppLogService.error( e );
575 }
576 catch ( IllegalAccessException e )
577 {
578
579 AppLogService.error( e );
580 }
581
582 attribute.setIdAttribute( daoUtil.getInt( 3 ) );
583 attribute.setTitle( daoUtil.getString( 15 ) );
584 attribute.setHelpMessage( daoUtil.getString( 16 ) );
585 attribute.setMandatory( daoUtil.getBoolean( 17 ) );
586 attribute.setPosition( daoUtil.getInt( 18 ) );
587 attribute.setAttributeType( new Locale( daoUtil.getString( 12 ) ) );
588 userField.setAttribute( attribute );
589
590
591 AttributeField attributeField = new AttributeField( );
592 attributeField.setIdField( daoUtil.getInt( 4 ) );
593 attributeField.setTitle( daoUtil.getString( 19 ) );
594 attributeField.setValue( daoUtil.getString( 20 ) );
595 attributeField.setDefaultValue( daoUtil.getBoolean( 21 ) );
596 attributeField.setHeight( daoUtil.getInt( 22 ) );
597 attributeField.setWidth( daoUtil.getInt( 23 ) );
598 attributeField.setMaxSizeEnter( daoUtil.getInt( 24 ) );
599 attributeField.setMultiple( daoUtil.getBoolean( 25 ) );
600 attributeField.setPosition( daoUtil.getInt( 26 ) );
601 userField.setAttributeField( attributeField );
602
603
604 if ( daoUtil.getObject( 5 ) != null )
605 {
606 File file = new File( );
607 file.setIdFile( daoUtil.getInt( 5 ) );
608 userField.setFile( file );
609 }
610
611 listUserFields.add( userField );
612 }
613
614 daoUtil.free( );
615
616 return listUserFields;
617 }
618 }