View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.calendar.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.sql.Timestamp;
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  
44  /**
45   * This class provides Data Access methods for subscriber objects
46   */
47  public final class CalendarSubscriberDAO implements ICalendarSubscriberDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_INSERT = "INSERT INTO calendar_subscriber_details ( id_subscriber , email ) VALUES ( ?, ? )";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM calendar_subscriber_details WHERE id_subscriber = ? ";
52      private static final String SQL_QUERY_SELECT = "SELECT email FROM calendar_subscriber_details WHERE id_subscriber = ? ";
53      private static final String SQL_QUERY_SELECT_ALL = "SELECT id_subscriber, email FROM calendar_subscriber_details ";
54      private static final String SQL_QUERY_SELECT_SUBSCRIBERS_LIST = "SELECT id_subscriber , email FROM calendar_subscriber_details ";
55      private static final String SQL_QUERY_SELECT_BY_EMAIL = "SELECT id_subscriber , email FROM calendar_subscriber_details WHERE email = ? ";
56      private static final String SQL_QUERY_SELECT_SUBSCRIBERS_BY_CALENDAR = "SELECT a.id_subscriber , a.email, b.date_subscription FROM calendar_subscriber_details a, calendar_subscriber b WHERE a.id_subscriber = b.id_subscriber AND b.id_agenda = ? ";
57      private static final String SQL_QUERY_SELECT_SUBSCRIBERS = " SELECT a.id_subscriber , a.email, b.date_subscription FROM calendar_subscriber_details a, calendar_subscriber b WHERE a.id_subscriber = b.id_subscriber AND b.id_agenda = ? AND a.email LIKE ? ORDER BY a.email LIMIT ? OFFSET ? ";
58      private static final String SQL_QUERY_COUNT_CALENDARS_NBR_SUBSCRIBERS = "SELECT count(*) FROM calendar_subscriber_details a, calendar_subscriber b WHERE a.id_subscriber = b.id_subscriber AND b.id_agenda = ? ";
59      private static final String SQL_QUERY_UPDATE = "UPDATE calendar_subscriber SET email = ? WHERE id_subscriber = ?";
60      private static final String SQL_QUERY_CHECK_PRIMARY_KEY = "SELECT id_subscriber FROM calendar_subscriber_details WHERE id_subscriber = ?";
61      private static final String SQL_QUERY_NEW_PRIMARY_KEY = "SELECT max(id_subscriber) FROM calendar_subscriber_details ";
62      private static final String SQL_QUERY_DELETE_FROM_SUBSCRIBER = "DELETE FROM calendar_subscriber WHERE id_agenda = ? and id_subscriber = ? ";
63      private static final String SQL_QUERY_CHECK_IS_REGISTERED = "SELECT id_agenda FROM calendar_subscriber WHERE  id_subscriber = ?  AND id_agenda = ?";
64      private static final String SQL_QUERY_INSERT_SUBSCRIBER = "INSERT INTO calendar_subscriber (  id_subscriber, id_agenda ,date_subscription ) VALUES ( ?, ?, ? )";
65      private static final String SQL_QUERY_CHECK_IS_REGISTERED_TO_ANY_CALENDAR = "SELECT id_agenda FROM calendar_subscriber WHERE id_subscriber = ? LIMIT 1";
66  
67      ///////////////////////////////////////////////////////////////////////////////////////
68      //Access methods to data
69  
70      /**
71       * Insert a new record in the table.
72       * 
73       * @param subscriber the object to be inserted
74       * @param plugin the Plugin
75       */
76      public void insert( CalendarSubscriber subscriber, Plugin plugin )
77      {
78          int nNewPrimaryKey = newPrimaryKey( plugin );
79          subscriber.setId( nNewPrimaryKey );
80  
81          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
82  
83          daoUtil.setInt( 1, subscriber.getId( ) );
84          daoUtil.setString( 2, subscriber.getEmail( ) );
85  
86          daoUtil.executeUpdate( );
87          daoUtil.free( );
88      }
89  
90      /**
91       * Delete a record from the table
92       * 
93       * @param nId the subscriber's identifier
94       * @param plugin the Plugin
95       */
96      public void delete( int nId, Plugin plugin )
97      {
98          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
99          daoUtil.setInt( 1, nId );
100         daoUtil.executeUpdate( );
101         daoUtil.free( );
102     }
103 
104     /**
105      * loads data from a subscriber's identifier
106      * 
107      * @param nId the subscriber's identifier
108      * @param plugin the Plugin
109      * @return an object Subscriber
110      */
111     public CalendarSubscriber load( int nId, Plugin plugin )
112     {
113         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
114         daoUtil.setInt( 1, nId );
115         daoUtil.executeQuery( );
116 
117         CalendarSubscriber subscriber = new CalendarSubscriber( );
118 
119         if ( daoUtil.next( ) )
120         {
121             subscriber.setId( nId );
122             subscriber.setEmail( daoUtil.getString( 1 ) );
123         }
124 
125         daoUtil.free( );
126 
127         return subscriber;
128     }
129 
130     /**
131      * Update the record in the table
132      * 
133      * @param subscriber the instance of subscriber class to be updated
134      * @param plugin the Plugin
135      */
136     public void store( CalendarSubscriber subscriber, Plugin plugin )
137     {
138         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
139         daoUtil.setString( 1, subscriber.getEmail( ) );
140         daoUtil.setInt( 2, subscriber.getId( ) );
141 
142         daoUtil.executeUpdate( );
143         daoUtil.free( );
144     }
145 
146     /**
147      * Check the unicity of a primary key
148      * 
149      * @param nKey the primary key to be checked
150      * @param plugin the Plugin
151      * @return true if the key exists, false if not
152      */
153     boolean checkPrimaryKey( int nKey, Plugin plugin )
154     {
155         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PRIMARY_KEY, plugin );
156         daoUtil.setInt( 1, nKey );
157         daoUtil.executeQuery( );
158 
159         if ( !daoUtil.next( ) )
160         {
161             daoUtil.free( );
162 
163             return false;
164         }
165 
166         daoUtil.free( );
167 
168         return true;
169     }
170 
171     /**
172      * Generates a new primary key
173      * 
174      * @param plugin the Plugin
175      * @return the new primary key
176      */
177     int newPrimaryKey( Plugin plugin )
178     {
179         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, plugin );
180 
181         int nKey;
182 
183         daoUtil.executeQuery( );
184 
185         if ( !daoUtil.next( ) )
186         {
187             // If the table is empty
188             nKey = 1;
189         }
190 
191         nKey = daoUtil.getInt( 1 ) + 1;
192 
193         daoUtil.free( );
194 
195         return nKey;
196     }
197 
198     /**
199      * Loads the list of subscribers
200      * 
201      * @param plugin the Plugin
202      * @return a collection of objects Subscriber
203      */
204     public List<CalendarSubscriber> selectAll( Plugin plugin )
205     {
206         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin );
207         daoUtil.executeQuery( );
208 
209         List<CalendarSubscriber> list = new ArrayList<CalendarSubscriber>( );
210 
211         while ( daoUtil.next( ) )
212         {
213             CalendarSubscriber subscriber = new CalendarSubscriber( );
214             subscriber.setId( daoUtil.getInt( 1 ) );
215             subscriber.setEmail( daoUtil.getString( 2 ) );
216             list.add( subscriber );
217         }
218 
219         daoUtil.free( );
220 
221         return list;
222     }
223 
224     /**
225      * Finds a subscriber from his email
226      * 
227      * @param strEmail the subscriber's email
228      * @param plugin the Plugin
229      * @return a subscriber object if it exists, null if not
230      */
231     public CalendarSubscriber selectByEmail( String strEmail, Plugin plugin )
232     {
233         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_EMAIL, plugin );
234         daoUtil.setString( 1, strEmail.toLowerCase( ) );
235         daoUtil.executeQuery( );
236 
237         CalendarSubscriber subscriber = null;
238 
239         if ( daoUtil.next( ) )
240         {
241             subscriber = new CalendarSubscriber( );
242             subscriber.setId( daoUtil.getInt( 1 ) );
243             subscriber.setEmail( daoUtil.getString( 2 ) );
244         }
245 
246         daoUtil.free( );
247 
248         return subscriber;
249     }
250 
251     /**
252      * loads the list of subscribers for a calendar
253      * 
254      * @param nCalendarId the Calendar identifier
255      * @param plugin the Plugin
256      * @return a collection of subscribers
257      */
258     public List<CalendarSubscriber> selectSubscribers( int nCalendarId, Plugin plugin )
259     {
260         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS_BY_CALENDAR, plugin );
261         daoUtil.setInt( 1, nCalendarId );
262         daoUtil.executeQuery( );
263 
264         List<CalendarSubscriber> list = new ArrayList<CalendarSubscriber>( );
265 
266         while ( daoUtil.next( ) )
267         {
268             CalendarSubscriber subscriber = new CalendarSubscriber( );
269             subscriber.setId( daoUtil.getInt( 1 ) );
270             subscriber.setEmail( daoUtil.getString( 2 ) );
271             subscriber.setDateSubscription( daoUtil.getTimestamp( 3 ) );
272             list.add( subscriber );
273         }
274 
275         daoUtil.free( );
276 
277         return list;
278     }
279 
280     /**
281      * loads the list of subscribers for a Calendar
282      * 
283      * @param nCalendarId the Calendar identifier
284      * @param strSearchString gets all the subscribers if null or empty
285      *            and gets the subscribers whith an email containing this string
286      *            otherwise
287      * @param nBegin the rank of the first subscriber to return
288      * @param nEnd the maximum number of suscribers to return
289      * @param plugin the Plugin
290      * @return a collection of subscribers
291      */
292     public List<CalendarSubscriber> selectSubscribers( int nCalendarId, String strSearchString, int nBegin, int nEnd,
293             Plugin plugin )
294     {
295         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS, plugin );
296 
297         daoUtil.setInt( 1, nCalendarId );
298         daoUtil.setString( 2, "%" + strSearchString + "%" );
299         daoUtil.setInt( 3, nEnd );
300         daoUtil.setInt( 4, nBegin );
301 
302         daoUtil.executeQuery( );
303 
304         List<CalendarSubscriber> list = new ArrayList<CalendarSubscriber>( );
305 
306         while ( daoUtil.next( ) )
307         {
308             CalendarSubscriber subscriber = new CalendarSubscriber( );
309             subscriber.setId( daoUtil.getInt( 1 ) );
310             subscriber.setEmail( daoUtil.getString( 2 ) );
311             subscriber.setDateSubscription( daoUtil.getTimestamp( 3 ) );
312             list.add( subscriber );
313         }
314 
315         daoUtil.free( );
316 
317         return list;
318     }
319 
320     /**
321      * Returns, for a subscriber, the number of his subscriptions
322      * 
323      * @param nSubscriberId the subscriber's identifier
324      * @param plugin the Plugin
325      * @return the number of subscriptions
326      */
327     public int selectSubscriberNumber( int nSubscriberId, Plugin plugin )
328     {
329         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_CALENDARS_NBR_SUBSCRIBERS, plugin );
330 
331         int nCount = 0;
332 
333         daoUtil.setInt( 1, nSubscriberId );
334 
335         daoUtil.executeQuery( );
336 
337         if ( daoUtil.next( ) )
338         {
339             nCount = daoUtil.getInt( 1 );
340         }
341 
342         daoUtil.free( );
343 
344         return nCount;
345     }
346 
347     /**
348      * loads the list of subscribers
349      * 
350      * @param plugin the Plugin
351      * @return a collection of subscribers
352      */
353     public List<CalendarSubscriber> selectSubscribersList( Plugin plugin )
354     {
355         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS_LIST, plugin );
356 
357         daoUtil.executeQuery( );
358 
359         List<CalendarSubscriber> list = new ArrayList<CalendarSubscriber>( );
360 
361         while ( daoUtil.next( ) )
362         {
363             CalendarSubscriber subscriber = new CalendarSubscriber( );
364             subscriber.setId( daoUtil.getInt( 1 ) );
365             subscriber.setEmail( daoUtil.getString( 2 ) );
366             list.add( subscriber );
367         }
368 
369         daoUtil.free( );
370 
371         return list;
372     }
373 
374     /**
375      * Remove the subscriber's inscription to a calendar
376      * 
377      * @param nCalendarId the calendar identifier
378      * @param nSubscriberId the subscriber identifier
379      * @param plugin the Plugin
380      */
381     public void deleteSubscriber( int nCalendarId, int nSubscriberId, Plugin plugin )
382     {
383         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_FROM_SUBSCRIBER, plugin );
384 
385         daoUtil.setInt( 1, nCalendarId );
386         daoUtil.setInt( 2, nSubscriberId );
387 
388         daoUtil.executeUpdate( );
389         daoUtil.free( );
390     }
391 
392     /**
393      * Insert a new subscriber for a calendar
394      * 
395      * @param nCalendarId the calendar identifier
396      * @param nSubscriberId the subscriber identifier
397      * @param tToday The day
398      * @param plugin the Plugin
399      */
400     public void insertSubscriber( int nCalendarId, int nSubscriberId, Timestamp tToday, Plugin plugin )
401     {
402         // Check if the subscriber is yet registered for the calendar
403         if ( isRegistered( nCalendarId, nSubscriberId, plugin ) )
404         {
405             return;
406         }
407 
408         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_SUBSCRIBER, plugin );
409 
410         daoUtil.setInt( 1, nSubscriberId );
411         daoUtil.setInt( 2, nCalendarId );
412         daoUtil.setTimestamp( 3, tToday );
413 
414         daoUtil.executeUpdate( );
415         daoUtil.free( );
416     }
417 
418     /**
419      * check if the subscriber is not yet registered to a calendar
420      * 
421      * @param nCalendarId the calendar identifier
422      * @param nSubscriberId the subscriber identifier
423      * @param plugin the Plugin
424      * @return true if he is registered and false if not
425      */
426     public boolean isRegistered( int nCalendarId, int nSubscriberId, Plugin plugin )
427     {
428         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_IS_REGISTERED, plugin );
429 
430         daoUtil.setInt( 1, nSubscriberId );
431         daoUtil.setInt( 2, nCalendarId );
432 
433         daoUtil.executeQuery( );
434 
435         if ( !daoUtil.next( ) )
436         {
437             daoUtil.free( );
438 
439             return false;
440         }
441 
442         daoUtil.free( );
443 
444         return true;
445     }
446 
447     /**
448      * Check if the user is subscribed to any agenda
449      * @param nSubscriberId the ID of the subscriber
450      * @param plugin plugin
451      * @return true if the user is subscribed to any calendar, false otherwise
452      */
453     public boolean isUserSubscribed( int nSubscriberId, Plugin plugin )
454     {
455         boolean bIsSubscribed = false;
456         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_IS_REGISTERED_TO_ANY_CALENDAR, plugin );
457 
458         daoUtil.setInt( 1, nSubscriberId );
459 
460         daoUtil.executeQuery( );
461 
462         if ( daoUtil.next( ) )
463         {
464             bIsSubscribed = true;
465         }
466         daoUtil.free( );
467 
468         return bIsSubscribed;
469     }
470 }