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.ReferenceList;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.Collection;
42  
43  /**
44   * This class provides Data Access methods for Contact objects
45   */
46  public final class ContactDAO implements IContactDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEWPK = "SELECT max( id_contact ) FROM contact ";
50      private static final String SQL_QUERY_SELECT = "SELECT id_contact, description, email, workgroup_key FROM contact WHERE id_contact = ? ";
51      private static final String SQL_QUERY_SELECTALL = "SELECT id_contact, description, email, workgroup_key, hits FROM contact ORDER BY id_contact DESC";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO contact ( id_contact , description, email, workgroup_key, hits )  VALUES ( ? , ? , ?, ?, 0 ) ";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM contact WHERE id_contact = ? ";
54      private static final String SQL_QUERY_UPDATE = "UPDATE contact SET description = ? , email = ?, workgroup_key = ?  WHERE id_contact = ?  ";
55      private static final String SQL_QUERY_SELECTALL_BY_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";
56      private static final String SQL_QUERY_SELECT_CONTACT_HITS = "SELECT hits FROM contact WHERE id_contact = ?";
57      private static final String SQL_QUERY_SELECT_CONTACT_IN_LIST_HITS = "SELECT hits FROM contact_list_contact WHERE id_contact = ? AND id_contact_list = ?";
58      private static final String SQL_QUERY_UPDATE_CONTACT_HITS = "UPDATE contact SET hits = ? WHERE id_contact = ?";
59      private static final String SQL_QUERY_UPDATE_CONTACT_IN_LIST_HITS = "UPDATE contact_list_contact SET hits = ? WHERE id_contact = ? AND id_contact_list = ?";
60  
61      // CONTACT ORDER
62      private static final String SQL_QUERY_SELECT_CONTACT_ID_BY_ORDER = "SELECT id_contact FROM contact_list_contact WHERE contact_order = ? AND id_contact_list = ?";
63      private static final String SQL_QUERY_SELECT_CONTACT_ORDER_BY_ID = "SELECT contact_order FROM contact_list_contact WHERE id_contact = ? AND id_contact_list = ?";
64      private static final String SQL_QUERY_UPDATE_CONTACT_ORDER = "UPDATE contact_list_contact SET contact_order = ?  WHERE id_contact = ? AND id_contact_list = ?";
65  
66      ///////////////////////////////////////////////////////////////////////////////////////
67      // Access methods to data
68  
69      /**
70       * Generates a new primary key
71       * 
72       * @param plugin
73       *            The plugin
74       * @return The new primary key
75       */
76      private int newPrimaryKey( Plugin plugin )
77      {
78          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEWPK, plugin );
79          daoUtil.executeQuery( );
80  
81          int nKey;
82  
83          if ( !daoUtil.next( ) )
84          {
85              // if the table is empty
86              nKey = 1;
87          }
88  
89          nKey = daoUtil.getInt( 1 ) + 1;
90  
91          daoUtil.free( );
92  
93          return nKey;
94      }
95  
96      ////////////////////////////////////////////////////////////////////////
97      // Methods using a dynamic pool
98  
99      /**
100      * Insert a new record in the table.
101      * 
102      * @param contact
103      *            The contact object
104      * @param plugin
105      *            The plugin
106      */
107     public void insert( Contact contact, Plugin plugin )
108     {
109         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
110         contact.setId( newPrimaryKey( plugin ) );
111         daoUtil.setInt( 1, contact.getId( ) );
112         daoUtil.setString( 2, contact.getName( ) );
113         daoUtil.setString( 3, contact.getEmail( ) );
114         daoUtil.setString( 4, contact.getWorkgroup( ) );
115 
116         daoUtil.executeUpdate( );
117         daoUtil.free( );
118     }
119 
120     /**
121      * Load the data of Contact from the table
122      * 
123      * @param nContactId
124      *            The identifier of Contact
125      * @param plugin
126      *            The plugin
127      * @return the instance of the Contact
128      */
129     public Contact load( int nContactId, Plugin plugin )
130     {
131         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
132         daoUtil.setInt( 1, nContactId );
133         daoUtil.executeQuery( );
134 
135         Contact contact = null;
136 
137         if ( daoUtil.next( ) )
138         {
139             contact = new Contact( );
140             contact.setId( daoUtil.getInt( 1 ) );
141             contact.setName( daoUtil.getString( 2 ) );
142             contact.setEmail( daoUtil.getString( 3 ) );
143             contact.setWorkgroup( daoUtil.getString( 4 ) );
144         }
145 
146         daoUtil.free( );
147 
148         return contact;
149     }
150 
151     /**
152      * Delete a record from the table
153      * 
154      * @param contact
155      *            The Contact object
156      * @param plugin
157      *            The plugin
158      */
159     public void delete( Contact contact, Plugin plugin )
160     {
161         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
162         daoUtil.setInt( 1, contact.getId( ) );
163         daoUtil.executeUpdate( );
164         daoUtil.free( );
165     }
166 
167     /**
168      * Update the record in the table
169      * 
170      * @param contact
171      *            The reference of contact
172      * @param plugin
173      *            The plugin
174      */
175     public void store( Contact contact, Plugin plugin )
176     {
177         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
178         int nContactId = contact.getId( );
179 
180         daoUtil.setString( 1, contact.getName( ) );
181         daoUtil.setString( 2, contact.getEmail( ) );
182         daoUtil.setString( 3, contact.getWorkgroup( ) );
183         daoUtil.setInt( 4, nContactId );
184 
185         daoUtil.executeUpdate( );
186         daoUtil.free( );
187     }
188 
189     /**
190      * Load the list of contacts
191      * 
192      * @return The Collection of the Contacts with the string at the top
193      * @param nIdContactList
194      *            The id of contact List
195      * @param strComboItem
196      *            the string to display at the top of the list
197      * @param plugin
198      *            The plugin
199      */
200     public ReferenceList selectContactsByListWithString( int nIdContactList, String strComboItem, Plugin plugin )
201     {
202         ReferenceList contactList = new ReferenceList( );
203         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_BY_LIST, plugin );
204         daoUtil.setInt( 1, nIdContactList );
205         daoUtil.executeQuery( );
206         contactList.addItem( 0, strComboItem );
207 
208         while ( daoUtil.next( ) )
209         {
210             contactList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
211         }
212 
213         daoUtil.free( );
214 
215         return contactList;
216     }
217 
218     /**
219      * Load the list of contacts
220      * 
221      * @param plugin
222      *            The plugin
223      * @return The Collection of the Contacts
224      */
225     public Collection<Contact> selectAll( Plugin plugin )
226     {
227         Collection<Contact> contactList = new ArrayList<Contact>( );
228         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
229         daoUtil.executeQuery( );
230 
231         while ( daoUtil.next( ) )
232         {
233             Contactcontact/business/Contact.html#Contact">Contact contact = new Contact( );
234             contact.setId( daoUtil.getInt( 1 ) );
235             contact.setName( daoUtil.getString( 2 ) );
236             contact.setEmail( daoUtil.getString( 3 ) );
237             contact.setWorkgroup( daoUtil.getString( 4 ) );
238             contact.setHits( daoUtil.getInt( 5 ) );
239             contactList.add( contact );
240         }
241 
242         daoUtil.free( );
243 
244         return contactList;
245     }
246 
247     /**
248      * Update hits of the contact in contact and contact_list_contact tables
249      * 
250      * @param nIdContact
251      *            id of the contact
252      * @param nIdContactList
253      *            id of contact list
254      * @param plugin
255      *            the plugin Contact
256      */
257     public void updateHits( int nIdContact, int nIdContactList, Plugin plugin )
258     {
259         int nNewContactInListHits;
260         int nNewContactHits;
261 
262         nNewContactHits = checkContactHits( nIdContact, plugin ) + 1;
263         nNewContactInListHits = checkContactInListHits( nIdContact, nIdContactList, plugin ) + 1;
264 
265         updateContactHits( nIdContact, nNewContactHits, plugin );
266         updateContactInListHits( nIdContact, nIdContactList, nNewContactInListHits, plugin );
267     }
268 
269     /**
270      * Returns the number of hits of a contact
271      * 
272      * @param nIdContact
273      *            id of the contact
274      * @param plugin
275      *            the plugin Contact
276      * @return the number of hits
277      */
278     private int checkContactHits( int nIdContact, Plugin plugin )
279     {
280         int nHits = 0;
281 
282         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTACT_HITS, plugin );
283         daoUtil.setInt( 1, nIdContact );
284         daoUtil.executeQuery( );
285 
286         if ( daoUtil.next( ) )
287         {
288             nHits = daoUtil.getInt( 1 );
289         }
290 
291         daoUtil.free( );
292 
293         return nHits;
294     }
295 
296     /**
297      * updates the number of hits of a contact
298      * 
299      * @param nIdContact
300      *            id of the contact
301      * @param nNewContactHits
302      *            new number of hits
303      * @param plugin
304      *            the plugin Contact
305      */
306     private void updateContactHits( int nIdContact, int nNewContactHits, Plugin plugin )
307     {
308         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_CONTACT_HITS, plugin );
309         daoUtil.setInt( 1, nNewContactHits );
310         daoUtil.setInt( 2, nIdContact );
311         daoUtil.executeUpdate( );
312         daoUtil.free( );
313     }
314 
315     /**
316      * Returns the number of hits of a contact in a specified list
317      * 
318      * @param nIdContact
319      *            id of the contact
320      * @param nIdContactList
321      *            id of contact list
322      * @param plugin
323      *            the plugin Contact
324      * @return the number of hits
325      */
326     private int checkContactInListHits( int nIdContact, int nIdContactList, Plugin plugin )
327     {
328         int nHits = 0;
329 
330         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTACT_IN_LIST_HITS, plugin );
331         daoUtil.setInt( 1, nIdContact );
332         daoUtil.setInt( 2, nIdContactList );
333         daoUtil.executeQuery( );
334 
335         if ( daoUtil.next( ) )
336         {
337             nHits = daoUtil.getInt( 1 );
338         }
339 
340         daoUtil.free( );
341 
342         return nHits;
343     }
344 
345     /**
346      * updates the number of hits of a contact in a specified list
347      * 
348      * @param nIdContact
349      *            id of the contact
350      * @param nIdContactList
351      *            id of contact list
352      * @param nNewContactInListHits
353      *            the new number of hits
354      * @param plugin
355      *            the plugin Contact
356      */
357     private void updateContactInListHits( int nIdContact, int nIdContactList, int nNewContactInListHits, Plugin plugin )
358     {
359         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_CONTACT_IN_LIST_HITS, plugin );
360         daoUtil.setInt( 1, nNewContactInListHits );
361         daoUtil.setInt( 2, nIdContact );
362         daoUtil.setInt( 3, nIdContactList );
363         daoUtil.executeUpdate( );
364         daoUtil.free( );
365     }
366 
367     ////////////////////////////////////////////////////////////////////////////
368     // Contact Order management
369 
370     /**
371      * Modify the order of a contact
372      * 
373      * @param nIdContactList
374      *            The id of contactList
375      * @param nNewOrder
376      *            The order number
377      * @param nId
378      *            The contact identifier
379      * @param plugin
380      *            The plugin
381      */
382     public void storeContactOrder( int nNewOrder, int nId, int nIdContactList, Plugin plugin )
383     {
384         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_CONTACT_ORDER, plugin );
385         daoUtil.setInt( 1, nNewOrder );
386         daoUtil.setInt( 2, nId );
387         daoUtil.setInt( 3, nIdContactList );
388         daoUtil.executeUpdate( );
389         daoUtil.free( );
390     }
391 
392     /**
393      * Returns a contact identifier in a distinct order
394      * 
395      * @return The order of the Contact
396      * @param nIdContactList
397      *            the id of the contact List
398      * @param nContactOrder
399      *            The order number
400      * @param plugin
401      *            The plugin
402      */
403     public int selectContactIdByOrder( int nContactOrder, int nIdContactList, Plugin plugin )
404     {
405         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTACT_ID_BY_ORDER, plugin );
406         int nResult;
407         daoUtil.setInt( 1, nContactOrder );
408         daoUtil.setInt( 2, nIdContactList );
409         daoUtil.executeQuery( );
410 
411         if ( !daoUtil.next( ) )
412         {
413             // If number order doesn't exist
414             nResult = 1;
415         }
416         else
417         {
418             nResult = daoUtil.getInt( 1 );
419         }
420 
421         daoUtil.free( );
422 
423         return nResult;
424     }
425 
426     /**
427      * Returns the order of a contact in a list, using the Identifier
428      * 
429      * @param nIdContact
430      *            the id of contact
431      * @param nIdContactList
432      *            the id of contactList
433      * @param plugin
434      *            the plugin contact
435      * @return the order of the contact in the list
436      */
437     public int selectContactOrderById( int nIdContact, int nIdContactList, Plugin plugin )
438     {
439         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTACT_ORDER_BY_ID, plugin );
440         int nResult;
441         daoUtil.setInt( 1, nIdContact );
442         daoUtil.setInt( 2, nIdContactList );
443         daoUtil.executeQuery( );
444 
445         if ( !daoUtil.next( ) )
446         {
447             // If number order doesn't exist
448             nResult = 1;
449         }
450         else
451         {
452             nResult = daoUtil.getInt( 1 );
453         }
454 
455         daoUtil.free( );
456 
457         return nResult;
458     }
459 }