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.portal.service.spring.SpringContextService;
38  
39  import java.sql.Timestamp;
40  import java.util.List;
41  
42  
43  /**
44   * This class provides instances management methods (create, find, ...) for Subscriber objects
45   */
46  public final class CalendarSubscriberHome
47  {
48      //Properties
49      private static final String CONSTANT_EMPTY_STRING = "";
50  
51      // Static variable pointed at the DAO instance
52      private static ICalendarSubscriberDAO _dao = (ICalendarSubscriberDAO) SpringContextService.getPluginBean( "calendar",
53              "calendar.calendarSubscriberDAO" );
54  
55      /**
56       * Private constructor - this class need not be instantiated
57       */
58      private CalendarSubscriberHome(  )
59      {
60      }
61  
62      /**
63       * Create an instance of the Subscriber class
64       *
65       * @param subscriber the object to insert into the database
66       * @param plugin the plugin
67       * @return the instance created
68       */
69      public static CalendarSubscriber create( CalendarSubscriber subscriber, Plugin plugin )
70      {
71          _dao.insert( subscriber, plugin );
72  
73          return subscriber;
74      }
75  
76      /**
77       * Update of the subscriber's data specified in paramater
78       *
79       * @param subscriber the instance of class which contains the data to store
80       * @param plugin the plugin
81       * @return the instance  of the subscriber updated
82       */
83      public static CalendarSubscriber update( CalendarSubscriber subscriber, Plugin plugin )
84      {
85          _dao.store( subscriber, plugin );
86  
87          return subscriber;
88      }
89  
90      /**
91       * Remove the subscriber whose identifier is specified in parameter
92       *
93       * @param nSubscriberId the subscriber's identifier
94       * @param plugin the plugin
95       */
96      public static void remove( int nSubscriberId, Plugin plugin )
97      {
98          _dao.delete( nSubscriberId, plugin );
99      }
100 
101     ///////////////////////////////////////////////////////////////////////////
102     // Finders
103 
104     /**
105      * Returns an object Subscriber from its identifier
106      *
107      * @param nKey the primary key of the subscriber
108      * @param plugin the plugin
109      * @return an instance of the class
110      */
111     public static CalendarSubscriber findByPrimaryKey( int nKey, Plugin plugin )
112     {
113         return _dao.load( nKey, plugin );
114     }
115 
116     /**
117      * @param plugin the plugin
118      * @return a collection of objects Subscriber
119      */
120     public static List<CalendarSubscriber> findAllSubsriber( Plugin plugin )
121     {
122         return _dao.selectAll( plugin );
123     }
124 
125     /**
126      * Returns a subscriber object from the email
127      *
128      * @param strEmail the subscriber's email
129      * @param plugin the plugin
130      * @return a subscriber object if it exists, null if not
131      */
132     public static CalendarSubscriber findByEmail( String strEmail, Plugin plugin )
133     {
134         return _dao.selectByEmail( strEmail, plugin );
135     }
136 
137     /**
138      * loads the list of subscribers for a calendar
139      *
140      * @param nCalendarId the calendar identifier
141      * @param nBegin the rank of the first subscriber to return
142      * @param nEnd the maximum number of suscribers to return
143      * @param plugin the plugin
144      * @return a collection of subscribers
145      */
146     public static List<CalendarSubscriber> findSubscribers( int nCalendarId, int nBegin, int nEnd, Plugin plugin )
147     {
148         return _dao.selectSubscribers( nCalendarId, CONSTANT_EMPTY_STRING, nBegin, nEnd, plugin );
149     }
150 
151     /**
152      * loads the list of subscribers for a calendar
153      *
154      * @param nCalendarId the calendar identifier
155      * @param strSearchString gets all the subscribers if null or empty
156      *         and gets the subscribers with email containing this string otherwise
157      * @param nBegin the rank of the first subscriber to return
158      * @param nEnd the maximum number of suscribers to return
159      * @param plugin the plugin
160      * @return a collection of subscribers
161      */
162     public static List<CalendarSubscriber> findSubscribers( int nCalendarId, String strSearchString, int nBegin,
163         int nEnd, Plugin plugin )
164     {
165         return _dao.selectSubscribers( nCalendarId, strSearchString, nBegin, nEnd, plugin );
166     }
167 
168     /**
169      * loads the list of subscribers for a calendar
170      *
171      * @param nCalendarId the calendar identifier
172      * @param plugin the plugin
173      * @return a collection of subscribers
174      */
175     public static List<CalendarSubscriber> findSubscribers( int nCalendarId, Plugin plugin )
176     {
177         return _dao.selectSubscribers( nCalendarId, plugin );
178     }
179 
180     /**
181      * Returns, for a subscriber, the number of his subscriptions
182      * 
183      * @param nAgendaId the agenda's identifier
184      * @param plugin the plugin
185      * @return the number of subscriptions
186      */
187     public static int findSubscriberNumber( int nAgendaId, Plugin plugin )
188     {
189         return _dao.selectSubscriberNumber( nAgendaId, plugin );
190     }
191 
192     /**
193      * loads the list of subscribers
194      * @param plugin the plugin
195      * @return a collection of subscribers
196      */
197     public static List<CalendarSubscriber> getSubscribersList( Plugin plugin )
198     {
199         return _dao.selectSubscribersList( plugin );
200     }
201 
202     /**
203      * removes an subscriber's inscription for a calendar
204      *
205      * @param nAgendaId the calendar identifier
206      * @param nSubscriberId the subscriber identifier
207      * @param plugin the Plugin
208      */
209     public static void removeSubscriber( int nSubscriberId, int nAgendaId, Plugin plugin )
210     {
211         _dao.deleteSubscriber( nSubscriberId, nAgendaId, plugin );
212     }
213 
214     /**
215      * insert a new subscriber for e calendar
216      *
217      * @param nCalendarId the calendar identifier
218      * @param nSubscriberId the subscriber indentifier
219      * @param plugin the Plugin
220      * @param tToday the day
221      */
222     public static void addSubscriber( int nCalendarId, int nSubscriberId, Timestamp tToday, Plugin plugin )
223     {
224         _dao.insertSubscriber( nCalendarId, nSubscriberId, tToday, plugin );
225     }
226     
227     /**
228      * Check if the user is subscribed to any agenda
229      * @param nSubscriberId the ID of the subscriber
230      * @param plugin plugin
231      * @return true if the user is subscribed to any agenda, false otherwise
232      */
233     public static boolean isUserSubscribed( int nSubscriberId, Plugin plugin )
234     {
235     	return _dao.isUserSubscribed( nSubscriberId, plugin );
236     }
237 }