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.portal.business.mailinglist;
35
36 import fr.paris.lutece.util.sql.DAOUtil;
37
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.List;
41
42
43
44
45
46 public final class MailingListDAO implements IMailingListDAO
47 {
48
49 private static final String SQL_QUERY_NEW_PK = "SELECT max( id_mailinglist ) FROM core_admin_mailinglist";
50 private static final String SQL_QUERY_SELECT = "SELECT id_mailinglist, name, description, workgroup FROM core_admin_mailinglist WHERE id_mailinglist = ?";
51 private static final String SQL_QUERY_INSERT = "INSERT INTO core_admin_mailinglist ( id_mailinglist, name, description, workgroup ) VALUES ( ?, ?, ?, ? ) ";
52 private static final String SQL_QUERY_DELETE = "DELETE FROM core_admin_mailinglist WHERE id_mailinglist = ? ";
53 private static final String SQL_QUERY_UPDATE = "UPDATE core_admin_mailinglist SET id_mailinglist = ?, name = ?, description = ?, workgroup = ? WHERE id_mailinglist = ?";
54 private static final String SQL_QUERY_SELECTALL = "SELECT id_mailinglist, name, description, workgroup FROM core_admin_mailinglist";
55 private static final String SQL_QUERY_SELECT_BY_WORKGROUP = "SELECT id_mailinglist, name, description, workgroup FROM core_admin_mailinglist WHERE workgroup = ? ";
56
57
58 private static final String SQL_QUERY_FILTERS_INSERT = "INSERT INTO core_admin_mailinglist_filter ( id_mailinglist, workgroup, role ) VALUES ( ?, ?, ? ) ";
59 private static final String SQL_QUERY_FILTERS_DELETE = "DELETE FROM core_admin_mailinglist_filter WHERE id_mailinglist = ? ";
60 private static final String SQL_QUERY_FILTERS_DELETE_FILTER = "DELETE FROM core_admin_mailinglist_filter WHERE id_mailinglist = ? AND workgroup = ? AND role = ? ";
61 private static final String SQL_QUERY_FILTERS_SELECTALL = "SELECT id_mailinglist, workgroup, role FROM core_admin_mailinglist_filter WHERE id_mailinglist = ?";
62 private static final String SQL_QUERY_FILTERS_SELECT = "SELECT id_mailinglist, workgroup, role FROM core_admin_mailinglist_filter WHERE id_mailinglist = ? AND workgroup = ? AND role = ?";
63
64
65
66
67
68
69 public int newPrimaryKey( )
70 {
71 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
72 daoUtil.executeQuery( );
73
74 int nKey;
75
76 if ( !daoUtil.next( ) )
77 {
78
79 nKey = 1;
80 }
81
82 nKey = daoUtil.getInt( 1 ) + 1;
83 daoUtil.free( );
84
85 return nKey;
86 }
87
88
89
90
91
92
93 @Override
94 public synchronized void insert( MailingList mailingList )
95 {
96 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
97
98 mailingList.setId( newPrimaryKey( ) );
99
100 daoUtil.setInt( 1, mailingList.getId( ) );
101 daoUtil.setString( 2, mailingList.getName( ) );
102 daoUtil.setString( 3, mailingList.getDescription( ) );
103 daoUtil.setString( 4, mailingList.getWorkgroup( ) );
104
105 daoUtil.executeUpdate( );
106 daoUtil.free( );
107 }
108
109
110
111
112
113
114
115 @Override
116 public MailingList load( int nId )
117 {
118 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
119 daoUtil.setInt( 1, nId );
120 daoUtil.executeQuery( );
121
122 MailingList mailinglist = null;
123
124 if ( daoUtil.next( ) )
125 {
126 mailinglist = new MailingList( );
127
128 mailinglist.setId( daoUtil.getInt( 1 ) );
129 mailinglist.setName( daoUtil.getString( 2 ) );
130 mailinglist.setDescription( daoUtil.getString( 3 ) );
131 mailinglist.setWorkgroup( daoUtil.getString( 4 ) );
132 }
133
134 daoUtil.free( );
135
136
137 selectMailingListUsersFilters( mailinglist );
138
139 return mailinglist;
140 }
141
142
143
144
145
146
147 @Override
148 public void delete( int nMailingListId )
149 {
150 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
151 daoUtil.setInt( 1, nMailingListId );
152 daoUtil.executeUpdate( );
153 daoUtil.free( );
154
155
156 deleteFilters( nMailingListId );
157 }
158
159
160
161
162
163
164 @Override
165 public void store( MailingList mailingList )
166 {
167 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
168
169 daoUtil.setInt( 1, mailingList.getId( ) );
170 daoUtil.setString( 2, mailingList.getName( ) );
171 daoUtil.setString( 3, mailingList.getDescription( ) );
172 daoUtil.setString( 4, mailingList.getWorkgroup( ) );
173 daoUtil.setInt( 5, mailingList.getId( ) );
174
175 daoUtil.executeUpdate( );
176 daoUtil.free( );
177 }
178
179
180
181
182
183
184 @Override
185 public Collection<MailingList> selectAll( )
186 {
187 Collection<MailingList> mailingListList = new ArrayList<MailingList>( );
188 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
189 daoUtil.executeQuery( );
190
191 while ( daoUtil.next( ) )
192 {
193 MailingList mailingList = new MailingList( );
194
195 mailingList.setId( daoUtil.getInt( 1 ) );
196 mailingList.setName( daoUtil.getString( 2 ) );
197 mailingList.setDescription( daoUtil.getString( 3 ) );
198 mailingList.setWorkgroup( daoUtil.getString( 4 ) );
199
200 mailingListList.add( mailingList );
201 }
202
203 daoUtil.free( );
204
205 return mailingListList;
206 }
207
208
209
210
211
212
213
214 @Override
215 public Collection<MailingList> selectByWorkgroup( String strWorkgroup )
216 {
217 Collection<MailingList> mailingListList = new ArrayList<MailingList>( );
218 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_WORKGROUP );
219 daoUtil.setString( 1, strWorkgroup );
220 daoUtil.executeQuery( );
221
222 while ( daoUtil.next( ) )
223 {
224 MailingList mailingList = new MailingList( );
225
226 mailingList.setId( daoUtil.getInt( 1 ) );
227 mailingList.setName( daoUtil.getString( 2 ) );
228 mailingList.setDescription( daoUtil.getString( 3 ) );
229 mailingList.setWorkgroup( daoUtil.getString( 4 ) );
230
231 mailingListList.add( mailingList );
232 }
233
234 daoUtil.free( );
235
236 return mailingListList;
237 }
238
239
240
241
242
243
244
245 @Override
246 public void insertFilter( MailingListUsersFilter mailingListUsersFilter, int nMailingListId )
247 {
248 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FILTERS_INSERT );
249
250 daoUtil.setInt( 1, nMailingListId );
251 daoUtil.setString( 2, mailingListUsersFilter.getWorkgroup( ) );
252 daoUtil.setString( 3, mailingListUsersFilter.getRole( ) );
253
254 daoUtil.executeUpdate( );
255 daoUtil.free( );
256 }
257
258
259
260
261
262
263 @Override
264 public void deleteFilter( MailingListUsersFilter filter, int nMailingListId )
265 {
266 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FILTERS_DELETE_FILTER );
267 daoUtil.setInt( 1, nMailingListId );
268 daoUtil.setString( 2, filter.getWorkgroup( ) );
269 daoUtil.setString( 3, filter.getRole( ) );
270 daoUtil.executeUpdate( );
271 daoUtil.free( );
272 }
273
274
275
276
277
278
279 public void deleteFilters( int nMailingListUsersFilterId )
280 {
281 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FILTERS_DELETE );
282 daoUtil.setInt( 1, nMailingListUsersFilterId );
283 daoUtil.executeUpdate( );
284 daoUtil.free( );
285 }
286
287
288
289
290
291 public void selectMailingListUsersFilters( MailingList mailinglist )
292 {
293 if ( mailinglist != null )
294 {
295 Collection<MailingListUsersFilter> mailingListUsersFilterList = mailinglist.getFilters( );
296 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FILTERS_SELECTALL );
297 daoUtil.setInt( 1, mailinglist.getId( ) );
298 daoUtil.executeQuery( );
299
300 while ( daoUtil.next( ) )
301 {
302 MailingListUsersFilter mailingListUsersFilter = new MailingListUsersFilter( );
303
304 mailingListUsersFilter.setWorkgroup( daoUtil.getString( 2 ) );
305 mailingListUsersFilter.setRole( daoUtil.getString( 3 ) );
306
307 mailingListUsersFilterList.add( mailingListUsersFilter );
308 }
309
310 daoUtil.free( );
311 }
312 }
313
314
315
316
317 @Override
318 public boolean checkFilter( MailingListUsersFilter filter, int nId )
319 {
320 boolean bExists = false;
321 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FILTERS_SELECT );
322 daoUtil.setInt( 1, nId );
323 daoUtil.setString( 2, filter.getWorkgroup( ) );
324 daoUtil.setString( 3, filter.getRole( ) );
325 daoUtil.executeQuery( );
326
327 if ( daoUtil.next( ) )
328 {
329 bExists = true;
330 }
331
332 daoUtil.free( );
333
334 return bExists;
335 }
336
337
338
339
340 @Override
341 public List<MailingList> selectByFilter( MailingListFilter filter )
342 {
343 List<MailingList> mailingListList = new ArrayList<MailingList>( );
344 DAOUtil daoUtil = new DAOUtil( filter.buildSQLQuery( SQL_QUERY_SELECTALL ) );
345 filter.setFilterValues( daoUtil );
346 daoUtil.executeQuery( );
347
348 while ( daoUtil.next( ) )
349 {
350 int nIndex = 1;
351 MailingList mailingList = new MailingList( );
352
353 mailingList.setId( daoUtil.getInt( nIndex++ ) );
354 mailingList.setName( daoUtil.getString( nIndex++ ) );
355 mailingList.setDescription( daoUtil.getString( nIndex++ ) );
356 mailingList.setWorkgroup( daoUtil.getString( nIndex ) );
357
358 mailingListList.add( mailingList );
359 }
360
361 daoUtil.free( );
362
363 return mailingListList;
364 }
365 }