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