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.newsletter.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 subscriber objects
44   */
45  public final class SubscriberDAO implements ISubscriberDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_INSERT = "INSERT INTO newsletter_subscriber_details ( id_subscriber , email ) VALUES ( ?, ? )";
49      private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_subscriber_details WHERE id_subscriber = ? ";
50      private static final String SQL_QUERY_SELECT = "SELECT email FROM newsletter_subscriber_details WHERE id_subscriber = ? ";
51      private static final String SQL_QUERY_SELECT_ALL = "SELECT id_subscriber, email FROM newsletter_subscriber_details ";
52      private static final String SQL_QUERY_SELECT_SUBSCRIBERS_LIST = "SELECT id_subscriber , email FROM newsletter_subscriber_details ";
53      private static final String SQL_QUERY_SELECT_BY_EMAIL = "SELECT id_subscriber , email FROM newsletter_subscriber_details WHERE email = ? ";
54      private static final String SQL_QUERY_SELECT_SUBSCRIBERS_BY_NEWSLETTER = "SELECT a.id_subscriber , a.email, b.confirmed, b.date_subscription FROM newsletter_subscriber_details a, newsletter_subscriber b WHERE a.id_subscriber = b.id_subscriber AND b.id_newsletter = ? AND b.confirmed = 1 ";
55      private static final String SQL_QUERY_SELECT_SUBSCRIBERS_FOR_SENDING = " SELECT a.id_subscriber , a.email, b.confirmed, b.date_subscription FROM newsletter_subscriber_details a, newsletter_subscriber b WHERE a.id_subscriber = b.id_subscriber AND b.id_newsletter = ? AND a.email LIKE ? AND confirmed = 1 ORDER BY a.email LIMIT ? OFFSET ? ";
56      private static final String SQL_QUERY_SELECT_SUBSCRIBERS = " SELECT a.id_subscriber , a.email, b.confirmed, b.date_subscription FROM newsletter_subscriber_details a, newsletter_subscriber b WHERE a.id_subscriber = b.id_subscriber AND b.id_newsletter = ? AND a.email LIKE ? ORDER BY a.email LIMIT ? OFFSET ? ";
57      private static final String SQL_QUERY_COUNT_NEWSLETTERS_BY_SUBSCRIBER = "SELECT count(*) FROM newsletter_subscriber where id_subscriber = ? ";
58      private static final String SQL_QUERY_CHECK_PRIMARY_KEY = "SELECT id_subscriber FROM newsletter_subscriber_details WHERE id_subscriber = ?";
59      private static final String SQL_QUERY_NEW_PRIMARY_KEY = "SELECT max(id_subscriber) FROM newsletter_subscriber_details ";
60  
61      ///////////////////////////////////////////////////////////////////////////////////////
62      // Access methods to data
63  
64      /**
65       * Insert a new record in the table.
66       *
67       * @param subscriber
68       *            the object to be inserted
69       * @param plugin
70       *            the Plugin
71       */
72      public void insert( Subscriber subscriber, Plugin plugin )
73      {
74          int nNewPrimaryKey = newPrimaryKey( plugin );
75          subscriber.setId( nNewPrimaryKey );
76  
77          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
78  
79          daoUtil.setInt( 1, subscriber.getId( ) );
80          daoUtil.setString( 2, subscriber.getEmail( ) );
81  
82          daoUtil.executeUpdate( );
83          daoUtil.free( );
84      }
85  
86      /**
87       * Delete a record from the table
88       *
89       * @param nId
90       *            the subscriber's identifier
91       * @param plugin
92       *            the Plugin
93       */
94      public void delete( int nId, Plugin plugin )
95      {
96          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
97          daoUtil.setInt( 1, nId );
98          daoUtil.executeUpdate( );
99          daoUtil.free( );
100     }
101 
102     /**
103      * loads data from a subscriber's identifier
104      *
105      * @param nId
106      *            the subscriber's identifier
107      * @param plugin
108      *            the Plugin
109      * @return an object Subscriber
110      */
111     public Subscriber 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         Subscribertter/business/Subscriber.html#Subscriber">Subscriber subscriber = new Subscriber( );
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      * Check the unicity of a primary key
132      *
133      * @param nKey
134      *            the primary key to be checked
135      * @param plugin
136      *            the Plugin
137      * @return true if the key exists, false if not
138      */
139     boolean checkPrimaryKey( int nKey, Plugin plugin )
140     {
141         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PRIMARY_KEY, plugin );
142         daoUtil.setInt( 1, nKey );
143         daoUtil.executeQuery( );
144 
145         if ( !daoUtil.next( ) )
146         {
147             daoUtil.free( );
148 
149             return false;
150         }
151 
152         daoUtil.free( );
153 
154         return true;
155     }
156 
157     /**
158      * Generates a new primary key
159      *
160      * @param plugin
161      *            the Plugin
162      * @return the new primary key
163      */
164     int newPrimaryKey( Plugin plugin )
165     {
166         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, plugin );
167 
168         int nKey;
169 
170         daoUtil.executeQuery( );
171 
172         if ( !daoUtil.next( ) )
173         {
174             // If the table is empty
175             nKey = 1;
176         }
177 
178         nKey = daoUtil.getInt( 1 ) + 1;
179 
180         daoUtil.free( );
181 
182         return nKey;
183     }
184 
185     /**
186      * Loads the list of subscribers
187      *
188      * @param plugin
189      *            the Plugin
190      * @return a collection of objects Subscriber
191      */
192     public Collection<Subscriber> selectAll( Plugin plugin )
193     {
194         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin );
195         daoUtil.executeQuery( );
196 
197         ArrayList<Subscriber> list = new ArrayList<Subscriber>( );
198 
199         while ( daoUtil.next( ) )
200         {
201             Subscribertter/business/Subscriber.html#Subscriber">Subscriber subscriber = new Subscriber( );
202             subscriber.setId( daoUtil.getInt( 1 ) );
203             subscriber.setEmail( daoUtil.getString( 2 ) );
204             subscriber.setConfirmed( daoUtil.getBoolean( 3 ) );
205             list.add( subscriber );
206         }
207 
208         daoUtil.free( );
209 
210         return list;
211     }
212 
213     /**
214      * Finds a subscriber from his email
215      *
216      * @param strEmail
217      *            the subscriber's email
218      * @param plugin
219      *            the Plugin
220      * @return a subscriber object if it exists, null if not
221      */
222     public Subscriber selectByEmail( String strEmail, Plugin plugin )
223     {
224         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_EMAIL, plugin );
225         daoUtil.setString( 1, strEmail.toLowerCase( ) );
226         daoUtil.executeQuery( );
227 
228         Subscriber subscriber = null;
229 
230         if ( daoUtil.next( ) )
231         {
232             subscriber = new Subscriber( );
233             subscriber.setId( daoUtil.getInt( 1 ) );
234             subscriber.setEmail( daoUtil.getString( 2 ) );
235         }
236 
237         daoUtil.free( );
238 
239         return subscriber;
240     }
241 
242     /**
243      * loads the list of subscribers for a newsletter
244      *
245      * @param nNewsLetterId
246      *            the newsletter identifier
247      * @param plugin
248      *            the Plugin
249      * @return a collection of subscribers
250      */
251     public Collection<Subscriber> selectSubscribers( int nNewsLetterId, Plugin plugin )
252     {
253         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS_BY_NEWSLETTER, plugin );
254         daoUtil.setInt( 1, nNewsLetterId );
255         daoUtil.executeQuery( );
256 
257         ArrayList<Subscriber> list = new ArrayList<Subscriber>( );
258 
259         while ( daoUtil.next( ) )
260         {
261             Subscribertter/business/Subscriber.html#Subscriber">Subscriber subscriber = new Subscriber( );
262             subscriber.setId( daoUtil.getInt( 1 ) );
263             subscriber.setEmail( daoUtil.getString( 2 ) );
264             subscriber.setConfirmed( daoUtil.getBoolean( 3 ) );
265             subscriber.setDateSubscription( daoUtil.getTimestamp( 4 ) );
266             list.add( subscriber );
267         }
268 
269         daoUtil.free( );
270 
271         return list;
272     }
273 
274     /**
275      * loads the list of subscribers for a newsletter
276      *
277      * @param nNewsLetterId
278      *            the newsletter identifier
279      * @param strSearchString
280      *            gets all the subscribers if null or empty and gets the subscribers whith an email containing this string otherwise
281      * @param nBegin
282      *            the rank of the first subscriber to return
283      * @param nEnd
284      *            the maximum number of suscribers to return
285      * @param plugin
286      *            the Plugin
287      * @return a collection of subscribers
288      */
289     public Collection<Subscriber> selectSubscribers( int nNewsLetterId, String strSearchString, int nBegin, int nEnd, Plugin plugin )
290     {
291         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS, plugin );
292 
293         daoUtil.setInt( 1, nNewsLetterId );
294         daoUtil.setString( 2, "%" + strSearchString + "%" );
295         daoUtil.setInt( 3, nEnd );
296         daoUtil.setInt( 4, nBegin );
297 
298         daoUtil.executeQuery( );
299 
300         ArrayList<Subscriber> list = new ArrayList<Subscriber>( );
301 
302         while ( daoUtil.next( ) )
303         {
304             Subscribertter/business/Subscriber.html#Subscriber">Subscriber subscriber = new Subscriber( );
305             subscriber.setId( daoUtil.getInt( 1 ) );
306             subscriber.setEmail( daoUtil.getString( 2 ) );
307             subscriber.setConfirmed( daoUtil.getBoolean( 3 ) );
308             subscriber.setDateSubscription( daoUtil.getTimestamp( 4 ) );
309 
310             list.add( subscriber );
311         }
312 
313         daoUtil.free( );
314 
315         return list;
316     }
317 
318     /**
319      * loads the list of subscribers for a newsletter
320      *
321      * @param nNewsLetterId
322      *            the newsletter identifier
323      * @param strSearchString
324      *            gets all the subscribers if null or empty and gets the subscribers whith an email containing this string otherwise
325      * @param nBegin
326      *            the rank of the first subscriber to return
327      * @param nEnd
328      *            the maximum number of suscribers to return
329      * @param plugin
330      *            the Plugin
331      * @return a collection of subscribers
332      */
333     public Collection<Subscriber> selectSubscribersForSending( int nNewsLetterId, String strSearchString, int nBegin, int nEnd, Plugin plugin )
334     {
335         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS_FOR_SENDING, plugin );
336 
337         daoUtil.setInt( 1, nNewsLetterId );
338         daoUtil.setString( 2, "%" + strSearchString + "%" );
339         daoUtil.setInt( 3, nEnd );
340         daoUtil.setInt( 4, nBegin );
341 
342         daoUtil.executeQuery( );
343 
344         ArrayList<Subscriber> list = new ArrayList<Subscriber>( );
345 
346         while ( daoUtil.next( ) )
347         {
348             Subscribertter/business/Subscriber.html#Subscriber">Subscriber subscriber = new Subscriber( );
349             subscriber.setId( daoUtil.getInt( 1 ) );
350             subscriber.setEmail( daoUtil.getString( 2 ) );
351             subscriber.setConfirmed( daoUtil.getBoolean( 3 ) );
352             subscriber.setDateSubscription( daoUtil.getTimestamp( 4 ) );
353 
354             list.add( subscriber );
355         }
356 
357         daoUtil.free( );
358 
359         return list;
360     }
361 
362     /**
363      * Returns, for a subscriber, the number of his subscriptions
364      *
365      * @param nSubscriberId
366      *            the subscriber's identifier
367      * @param plugin
368      *            the Plugin
369      * @return the number of subscriptions
370      */
371     public int selectNewsLetters( int nSubscriberId, Plugin plugin )
372     {
373         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_NEWSLETTERS_BY_SUBSCRIBER, plugin );
374 
375         int nCount;
376 
377         daoUtil.setInt( 1, nSubscriberId );
378 
379         daoUtil.executeQuery( );
380 
381         if ( !daoUtil.next( ) )
382         {
383             // If the table is empty
384             nCount = 0;
385         }
386 
387         nCount = daoUtil.getInt( 1 );
388 
389         daoUtil.free( );
390 
391         return nCount;
392     }
393 
394     /**
395      * loads the list of subscribers
396      *
397      * @param plugin
398      *            the Plugin
399      * @return a collection of subscribers
400      */
401     public Collection<Subscriber> selectSubscribersList( Plugin plugin )
402     {
403         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS_LIST, plugin );
404 
405         daoUtil.executeQuery( );
406 
407         ArrayList<Subscriber> list = new ArrayList<Subscriber>( );
408 
409         while ( daoUtil.next( ) )
410         {
411             Subscribertter/business/Subscriber.html#Subscriber">Subscriber subscriber = new Subscriber( );
412             subscriber.setId( daoUtil.getInt( 1 ) );
413             subscriber.setEmail( daoUtil.getString( 2 ) );
414             list.add( subscriber );
415         }
416 
417         daoUtil.free( );
418 
419         return list;
420     }
421 }