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.contact.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
45 public final class ContactListDAO implements IContactListDAO
46 {
47
48 private static final String SQL_QUERY_NEWPK = "SELECT max( id_contact_list ) FROM contact_list ";
49 private static final String SQL_QUERY_INSERT = "INSERT INTO contact_list VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
50 private static final String SQL_QUERY_SELECT = "SELECT id_contact_list, label_contact_list, description_contact_list, workgroup_key, role, is_tos_active, tos_message FROM contact_list WHERE id_contact_list = ?";
51 private static final String SQL_QUERY_DELETE = "DELETE FROM contact_list WHERE id_contact_list = ?";
52 private static final String SQL_QUERY_UPDATE = "UPDATE contact_list SET label_contact_list = ?, description_contact_list = ?, workgroup_key = ?, role = ?, is_tos_active = ?, tos_message = ? WHERE id_contact_list = ?";
53 private static final String SQL_QUERY_SELECTALL = "SELECT id_contact_list, label_contact_list, description_contact_list,workgroup_key, role, contact_list_order FROM contact_list ORDER BY contact_list_order";
54 private static final String SQL_QUERY_COUNT_CONTACTS_FOR_LIST = "SELECT COUNT(*) FROM contact_list_contact WHERE id_contact_list = ?";
55 private static final String SQL_QUERY_COUNT_LISTS_FOR_CONTACT = "SELECT COUNT(*) FROM contact_list_contact WHERE id_contact = ?";
56 private static final String SQL_QUERY_SELECT_CONTACTS_FOR_LIST = "SELECT a.id_contact, b.description, b.email, a.contact_order FROM contact_list_contact a, contact b WHERE id_contact_list = ? AND b.id_contact=a.id_contact ORDER BY a.contact_order";
57 private static final String SQL_QUERY_SELECT_CONTACT_IN_LIST = "SELECT COUNT(*) FROM contact_list_contact WHERE id_contact_list = ? AND id_contact = ?";
58 private static final String SQL_QUERY_ASSIGN = "INSERT INTO contact_list_contact(id_contact_list, id_contact, contact_order) VALUES (?, ?, ?)";
59 private static final String SQL_QUERY_SELECT_BY_ROLE_KEY = "SELECT id_contact_list, label_contact_list, description_contact_list,workgroup_key, role FROM contact_list WHERE role = ? ";
60 private static final String SQL_QUERY_UNASSIGN = "DELETE FROM contact_list_contact WHERE id_contact_list = ? AND id_contact = ?";
61 private static final String SQL_QUERY_UNASSIGN_CONTACTS_FOR_LIST = "DELETE FROM contact_list_contact WHERE id_contact_list = ?";
62 private static final String SQL_QUERY_UNASSIGN_LISTS_FOR_CONTACT = "DELETE FROM contact_list_contact WHERE id_contact = ?";
63 private static final String SQL_QUERY_SELECT_NOT_ASSIGNED_CONTACTS_FOR_LIST = "SELECT id_contact, description, email, workgroup_key FROM contact WHERE id_contact NOT IN (SELECT id_contact from contact_list_contact WHERE id_contact_list = ?)";
64 private static final String SQL_QUERY_SELECT_ASSIGNED_LISTS_FOR_CONTACT = "SELECT a.id_contact_list, b.label_contact_list, b.description_contact_list, b.workgroup_key FROM contact_list_contact a, contact_list b WHERE id_contact = ? AND b.id_contact_list = a.id_contact_list";
65 private static final String SQL_QUERY_SELECT_NOT_ASSIGNED_LISTS_FOR_CONTACT = "SELECT id_contact_list, label_contact_list, description_contact_list, workgroup_key FROM contact_list WHERE id_contact_list NOT IN (SELECT id_contact_list from contact_list_contact WHERE id_contact = ?)";
66 private static final String SQL_QUERY_COUNT_LISTS = "SELECT COUNT(*) FROM contact_list WHERE id_contact_list = ?";
67 private static final String SQL_QUERY_SELECT_MAX_CONTACT_ORDER = "SELECT max(contact_order) FROM contact_list_contact WHERE id_contact_list = ?";
68
69
70 private static final String SQL_QUERY_SELECT_CONTACT_LIST_ID_BY_ORDER = "SELECT id_contact_list FROM contact_list WHERE contact_list_order = ?";
71 private static final String SQL_QUERY_SELECT_CONTACT_LIST_ORDER_BY_ID = "SELECT contact_list_order FROM contact_list WHERE id_contact_list = ?";
72 private static final String SQL_QUERY_SELECT_MAX_CONTACT_LIST_ORDER = "SELECT max(contact_list_order) FROM contact_list";
73 private static final String SQL_QUERY_UPDATE_CONTACT_LIST_ORDER = "UPDATE contact_list SET contact_list_order = ? WHERE id_contact_list = ?";
74
75
76
77
78
79
80
81
82
83
84
85 private int newPrimaryKey( Plugin plugin )
86 {
87 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEWPK, plugin );
88 daoUtil.executeQuery( );
89
90 int nKey;
91
92 if ( !daoUtil.next( ) )
93 {
94
95 nKey = 1;
96 }
97
98 nKey = daoUtil.getInt( 1 ) + 1;
99
100 daoUtil.free( );
101
102 return nKey;
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116 public void insert( ContactList contactList, Plugin plugin )
117 {
118 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
119 contactList.setId( newPrimaryKey( plugin ) );
120 daoUtil.setInt( 1, contactList.getId( ) );
121 daoUtil.setString( 2, contactList.getLabel( ) );
122 daoUtil.setString( 3, contactList.getDescription( ) );
123 daoUtil.setString( 4, contactList.getWorkgroup( ) );
124 daoUtil.setString( 5, contactList.getRole( ) );
125 daoUtil.setInt( 6, maxOrderContactList( plugin ) + 1 );
126 daoUtil.setBoolean( 7, contactList.getTos( ) );
127 daoUtil.setString( 8, contactList.getTosMessage( ) );
128 daoUtil.executeUpdate( );
129 daoUtil.free( );
130 }
131
132
133
134
135
136
137
138
139
140
141 public ContactList load( int nContactListId, Plugin plugin )
142 {
143 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
144 daoUtil.setInt( 1, nContactListId );
145 daoUtil.executeQuery( );
146
147 ContactList contactList = null;
148
149 if ( daoUtil.next( ) )
150 {
151 contactList = new ContactList( );
152 contactList.setId( daoUtil.getInt( 1 ) );
153 contactList.setLabel( daoUtil.getString( 2 ) );
154 contactList.setDescription( daoUtil.getString( 3 ) );
155 contactList.setWorkgroup( daoUtil.getString( 4 ) );
156 contactList.setRole( daoUtil.getString( 5 ) );
157 contactList.setTos( daoUtil.getBoolean( 6 ) );
158 contactList.setTosMessage( daoUtil.getString( 7 ) );
159 }
160
161 daoUtil.free( );
162
163 return contactList;
164 }
165
166
167
168
169
170
171
172
173 public Collection<ContactList> selectAll( Plugin plugin )
174 {
175 Collection<ContactList> contactListsList = new ArrayList<ContactList>( );
176 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
177 daoUtil.executeQuery( );
178
179 while ( daoUtil.next( ) )
180 {
181 ContactListbusiness/ContactList.html#ContactList">ContactList contactList = new ContactList( );
182 contactList.setId( daoUtil.getInt( 1 ) );
183 contactList.setLabel( daoUtil.getString( 2 ) );
184 contactList.setDescription( daoUtil.getString( 3 ) );
185 contactList.setContactsNumber( countContactsForList( daoUtil.getInt( 1 ), plugin ) );
186 contactList.setWorkgroup( daoUtil.getString( 4 ) );
187 contactList.setRole( daoUtil.getString( 5 ) );
188 contactList.setContactListOrder( daoUtil.getInt( 6 ) );
189 contactListsList.add( contactList );
190 }
191
192 daoUtil.free( );
193
194 return contactListsList;
195 }
196
197
198
199
200
201
202
203
204
205
206 public Collection<ContactList> selectByRoleKey( String strRoleKey, Plugin plugin )
207 {
208 Collection<ContactList> contactListsList = new ArrayList<ContactList>( );
209 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ROLE_KEY, plugin );
210 daoUtil.setString( 1, strRoleKey );
211 daoUtil.executeQuery( );
212
213 while ( daoUtil.next( ) )
214 {
215 ContactListbusiness/ContactList.html#ContactList">ContactList contactList = new ContactList( );
216 contactList.setId( daoUtil.getInt( 1 ) );
217 contactList.setLabel( daoUtil.getString( 2 ) );
218 contactList.setDescription( daoUtil.getString( 3 ) );
219 contactList.setContactsNumber( countContactsForList( daoUtil.getInt( 1 ), plugin ) );
220 contactList.setWorkgroup( daoUtil.getString( 4 ) );
221 contactList.setRole( daoUtil.getString( 5 ) );
222 contactListsList.add( contactList );
223 }
224
225 daoUtil.free( );
226
227 return contactListsList;
228 }
229
230
231
232
233
234
235
236
237
238
239 public int countContactsForList( int nIdContactList, Plugin plugin )
240 {
241 int nCounted = 0;
242 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_CONTACTS_FOR_LIST, plugin );
243 daoUtil.setInt( 1, nIdContactList );
244 daoUtil.executeQuery( );
245
246 if ( daoUtil.next( ) )
247 {
248 nCounted = daoUtil.getInt( 1 );
249 }
250
251 daoUtil.free( );
252
253 return nCounted;
254 }
255
256
257
258
259
260
261
262
263
264
265 public int countListsForContact( int nIdContact, Plugin plugin )
266 {
267 int nCounted = 0;
268 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_LISTS_FOR_CONTACT, plugin );
269 daoUtil.setInt( 1, nIdContact );
270 daoUtil.executeQuery( );
271
272 if ( daoUtil.next( ) )
273 {
274 nCounted = daoUtil.getInt( 1 );
275 }
276
277 daoUtil.free( );
278
279 return nCounted;
280 }
281
282
283
284
285
286
287
288
289
290
291 public boolean listExists( int nIdContactList, Plugin plugin )
292 {
293 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_LISTS, plugin );
294 daoUtil.setInt( 1, nIdContactList );
295
296 int nCounted = 0;
297 daoUtil.executeQuery( );
298
299 if ( daoUtil.next( ) )
300 {
301 nCounted = daoUtil.getInt( 1 );
302 }
303
304 daoUtil.free( );
305
306 if ( nCounted < 1 )
307 {
308 return false;
309 }
310
311 return true;
312 }
313
314
315
316
317
318
319
320
321
322
323 public Collection<Contact> selectContactsForList( int nIdContactList, Plugin plugin )
324 {
325 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTACTS_FOR_LIST, plugin );
326 daoUtil.setInt( 1, nIdContactList );
327 daoUtil.executeQuery( );
328
329 Collection<Contact> contactsList = new ArrayList<Contact>( );
330
331 while ( daoUtil.next( ) )
332 {
333 Contactcontact/business/Contact.html#Contact">Contact contact = new Contact( );
334 contact.setId( daoUtil.getInt( 1 ) );
335 contact.setName( daoUtil.getString( 2 ) );
336 contact.setEmail( daoUtil.getString( 3 ) );
337 contact.setContactOrder( daoUtil.getInt( 4 ) );
338 contactsList.add( contact );
339 }
340
341 daoUtil.free( );
342
343 return contactsList;
344 }
345
346
347
348
349
350
351
352
353
354
355
356
357 public boolean isAssigned( int nIdContact, int nIdContactList, Plugin plugin )
358 {
359 int nFound = 0;
360 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTACT_IN_LIST, plugin );
361 daoUtil.setInt( 1, nIdContactList );
362 daoUtil.setInt( 2, nIdContact );
363
364 daoUtil.executeQuery( );
365
366 if ( daoUtil.next( ) )
367 {
368 nFound = daoUtil.getInt( 1 );
369 }
370
371 daoUtil.free( );
372
373 if ( nFound < 1 )
374 {
375 return false;
376 }
377
378 return true;
379 }
380
381
382
383
384
385
386
387
388
389
390
391 public void assign( int nIdContact, int nIdContactList, Plugin plugin )
392 {
393 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_ASSIGN, plugin );
394 daoUtil.setInt( 1, nIdContactList );
395 daoUtil.setInt( 2, nIdContact );
396 daoUtil.setInt( 3, maxOrderContact( nIdContactList, plugin ) + 1 );
397
398 daoUtil.executeUpdate( );
399 daoUtil.free( );
400 }
401
402
403
404
405
406
407
408
409
410
411
412 public void unAssign( int nIdContact, int nIdContactList, Plugin plugin )
413 {
414 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UNASSIGN, plugin );
415 daoUtil.setInt( 1, nIdContactList );
416 daoUtil.setInt( 2, nIdContact );
417 daoUtil.executeUpdate( );
418 daoUtil.free( );
419 }
420
421
422
423
424
425
426
427
428
429
430 public Collection<Contact> selectNotAssignedContactsFor( int nIdContactList, Plugin plugin )
431 {
432 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_NOT_ASSIGNED_CONTACTS_FOR_LIST, plugin );
433 daoUtil.setInt( 1, nIdContactList );
434 daoUtil.executeQuery( );
435
436 Collection<Contact> contactsList = new ArrayList<Contact>( );
437
438 while ( daoUtil.next( ) )
439 {
440 Contactcontact/business/Contact.html#Contact">Contact contact = new Contact( );
441 contact.setId( daoUtil.getInt( 1 ) );
442 contact.setName( daoUtil.getString( 2 ) );
443 contact.setEmail( daoUtil.getString( 3 ) );
444 contact.setWorkgroup( daoUtil.getString( 4 ) );
445 contactsList.add( contact );
446 }
447
448 daoUtil.free( );
449
450 return contactsList;
451 }
452
453
454
455
456
457
458
459
460
461
462 public Collection<ContactList> selectAssignedListsFor( int nIdContact, Plugin plugin )
463 {
464 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ASSIGNED_LISTS_FOR_CONTACT, plugin );
465 daoUtil.setInt( 1, nIdContact );
466 daoUtil.executeQuery( );
467
468 Collection<ContactList> assignedLists = new ArrayList<ContactList>( );
469
470 while ( daoUtil.next( ) )
471 {
472 ContactListbusiness/ContactList.html#ContactList">ContactList contactList = new ContactList( );
473 contactList.setId( daoUtil.getInt( 1 ) );
474 contactList.setLabel( daoUtil.getString( 2 ) );
475 contactList.setDescription( daoUtil.getString( 3 ) );
476 contactList.setContactsNumber( countContactsForList( daoUtil.getInt( 1 ), plugin ) );
477 assignedLists.add( contactList );
478 }
479
480 daoUtil.free( );
481
482 return assignedLists;
483 }
484
485
486
487
488
489
490
491
492
493
494 public Collection<ContactList> selectNotAssignedListsFor( int nIdContact, Plugin plugin )
495 {
496 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_NOT_ASSIGNED_LISTS_FOR_CONTACT, plugin );
497 daoUtil.setInt( 1, nIdContact );
498 daoUtil.executeQuery( );
499
500 Collection<ContactList> notAssignedLists = new ArrayList<ContactList>( );
501
502 while ( daoUtil.next( ) )
503 {
504 ContactListbusiness/ContactList.html#ContactList">ContactList contactList = new ContactList( );
505 contactList.setId( daoUtil.getInt( 1 ) );
506 contactList.setLabel( daoUtil.getString( 2 ) );
507 contactList.setDescription( daoUtil.getString( 3 ) );
508 contactList.setWorkgroup( daoUtil.getString( 4 ) );
509 contactList.setContactsNumber( countContactsForList( daoUtil.getInt( 1 ), plugin ) );
510 notAssignedLists.add( contactList );
511 }
512
513 daoUtil.free( );
514
515 return notAssignedLists;
516 }
517
518
519
520
521
522
523
524
525
526 public void delete( int nIdContactList, Plugin plugin )
527 {
528 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
529 daoUtil.setInt( 1, nIdContactList );
530 daoUtil.executeUpdate( );
531 daoUtil.free( );
532 }
533
534
535
536
537
538
539
540
541
542 public void unassignContactsForList( int nIdContactList, Plugin plugin )
543 {
544 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UNASSIGN_CONTACTS_FOR_LIST, plugin );
545 daoUtil.setInt( 1, nIdContactList );
546 daoUtil.executeUpdate( );
547 daoUtil.free( );
548 }
549
550
551
552
553
554
555
556
557
558 public void unassignListsForContact( int nIdContact, Plugin plugin )
559 {
560 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UNASSIGN_LISTS_FOR_CONTACT, plugin );
561 daoUtil.setInt( 1, nIdContact );
562 daoUtil.executeUpdate( );
563 daoUtil.free( );
564 }
565
566
567
568
569
570
571
572
573
574 public void store( ContactList contactList, Plugin plugin )
575 {
576 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
577 int nContactListId = contactList.getId( );
578
579 daoUtil.setString( 1, contactList.getLabel( ) );
580 daoUtil.setString( 2, contactList.getDescription( ) );
581 daoUtil.setString( 3, contactList.getWorkgroup( ) );
582 daoUtil.setString( 4, contactList.getRole( ) );
583 daoUtil.setBoolean( 5, contactList.getTos( ) );
584 daoUtil.setString( 6, contactList.getTosMessage( ) );
585 daoUtil.setInt( 7, nContactListId );
586
587 daoUtil.executeUpdate( );
588 daoUtil.free( );
589 }
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604 public void storeContactListOrder( int nNewOrder, int nId, Plugin plugin )
605 {
606 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_CONTACT_LIST_ORDER, plugin );
607 daoUtil.setInt( 1, nNewOrder );
608 daoUtil.setInt( 2, nId );
609 daoUtil.executeUpdate( );
610 daoUtil.free( );
611 }
612
613
614
615
616
617
618
619
620
621
622 public int selectContactListIdByOrder( int nContactListOrder, Plugin plugin )
623 {
624 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTACT_LIST_ID_BY_ORDER, plugin );
625 int nResult = 0;
626 daoUtil.setInt( 1, nContactListOrder );
627 daoUtil.executeQuery( );
628
629 if ( !daoUtil.next( ) )
630 {
631
632 nResult = 1;
633 }
634 else
635 {
636 nResult = daoUtil.getInt( 1 );
637 }
638
639 daoUtil.free( );
640
641 return nResult;
642 }
643
644
645
646
647
648
649
650
651
652
653 public int selectContactListOrderById( int nIdContactList, Plugin plugin )
654 {
655 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTACT_LIST_ORDER_BY_ID, plugin );
656 int nResult = 0;
657 daoUtil.setInt( 1, nIdContactList );
658 daoUtil.executeQuery( );
659
660 if ( !daoUtil.next( ) )
661 {
662
663 nResult = 1;
664 }
665 else
666 {
667 nResult = daoUtil.getInt( 1 );
668 }
669
670 daoUtil.free( );
671
672 return nResult;
673 }
674
675
676
677
678
679
680
681
682 public int maxOrderContactList( Plugin plugin )
683 {
684 int nOrder = 0;
685 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MAX_CONTACT_LIST_ORDER, plugin );
686 daoUtil.executeQuery( );
687
688 if ( daoUtil.next( ) )
689 {
690 nOrder = daoUtil.getInt( 1 );
691 }
692
693 daoUtil.free( );
694
695 return nOrder;
696 }
697
698
699
700
701
702
703
704
705
706
707 public int maxOrderContact( int nIdContactList, Plugin plugin )
708 {
709 int nOrder = 0;
710 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MAX_CONTACT_ORDER, plugin );
711 daoUtil.setInt( 1, nIdContactList );
712 daoUtil.executeQuery( );
713
714 if ( daoUtil.next( ) )
715 {
716 nOrder = daoUtil.getInt( 1 );
717 }
718
719 daoUtil.free( );
720
721 return nOrder;
722 }
723 }