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.contact.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 public final class ContactDAO implements IContactDAO
47 {
48
49 private static final String SQL_QUERY_NEWPK = "SELECT max( id_contact ) FROM contact ";
50 private static final String SQL_QUERY_SELECT = "SELECT id_contact, description, email, workgroup_key FROM contact WHERE id_contact = ? ";
51 private static final String SQL_QUERY_SELECTALL = "SELECT id_contact, description, email, workgroup_key, hits FROM contact ORDER BY id_contact DESC";
52 private static final String SQL_QUERY_INSERT = "INSERT INTO contact ( id_contact , description, email, workgroup_key, hits ) VALUES ( ? , ? , ?, ?, 0 ) ";
53 private static final String SQL_QUERY_DELETE = "DELETE FROM contact WHERE id_contact = ? ";
54 private static final String SQL_QUERY_UPDATE = "UPDATE contact SET description = ? , email = ?, workgroup_key = ? WHERE id_contact = ? ";
55 private static final String SQL_QUERY_SELECTALL_BY_LIST = "SELECT a.id_contact, b.description, b.email, a.contact_order FROM contact_list_contact a, contact b WHERE id_contact_list = ? AND b.id_contact=a.id_contact ORDER BY a.contact_order";
56 private static final String SQL_QUERY_SELECT_CONTACT_HITS = "SELECT hits FROM contact WHERE id_contact = ?";
57 private static final String SQL_QUERY_SELECT_CONTACT_IN_LIST_HITS = "SELECT hits FROM contact_list_contact WHERE id_contact = ? AND id_contact_list = ?";
58 private static final String SQL_QUERY_UPDATE_CONTACT_HITS = "UPDATE contact SET hits = ? WHERE id_contact = ?";
59 private static final String SQL_QUERY_UPDATE_CONTACT_IN_LIST_HITS = "UPDATE contact_list_contact SET hits = ? WHERE id_contact = ? AND id_contact_list = ?";
60
61
62 private static final String SQL_QUERY_SELECT_CONTACT_ID_BY_ORDER = "SELECT id_contact FROM contact_list_contact WHERE contact_order = ? AND id_contact_list = ?";
63 private static final String SQL_QUERY_SELECT_CONTACT_ORDER_BY_ID = "SELECT contact_order FROM contact_list_contact WHERE id_contact = ? AND id_contact_list = ?";
64 private static final String SQL_QUERY_UPDATE_CONTACT_ORDER = "UPDATE contact_list_contact SET contact_order = ? WHERE id_contact = ? AND id_contact_list = ?";
65
66
67
68
69
70
71
72
73
74
75
76 private int newPrimaryKey( Plugin plugin )
77 {
78 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEWPK, plugin );
79 daoUtil.executeQuery( );
80
81 int nKey;
82
83 if ( !daoUtil.next( ) )
84 {
85
86 nKey = 1;
87 }
88
89 nKey = daoUtil.getInt( 1 ) + 1;
90
91 daoUtil.free( );
92
93 return nKey;
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107 public void insert( Contact contact, Plugin plugin )
108 {
109 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
110 contact.setId( newPrimaryKey( plugin ) );
111 daoUtil.setInt( 1, contact.getId( ) );
112 daoUtil.setString( 2, contact.getName( ) );
113 daoUtil.setString( 3, contact.getEmail( ) );
114 daoUtil.setString( 4, contact.getWorkgroup( ) );
115
116 daoUtil.executeUpdate( );
117 daoUtil.free( );
118 }
119
120
121
122
123
124
125
126
127
128
129 public Contact load( int nContactId, Plugin plugin )
130 {
131 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
132 daoUtil.setInt( 1, nContactId );
133 daoUtil.executeQuery( );
134
135 Contact contact = null;
136
137 if ( daoUtil.next( ) )
138 {
139 contact = new Contact( );
140 contact.setId( daoUtil.getInt( 1 ) );
141 contact.setName( daoUtil.getString( 2 ) );
142 contact.setEmail( daoUtil.getString( 3 ) );
143 contact.setWorkgroup( daoUtil.getString( 4 ) );
144 }
145
146 daoUtil.free( );
147
148 return contact;
149 }
150
151
152
153
154
155
156
157
158
159 public void delete( Contact contact, Plugin plugin )
160 {
161 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
162 daoUtil.setInt( 1, contact.getId( ) );
163 daoUtil.executeUpdate( );
164 daoUtil.free( );
165 }
166
167
168
169
170
171
172
173
174
175 public void store( Contact contact, Plugin plugin )
176 {
177 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
178 int nContactId = contact.getId( );
179
180 daoUtil.setString( 1, contact.getName( ) );
181 daoUtil.setString( 2, contact.getEmail( ) );
182 daoUtil.setString( 3, contact.getWorkgroup( ) );
183 daoUtil.setInt( 4, nContactId );
184
185 daoUtil.executeUpdate( );
186 daoUtil.free( );
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200 public ReferenceList selectContactsByListWithString( int nIdContactList, String strComboItem, Plugin plugin )
201 {
202 ReferenceList contactList = new ReferenceList( );
203 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_BY_LIST, plugin );
204 daoUtil.setInt( 1, nIdContactList );
205 daoUtil.executeQuery( );
206 contactList.addItem( 0, strComboItem );
207
208 while ( daoUtil.next( ) )
209 {
210 contactList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
211 }
212
213 daoUtil.free( );
214
215 return contactList;
216 }
217
218
219
220
221
222
223
224
225 public Collection<Contact> selectAll( Plugin plugin )
226 {
227 Collection<Contact> contactList = new ArrayList<Contact>( );
228 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
229 daoUtil.executeQuery( );
230
231 while ( daoUtil.next( ) )
232 {
233 Contactcontact/business/Contact.html#Contact">Contact contact = new Contact( );
234 contact.setId( daoUtil.getInt( 1 ) );
235 contact.setName( daoUtil.getString( 2 ) );
236 contact.setEmail( daoUtil.getString( 3 ) );
237 contact.setWorkgroup( daoUtil.getString( 4 ) );
238 contact.setHits( daoUtil.getInt( 5 ) );
239 contactList.add( contact );
240 }
241
242 daoUtil.free( );
243
244 return contactList;
245 }
246
247
248
249
250
251
252
253
254
255
256
257 public void updateHits( int nIdContact, int nIdContactList, Plugin plugin )
258 {
259 int nNewContactInListHits;
260 int nNewContactHits;
261
262 nNewContactHits = checkContactHits( nIdContact, plugin ) + 1;
263 nNewContactInListHits = checkContactInListHits( nIdContact, nIdContactList, plugin ) + 1;
264
265 updateContactHits( nIdContact, nNewContactHits, plugin );
266 updateContactInListHits( nIdContact, nIdContactList, nNewContactInListHits, plugin );
267 }
268
269
270
271
272
273
274
275
276
277
278 private int checkContactHits( int nIdContact, Plugin plugin )
279 {
280 int nHits = 0;
281
282 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTACT_HITS, plugin );
283 daoUtil.setInt( 1, nIdContact );
284 daoUtil.executeQuery( );
285
286 if ( daoUtil.next( ) )
287 {
288 nHits = daoUtil.getInt( 1 );
289 }
290
291 daoUtil.free( );
292
293 return nHits;
294 }
295
296
297
298
299
300
301
302
303
304
305
306 private void updateContactHits( int nIdContact, int nNewContactHits, Plugin plugin )
307 {
308 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_CONTACT_HITS, plugin );
309 daoUtil.setInt( 1, nNewContactHits );
310 daoUtil.setInt( 2, nIdContact );
311 daoUtil.executeUpdate( );
312 daoUtil.free( );
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326 private int checkContactInListHits( int nIdContact, int nIdContactList, Plugin plugin )
327 {
328 int nHits = 0;
329
330 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTACT_IN_LIST_HITS, plugin );
331 daoUtil.setInt( 1, nIdContact );
332 daoUtil.setInt( 2, nIdContactList );
333 daoUtil.executeQuery( );
334
335 if ( daoUtil.next( ) )
336 {
337 nHits = daoUtil.getInt( 1 );
338 }
339
340 daoUtil.free( );
341
342 return nHits;
343 }
344
345
346
347
348
349
350
351
352
353
354
355
356
357 private void updateContactInListHits( int nIdContact, int nIdContactList, int nNewContactInListHits, Plugin plugin )
358 {
359 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_CONTACT_IN_LIST_HITS, plugin );
360 daoUtil.setInt( 1, nNewContactInListHits );
361 daoUtil.setInt( 2, nIdContact );
362 daoUtil.setInt( 3, nIdContactList );
363 daoUtil.executeUpdate( );
364 daoUtil.free( );
365 }
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382 public void storeContactOrder( int nNewOrder, int nId, int nIdContactList, Plugin plugin )
383 {
384 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_CONTACT_ORDER, plugin );
385 daoUtil.setInt( 1, nNewOrder );
386 daoUtil.setInt( 2, nId );
387 daoUtil.setInt( 3, nIdContactList );
388 daoUtil.executeUpdate( );
389 daoUtil.free( );
390 }
391
392
393
394
395
396
397
398
399
400
401
402
403 public int selectContactIdByOrder( int nContactOrder, int nIdContactList, Plugin plugin )
404 {
405 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTACT_ID_BY_ORDER, plugin );
406 int nResult;
407 daoUtil.setInt( 1, nContactOrder );
408 daoUtil.setInt( 2, nIdContactList );
409 daoUtil.executeQuery( );
410
411 if ( !daoUtil.next( ) )
412 {
413
414 nResult = 1;
415 }
416 else
417 {
418 nResult = daoUtil.getInt( 1 );
419 }
420
421 daoUtil.free( );
422
423 return nResult;
424 }
425
426
427
428
429
430
431
432
433
434
435
436
437 public int selectContactOrderById( int nIdContact, int nIdContactList, Plugin plugin )
438 {
439 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTACT_ORDER_BY_ID, plugin );
440 int nResult;
441 daoUtil.setInt( 1, nIdContact );
442 daoUtil.setInt( 2, nIdContactList );
443 daoUtil.executeQuery( );
444
445 if ( !daoUtil.next( ) )
446 {
447
448 nResult = 1;
449 }
450 else
451 {
452 nResult = daoUtil.getInt( 1 );
453 }
454
455 daoUtil.free( );
456
457 return nResult;
458 }
459 }