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 import fr.paris.lutece.util.ReferenceList;
40
41 import java.sql.Timestamp;
42 import java.util.Calendar;
43 import java.util.Collection;
44
45 /**
46 * This class provides instances management methods (create, find, ...) for NewsLetter objects
47 */
48 public final class NewsLetterHome
49 {
50 // Static variable pointed at the DAO instance
51 private static INewsLetterDAO _dao = SpringContextService.getBean( "newsletter.newsLetterDAO" );
52
53 /**
54 * Private constructor - this class need not be instantiated
55 */
56 private NewsLetterHome( )
57 {
58 }
59
60 /**
61 * Create an instance of the newsletter
62 *
63 * @param newsLetter
64 * the object to insert into the database
65 * @param plugin
66 * the Plugin
67 * @return the instance created
68 */
69 public static NewsLetter/../../../../../fr/paris/lutece/plugins/newsletter/business/NewsLetter.html#NewsLetter">NewsLetter create( NewsLetter newsLetter, Plugin plugin )
70 {
71 _dao.insert( newsLetter, plugin );
72
73 return newsLetter;
74 }
75
76 /**
77 * update of the newsletter which is specified in parameter
78 *
79 * @param newsLetter
80 * the instance of newsletter which contains the data to store
81 * @param plugin
82 * the Plugin
83 * @return the new instance updated
84 */
85 public static NewsLetter/../../../../../fr/paris/lutece/plugins/newsletter/business/NewsLetter.html#NewsLetter">NewsLetter update( NewsLetter newsLetter, Plugin plugin )
86 {
87 _dao.store( newsLetter, plugin );
88
89 return newsLetter;
90 }
91
92 /**
93 * Remove the record from the identifier a newsletter
94 *
95 * @param nNewsLetterId
96 * the newsletter identifier
97 * @param plugin
98 * the Plugin
99 */
100 public static void remove( int nNewsLetterId, Plugin plugin )
101 {
102 _dao.delete( nNewsLetterId, plugin );
103 }
104
105 ///////////////////////////////////////////////////////////////////////////
106 // Finders
107
108 /**
109 * Returns an object NewsLetter from its identifier
110 *
111 * @param nKey
112 * the primary key of the newsletter
113 * @param plugin
114 * the Plugin
115 * @return an instance of the class
116 */
117 public static NewsLetter findByPrimaryKey( int nKey, Plugin plugin )
118 {
119 return _dao.load( nKey, plugin );
120 }
121
122 /**
123 * Returns a collection of NewsLetter objects
124 *
125 * @param plugin
126 * the Plugin
127 * @return the collection of objects
128 */
129 public static Collection<NewsLetter> findAll( Plugin plugin )
130 {
131 return _dao.selectAll( plugin );
132 }
133
134 /**
135 * Returns a ReferenceList of NewsLetter ids and names
136 *
137 * @param plugin
138 * the Plugin
139 * @return the ReferenceList of id and name
140 */
141 public static ReferenceList findAllId( Plugin plugin )
142 {
143 return _dao.selectAllId( plugin );
144 }
145
146 /**
147 * Gets a collection of NewsLetter associated with a given template
148 *
149 * @param nTemplateId
150 * The id of the template
151 * @param plugin
152 * the Plugin
153 * @return the collection of objects
154 */
155 public static Collection<NewsLetter> findAllByTemplateId( int nTemplateId, Plugin plugin )
156 {
157 return _dao.selectAllByTemplateId( nTemplateId, plugin );
158 }
159
160 ////////////////////////////////////////////////////////////////////////////
161 // Operations
162
163 /**
164 * insert a new subscriber for e newsletter. The subscriber is automatically marked as confirmed
165 *
166 * @param nNewsLetterId
167 * the newsletter identifier
168 * @param nSubscriberId
169 * the subscriber identifier
170 * @param plugin
171 * the Plugin
172 * @param tToday
173 * the day
174 */
175 public static void addSubscriber( int nNewsLetterId, int nSubscriberId, Timestamp tToday, Plugin plugin )
176 {
177 _dao.insertSubscriber( nNewsLetterId, nSubscriberId, tToday, plugin );
178 }
179
180 /**
181 * insert a new subscriber for e newsletter
182 *
183 * @param nNewsLetterId
184 * the newsletter identifier
185 * @param nSubscriberId
186 * the subscriber identifier
187 * @param bValidate
188 * <b>true</b> if user is automatically confirmed, <b>false</b> otherwise
189 * @param plugin
190 * the Plugin
191 * @param tToday
192 * the day
193 */
194 public static void addSubscriber( int nNewsLetterId, int nSubscriberId, boolean bValidate, Timestamp tToday, Plugin plugin )
195 {
196 _dao.insertSubscriber( nNewsLetterId, nSubscriberId, bValidate, tToday, plugin );
197 }
198
199 /**
200 * validates a new subscriber for a newsletter
201 *
202 * @param nNewsLetterId
203 * the newsletter identifier
204 * @param nSubscriberId
205 * the subscriber identifier
206 * @param plugin
207 * the Plugin
208 */
209 public static void validateSubscriber( int nNewsLetterId, int nSubscriberId, Plugin plugin )
210 {
211 _dao.validateSubscriber( nNewsLetterId, nSubscriberId, plugin );
212 }
213
214 /**
215 * removes an subscriber's inscription for a newsletter
216 *
217 * @param nNewsLetterId
218 * the newsletter identifier
219 * @param nSubscriberId
220 * the subscriber identifier
221 * @param plugin
222 * the Plugin
223 */
224 public static void removeSubscriber( int nNewsLetterId, int nSubscriberId, Plugin plugin )
225 {
226 _dao.deleteSubscriber( nNewsLetterId, nSubscriberId, plugin );
227 }
228
229 /**
230 * Performs confirm unsubscription process
231 *
232 * @param nConfirmLimit
233 * How many days before deleting a subscriber
234 * @param plugin
235 * the plugin
236 */
237 public static void removeOldUnconfirmed( int nConfirmLimit, Plugin plugin )
238 {
239 Calendar cal = Calendar.getInstance( );
240 cal.add( Calendar.DATE, -nConfirmLimit );
241
242 Timestamp limitDate = new java.sql.Timestamp( cal.getTimeInMillis( ) );
243 _dao.deleteOldUnconfirmed( limitDate, plugin );
244 }
245
246 /**
247 * Returns the number of subscriber for a newsletter
248 *
249 * @param nNewsLetterId
250 * the identifier of the newsletter
251 * @param plugin
252 * the Plugin
253 * @return the number of subscriber for a newsletter
254 */
255 public static int findNbrSubscribers( int nNewsLetterId, Plugin plugin )
256 {
257 return _dao.selectNbrSubscribers( nNewsLetterId, NewsLetterConstants.CONSTANT_EMPTY_STRING, plugin );
258 }
259
260 /**
261 * Returns the number of active subscriber for a newsletter
262 *
263 * @param nNewsLetterId
264 * the identifier of the newsletter
265 * @param plugin
266 * the Plugin
267 * @return the number of subscriber for a newsletter
268 */
269 public static int findNbrActiveSubscribers( int nNewsLetterId, Plugin plugin )
270 {
271 return _dao.selectNbrActiveSubscribers( nNewsLetterId, NewsLetterConstants.CONSTANT_EMPTY_STRING, plugin );
272 }
273
274 /**
275 * controls that a subscriber is not yet registered for a newsletter
276 *
277 * @param nNewsLetterId
278 * the newsletter identifier
279 * @param nSubscriberId
280 * the subscriber identifier
281 * @param plugin
282 * the Plugin
283 * @return true if he is registered, false if not
284 */
285 public static boolean findRegistration( int nNewsLetterId, int nSubscriberId, Plugin plugin )
286 {
287 return _dao.isRegistered( nNewsLetterId, nSubscriberId, plugin );
288 }
289
290 /**
291 * controls that a template is used by a newsletter
292 *
293 * @param nTemplateId
294 * the template identifier
295 * @param plugin
296 * the Plugin
297 * @return true if the template is used, false if not
298 */
299 public static boolean isTemplateUsedByNewsletter( int nTemplateId, Plugin plugin )
300 {
301 return _dao.isTemplateUsed( nTemplateId, plugin );
302 }
303
304 /**
305 * Verifies if a portlet uses a newsletter
306 *
307 * @return true if portlet uses newsletter
308 * @param nIdNewsletter
309 * the template identifier
310 */
311 public static boolean checkLinkedPortlets( int nIdNewsletter )
312 {
313 return _dao.checkLinkedPortlet( nIdNewsletter );
314 }
315
316 }