1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
44
45 public final class SubscriberDAO implements ISubscriberDAO
46 {
47
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
63
64
65
66
67
68
69
70
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
88
89
90
91
92
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
104
105
106
107
108
109
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
132
133
134
135
136
137
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
159
160
161
162
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
175 nKey = 1;
176 }
177
178 nKey = daoUtil.getInt( 1 ) + 1;
179
180 daoUtil.free( );
181
182 return nKey;
183 }
184
185
186
187
188
189
190
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
215
216
217
218
219
220
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
244
245
246
247
248
249
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
276
277
278
279
280
281
282
283
284
285
286
287
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
320
321
322
323
324
325
326
327
328
329
330
331
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
364
365
366
367
368
369
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
384 nCount = 0;
385 }
386
387 nCount = daoUtil.getInt( 1 );
388
389 daoUtil.free( );
390
391 return nCount;
392 }
393
394
395
396
397
398
399
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 }