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.plugins.newsletter.util.NewsLetterConstants;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.portal.service.spring.SpringContextService;
39  
40  import java.util.Collection;
41  
42  /**
43   * This class provides instacnes management methods (create, find, ...) for Subscriber objects
44   */
45  public final class SubscriberHome
46  {
47      // Static variable pointed at the DAO instance
48      private static ISubscriberDAO _dao = SpringContextService.getBean( "newsletter.subscriberDAO" );
49  
50      /**
51       * Private constructor - this class need not be instantiated
52       */
53      private SubscriberHome( )
54      {
55      }
56  
57      /**
58       * Create an instance of the Subscriber class
59       *
60       * @param subscriber
61       *            the object to insert into the database
62       * @param plugin
63       *            the plugin
64       * @return the instance created
65       */
66      public static Subscriber/../../../../../fr/paris/lutece/plugins/newsletter/business/Subscriber.html#Subscriber">Subscriber create( Subscriber subscriber, Plugin plugin )
67      {
68          _dao.insert( subscriber, plugin );
69  
70          return subscriber;
71      }
72  
73      /**
74       * Remove the subscriber whose identifier is specified in parameter
75       *
76       * @param nSubscriberId
77       *            the subscriber's identifier
78       * @param plugin
79       *            the plugin
80       */
81      public static void remove( int nSubscriberId, Plugin plugin )
82      {
83          _dao.delete( nSubscriberId, plugin );
84      }
85  
86      ///////////////////////////////////////////////////////////////////////////
87      // Finders
88  
89      /**
90       * Returns an object Subscriber from its identifier
91       *
92       * @param nKey
93       *            the primary key of the subscriber
94       * @param plugin
95       *            the plugin
96       * @return an instance of the class
97       */
98      public static Subscriber findByPrimaryKey( int nKey, Plugin plugin )
99      {
100         return _dao.load( nKey, plugin );
101     }
102 
103     /**
104      * @param plugin
105      *            the plugin
106      * @return a collection of objects Subscriber
107      */
108     public static Collection<Subscriber> findAll( Plugin plugin )
109     {
110         return _dao.selectAll( plugin );
111     }
112 
113     /**
114      * Returns a subscriber object from the email
115      *
116      * @param strEmail
117      *            the subscriber's email
118      * @param plugin
119      *            the plugin
120      * @return a subscriber object if it exists, null if not
121      */
122     public static Subscriber findByEmail( String strEmail, Plugin plugin )
123     {
124         return _dao.selectByEmail( strEmail, plugin );
125     }
126 
127     /**
128      * loads the list of subscribers for a newsletter
129      *
130      * @param nNewsLetterId
131      *            the newsletter identifier
132      * @param nBegin
133      *            the rank of the first subscriber to return
134      * @param nEnd
135      *            the maximum number of suscribers to return
136      * @param plugin
137      *            the plugin
138      * @return a collection of subscribers
139      */
140     public static Collection<Subscriber> findSubscribers( int nNewsLetterId, int nBegin, int nEnd, Plugin plugin )
141     {
142         return _dao.selectSubscribers( nNewsLetterId, NewsLetterConstants.CONSTANT_EMPTY_STRING, nBegin, nEnd, plugin );
143     }
144 
145     /**
146      * Find the collection of subscribers to send a newsletter
147      * 
148      * @param nNewsLetterId
149      *            The id of the newsletter
150      * @param nBegin
151      *            The begin index of subscribers to get
152      * @param nEnd
153      *            The end index of subscribers to get
154      * @param plugin
155      *            The plugin
156      * @return The collection of subscribers found
157      */
158     public static Collection<Subscriber> findSubscribersForSending( int nNewsLetterId, int nBegin, int nEnd, Plugin plugin )
159     {
160         return _dao.selectSubscribersForSending( nNewsLetterId, NewsLetterConstants.CONSTANT_EMPTY_STRING, nBegin, nEnd, plugin );
161     }
162 
163     /**
164      * loads the list of subscribers for a newsletter
165      *
166      * @param nNewsLetterId
167      *            the newsletter identifier
168      * @param strSearchString
169      *            gets all the subscribers if null or empty and gets the subscribers whith an email containing this string otherwise
170      * @param nBegin
171      *            the rank of the first subscriber to return
172      * @param nEnd
173      *            the maximum number of suscribers to return
174      * @param plugin
175      *            the plugin
176      * @return a collection of subscribers
177      */
178     public static Collection<Subscriber> findSubscribers( int nNewsLetterId, String strSearchString, int nBegin, int nEnd, Plugin plugin )
179     {
180         return _dao.selectSubscribers( nNewsLetterId, strSearchString, nBegin, nEnd, plugin );
181     }
182 
183     /**
184      * loads the list of subscribers for a newsletter
185      *
186      * @param nNewsLetterId
187      *            the newsletter identifier
188      * @param plugin
189      *            the plugin
190      * @return a collection of subscribers
191      */
192     public static Collection<Subscriber> findSubscribers( int nNewsLetterId, Plugin plugin )
193     {
194         return _dao.selectSubscribers( nNewsLetterId, plugin );
195     }
196 
197     /**
198      * Returns, for a subscriber, the number of his subscriptions
199      *
200      * @param nSubscriberId
201      *            the subscriber's identifier
202      * @param plugin
203      *            the plugin
204      * @return the number of subscriptions
205      */
206     public static int findNewsLetters( int nSubscriberId, Plugin plugin )
207     {
208         return _dao.selectNewsLetters( nSubscriberId, plugin );
209     }
210 
211     /**
212      * loads the list of subscribers
213      * 
214      * @param plugin
215      *            the plugin
216      * @return a collection of subscribers
217      */
218     public static Collection<Subscriber> getSubscribersList( Plugin plugin )
219     {
220         return _dao.selectSubscribersList( plugin );
221     }
222 }