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.helpdesk.business;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37 import fr.paris.lutece.util.ReferenceList;
38 import fr.paris.lutece.util.sql.DAOUtil;
39
40 import java.util.ArrayList;
41 import java.util.Collection;
42
43
44
45
46
47 public final class FaqDAO implements IFaqDAO
48 {
49 private static final String SQL_QUERY_NEW_PK = " SELECT max( id_faq ) FROM helpdesk_faq";
50 private static final String SQL_QUERY_SELECT = " SELECT id_faq, name, description, role_key, workgroup_key FROM helpdesk_faq WHERE id_faq = ?";
51 private static final String SQL_QUERY_SELECT_BY_SUBJECT = " SELECT hlfs.id_faq, hf.role_key FROM helpdesk_ln_faq_subject hlfs, helpdesk_faq hf WHERE hlfs.id_subject = ? AND hlfs.id_faq=hf.id_faq";
52 private static final String SQL_QUERY_INSERT = " INSERT INTO helpdesk_faq ( id_faq, name, description, role_key, workgroup_key ) VALUES ( ?, ?, ?, ?, ? )";
53 private static final String SQL_QUERY_DELETE = " DELETE FROM helpdesk_faq WHERE id_faq = ?";
54 private static final String SQL_QUERY_UPDATE = " UPDATE helpdesk_faq SET name = ?, description = ?, role_key = ? , workgroup_key = ? WHERE id_faq = ?";
55 private static final String SQL_QUERY_SELECTALL = " SELECT id_faq, name, description, role_key, workgroup_key FROM helpdesk_faq ORDER BY name ";
56 private static final String SQL_QUERY_SELECT_AUTHORIZED_SELECT = " SELECT id_faq, name, description, role_key, workgroup_key FROM helpdesk_faq WHERE ";
57 private static final String SQL_QUERY_SELECT_AUTHORIZED_ROLE_KEY = " role_key = ? ";
58 private static final String SQL_QUERY_SELECT_AUTHORIZED_WORKGROUP_KEY = " workgroup_key = ? ";
59 private static final String SQL_QUERY_SELECT_AUTHORIZED_OR = " OR ";
60 private static final String SQL_QUERY_SELECT_AUTHORIZED_ORDER_BY = " ORDER BY name ";
61
62
63
64
65
66
67
68
69
70
71 public int newPrimaryKey( Plugin plugin )
72 {
73 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin ) )
74 {
75 daoUtil.executeQuery( );
76
77 int nKey;
78
79 if ( !daoUtil.next( ) )
80 {
81
82 nKey = 1;
83 }
84
85 nKey = daoUtil.getInt( 1 ) + 1;
86
87 return nKey;
88 }
89 }
90
91
92
93
94
95
96
97 public synchronized void insert( Faq faq, Plugin plugin )
98 {
99 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
100 {
101 faq.setId( newPrimaryKey( plugin ) );
102
103 daoUtil.setInt( 1, faq.getId( ) );
104 daoUtil.setString( 2, faq.getName( ) );
105 daoUtil.setString( 3, faq.getDescription( ) );
106 daoUtil.setString( 4, faq.getRoleKey( ) );
107 daoUtil.setString( 5, faq.getWorkgroup( ) );
108
109 daoUtil.executeUpdate( );
110 }
111 }
112
113
114
115
116
117
118
119 public void delete( int nIdFaq, Plugin plugin )
120 {
121 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
122 {
123 daoUtil.setInt( 1, nIdFaq );
124
125 daoUtil.executeUpdate( );
126 }
127 }
128
129
130
131
132
133
134
135
136 public Faq load( int nIdFaq, Plugin plugin )
137 {
138 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
139 {
140 daoUtil.setInt( 1, nIdFaq );
141 daoUtil.executeQuery( );
142
143 Faq faq = null;
144
145 if ( daoUtil.next( ) )
146 {
147 faq = new Faq( );
148 faq.setId( daoUtil.getInt( 1 ) );
149 faq.setName( daoUtil.getString( 2 ) );
150 faq.setDescription( daoUtil.getString( 3 ) );
151 faq.setRoleKey( daoUtil.getString( 4 ) );
152 faq.setWorkgroup( daoUtil.getString( 5 ) );
153 }
154 return faq;
155 }
156 }
157
158
159
160
161
162
163
164 public void store( Faq faq, Plugin plugin )
165 {
166 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
167 {
168 daoUtil.setString( 1, faq.getName( ) );
169 daoUtil.setString( 2, faq.getDescription( ) );
170 daoUtil.setString( 3, faq.getRoleKey( ) );
171 daoUtil.setString( 4, faq.getWorkgroup( ) );
172 daoUtil.setInt( 5, faq.getId( ) );
173
174 daoUtil.executeUpdate( );
175 }
176 }
177
178
179
180
181
182
183 public Collection<Faq> findAll( Plugin plugin )
184 {
185 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
186 {
187 Collection<Faq> list = new ArrayList<>( );
188 daoUtil.executeQuery( );
189
190 while ( daoUtil.next( ) )
191 {
192 Faqplugins/helpdesk/business/Faq.html#Faq">Faq faq = new Faq( );
193 faq.setId( daoUtil.getInt( 1 ) );
194 faq.setName( daoUtil.getString( 2 ) );
195 faq.setDescription( daoUtil.getString( 3 ) );
196 faq.setRoleKey( daoUtil.getString( 4 ) );
197 faq.setWorkgroup( daoUtil.getString( 5 ) );
198 list.add( faq );
199 }
200 return list;
201 }
202 }
203
204
205
206
207
208
209 public ReferenceList findReferenceList( Plugin plugin )
210 {
211 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
212 {
213 ReferenceList list = new ReferenceList( );
214 daoUtil.executeQuery( );
215
216 while ( daoUtil.next( ) )
217 {
218 list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
219 }
220
221 return list;
222 }
223 }
224
225
226
227
228
229
230
231 public Collection<Faq> findAuthorizedFaq( String[] arrayRoleKey, Plugin plugin )
232 {
233 return findByKey( arrayRoleKey, false, plugin );
234 }
235
236
237
238
239
240
241
242 public Collection<Faq> findByWorkgroup( String[] strWorkgroupKey, Plugin plugin )
243 {
244 return findByKey( strWorkgroupKey, true, plugin );
245 }
246
247
248
249
250
251
252
253
254 private Collection<Faq> findByKey( String[] arrayKeys, boolean bAdminWorkgroup, Plugin plugin )
255 {
256 Collection<Faq> list = new ArrayList<>( );
257 StringBuilder sB = new StringBuilder( );
258 sB.append( SQL_QUERY_SELECT_AUTHORIZED_SELECT );
259
260 if ( arrayKeys.length == 0 )
261 {
262 return list;
263 }
264
265 int i = 1;
266
267 for ( String strRoleKey : arrayKeys )
268 {
269 if ( i++ > 1 )
270 {
271 sB.append( SQL_QUERY_SELECT_AUTHORIZED_OR );
272 }
273
274 if ( bAdminWorkgroup )
275 {
276 sB.append( SQL_QUERY_SELECT_AUTHORIZED_WORKGROUP_KEY );
277 }
278 else
279 {
280 sB.append( SQL_QUERY_SELECT_AUTHORIZED_ROLE_KEY );
281 }
282 }
283
284 sB.append( SQL_QUERY_SELECT_AUTHORIZED_ORDER_BY );
285
286 try ( DAOUtil daoUtil = new DAOUtil( sB.toString( ), plugin ) )
287 {
288 i = 1;
289
290 for ( String strKey : arrayKeys )
291 {
292 daoUtil.setString( i++, strKey );
293 }
294
295 daoUtil.executeQuery( );
296
297 while ( daoUtil.next( ) )
298 {
299 Faqplugins/helpdesk/business/Faq.html#Faq">Faq faq = new Faq( );
300 faq.setId( daoUtil.getInt( 1 ) );
301 faq.setName( daoUtil.getString( 2 ) );
302 faq.setDescription( daoUtil.getString( 3 ) );
303 faq.setRoleKey( daoUtil.getString( 4 ) );
304 faq.setWorkgroup( daoUtil.getString( 5 ) );
305 list.add( faq );
306 }
307 }
308 return list;
309 }
310
311
312
313
314
315
316
317 public Faq findBySubjectId( int nSubjectId, Plugin plugin )
318 {
319 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_SUBJECT, plugin ) )
320 {
321 Faqplugins/helpdesk/business/Faq.html#Faq">Faq faq = new Faq( );
322 daoUtil.setInt( 1, nSubjectId );
323 daoUtil.executeQuery( );
324
325 if ( daoUtil.next( ) )
326 {
327 faq.setId( daoUtil.getInt( 1 ) );
328 faq.setRoleKey( daoUtil.getString( 2 ) );
329 }
330 return faq;
331 }
332 }
333
334
335
336
337
338
339 public ReferenceList findListFaq( Plugin plugin )
340 {
341 try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
342 {
343 ReferenceList list = new ReferenceList( );
344 daoUtil.executeQuery( );
345
346 while ( daoUtil.next( ) )
347 {
348 list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
349 }
350 return list;
351 }
352 }
353 }