View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.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   * This class provides Data Access methods for Contact objects
44   */
45  public final class ContactListDAO implements IContactListDAO
46  {
47      // Constants
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      // CONTACT_LIST ORDER
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      // Access methods to data
77  
78      /**
79       * Generates a new primary key
80       * 
81       * @param plugin
82       *            The plugin
83       * @return The new primary key
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              // if the table is empty
95              nKey = 1;
96          }
97  
98          nKey = daoUtil.getInt( 1 ) + 1;
99  
100         daoUtil.free( );
101 
102         return nKey;
103     }
104 
105     ////////////////////////////////////////////////////////////////////////
106     // Methods using a dynamic pool
107 
108     /**
109      * Insert a new record in the table.
110      * 
111      * @param contactList
112      *            the instance of contactList to insert into DB
113      * @param plugin
114      *            the plugin contact
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      * Load the data of Contact from the table
134      * 
135      * @param nContactListId
136      *            the Id of the contactList to load
137      * @param plugin
138      *            the plugin contact
139      * @return the instance of contactList object loaded
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      * Load the list of contactsList
168      *
169      * @param plugin
170      *            The plugin
171      * @return The Collection of the Contacts
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      * Selects lists for a role key
199      * 
200      * @param strRoleKey
201      *            The role key
202      * @param plugin
203      *            the plugin contact
204      * @return collection of lists
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      * counts how many contacts are associated to the specified list
232      * 
233      * @param nIdContactList
234      *            the Id of contactList
235      * @param plugin
236      *            the plugin contact
237      * @return the number of contacts for the list
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      * counts how many lists the contact is associated to
258      * 
259      * @param nIdContact
260      *            the Id of concerned contact
261      * @param plugin
262      *            the plugin contact
263      * @return the number of counted lists
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      * Returns true if the contactList exists
284      * 
285      * @return boolean the existance of the list
286      * @param nIdContactList
287      *            The if of contactList
288      * @param plugin
289      *            The Plugin object
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      * Selects all contacts associated to a specified list
316      * 
317      * @param nIdContactList
318      *            the id of contactList
319      * @param plugin
320      *            the plugin contact
321      * @return list of contacts
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      * returns true if a contact is assigned to a list
348      * 
349      * @param nIdContact
350      *            The id of the contact
351      * @param nIdContactList
352      *            The id of the contactList
353      * @param plugin
354      *            the plugin contact
355      * @return boolean: true if is assigned, false if not
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      * Inserts 2 keys in association table
383      * 
384      * @param nIdContact
385      *            The id of the contact
386      * @param nIdContactList
387      *            The contact List that will be associated
388      * @param plugin
389      *            The plugin
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      * Unassigns a contact of a list, or a list of a contact
404      * 
405      * @param nIdContact
406      *            the id of the contact
407      * @param nIdContactList
408      *            the id of the contactList
409      * @param plugin
410      *            The plugin
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      * Selects the list of all contacts that are not assigned to the specified list
423      * 
424      * @param nIdContactList
425      *            the id of the contact List
426      * @param plugin
427      *            the plugin contact
428      * @return list of not assigned contacts
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      * Selects assigned lists for a contact
455      * 
456      * @param nIdContact
457      *            the id of the contact
458      * @param plugin
459      *            the plugin contact
460      * @return collection of lists, the contact is associated to
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      * selects all lists, the contact is not associated to
487      * 
488      * @param nIdContact
489      *            the id of the contact
490      * @param plugin
491      *            the plugin contact
492      * @return collection of contactLists
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      * Delete a record from the table
520      * 
521      * @param nIdContactList
522      *            the id of contactlist to delete
523      * @param plugin
524      *            The plugin
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      * Unassigns all contacts for a specified list
536      * 
537      * @param nIdContactList
538      *            the id of contactlist
539      * @param plugin
540      *            the plugin contact
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      * Unassigns all lists, the specified contact is assigned to
552      * 
553      * @param nIdContact
554      *            the Id of the contact
555      * @param plugin
556      *            the plugin contact
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      * Update the record in the table
568      * 
569      * @param contactList
570      *            The reference of contactList
571      * @param plugin
572      *            The plugin
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     // ContactList Order management
593 
594     /**
595      * Modify the order of a contact
596      * 
597      * @param nNewOrder
598      *            The order number
599      * @param nId
600      *            The contactList identifier
601      * @param plugin
602      *            The plugin
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      * Returns a contact identifier in a distinct order
615      * 
616      * @return The order of the ContactList
617      * @param nContactListOrder
618      *            The order number
619      * @param plugin
620      *            The plugin
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             // If number order doesn't exist
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      * Returns the order of a contactList
646      * 
647      * @param nIdContactList
648      *            the id of contactList
649      * @param plugin
650      *            the plugin contact
651      * @return the order of the contactList
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             // If number order doesn't exist
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      * Calculate the new max order in a list
677      * 
678      * @return the max order of contact
679      * @param plugin
680      *            The plugin
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      * Calculate the new max order in a list
700      * 
701      * @return the max order of contact
702      * @param nIdContactList
703      *            the id of the contact list
704      * @param plugin
705      *            The plugin
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 }