1
2
3
4
5
6 package fr.ifremer.quadrige2.core.dao.referential.taxon;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 import fr.ifremer.quadrige2.core.dao.PrincipalStore;
30 import fr.ifremer.quadrige2.core.dao.PropertySearch;
31 import fr.ifremer.quadrige2.core.dao.Search;
32 import fr.ifremer.quadrige2.core.dao.referential.Status;
33 import fr.ifremer.quadrige2.core.dao.technical.hibernate.HibernateDaoSupport;
34 import java.security.Principal;
35 import java.sql.Timestamp;
36 import java.util.Collection;
37 import java.util.Date;
38 import java.util.LinkedHashSet;
39 import java.util.List;
40 import java.util.Set;
41 import javax.annotation.Resource;
42 import org.andromda.spring.PaginationResult;
43 import org.apache.commons.collections.CollectionUtils;
44 import org.apache.commons.collections.Transformer;
45 import org.hibernate.Criteria;
46 import org.hibernate.HibernateException;
47 import org.hibernate.Query;
48 import org.hibernate.ScrollableResults;
49
50
51
52
53
54
55
56
57
58 public abstract class CitationDaoBase
59 extends HibernateDaoSupport
60 implements CitationDao
61 {
62
63
64
65 @Override
66 public Object get(final int transform, final Integer citId)
67 {
68 if (citId == null)
69 {
70 throw new IllegalArgumentException(
71 "Citation.get - 'citId' can not be null");
72 }
73 final Citation entity = get(CitationImpl.class, citId);
74 return transformEntity(transform, entity);
75 }
76
77
78
79 @Override
80 public Citation get(Integer citId)
81 {
82 return (Citation)this.get(TRANSFORM_NONE, citId);
83 }
84
85
86
87
88 @Override
89 public Object load(final int transform, final Integer citId)
90 {
91 if (citId == null)
92 {
93 throw new IllegalArgumentException(
94 "Citation.load - 'citId' can not be null");
95 }
96 final Citation entity = get(CitationImpl.class, citId);
97 return transformEntity(transform, entity);
98 }
99
100
101
102
103 @Override
104 public Citation load(Integer citId)
105 {
106 return (Citation)this.load(TRANSFORM_NONE, citId);
107 }
108
109
110
111
112 @Override
113 @SuppressWarnings({"unchecked"})
114 public Collection<Citation> loadAll()
115 {
116 return (Collection<Citation>) this.loadAll(CitationDao.TRANSFORM_NONE);
117 }
118
119
120
121
122 @Override
123 public Collection<?> loadAll(final int transform)
124 {
125 return this.loadAll(transform, -1, -1);
126 }
127
128
129
130
131 @Override
132 public Collection<?> loadAll(final int pageNumber, final int pageSize)
133 {
134 return this.loadAll(CitationDao.TRANSFORM_NONE, pageNumber, pageSize);
135 }
136
137
138
139
140 @Override
141 public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
142 {
143 try
144 {
145 final Criteria criteria = this.getSession().createCriteria(CitationImpl.class);
146 if (pageNumber > 0 && pageSize > 0)
147 {
148 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
149 criteria.setMaxResults(pageSize);
150 }
151 final Collection<?> results = criteria.list();
152 this.transformEntities(transform, results);
153 return results;
154 }
155 catch (HibernateException ex)
156 {
157 throw ex;
158 }
159 }
160
161
162
163
164
165
166
167 protected int calculateFirstResult(int pageNumber, int pageSize)
168 {
169 int firstResult = 0;
170 if (pageNumber > 0)
171 {
172 firstResult = (pageNumber - 1) * pageSize;
173 }
174 return firstResult;
175 }
176
177
178
179
180 @Override
181 public Citation create(Citation citation)
182 {
183 return (Citation)this.create(CitationDao.TRANSFORM_NONE, citation);
184 }
185
186
187
188
189 @Override
190 public Object create(final int transform, final Citation citation)
191 {
192 if (citation == null)
193 {
194 throw new IllegalArgumentException(
195 "Citation.create - 'citation' can not be null");
196 }
197 this.getSessionFactory().getCurrentSession().save(citation);
198 return this.transformEntity(transform, citation);
199 }
200
201
202
203
204 @Override
205 @SuppressWarnings({"unchecked"})
206 public Collection<Citation> create(final Collection<Citation> entities)
207 {
208 return (Collection<Citation>) create(CitationDao.TRANSFORM_NONE, entities);
209 }
210
211
212
213
214 @Override
215 public Collection<?> create(final int transform, final Collection<Citation> entities)
216 {
217 if (entities == null)
218 {
219 throw new IllegalArgumentException(
220 "Citation.create - 'entities' can not be null");
221 }
222 for (Citation entity : entities)
223 {
224 create(transform, entity);
225 }
226 return entities;
227 }
228
229
230
231
232 @Override
233 public Citation create(
234 String citNm,
235 Date citCreationDt,
236 Timestamp updateDt)
237 {
238 return (Citation)this.create(CitationDao.TRANSFORM_NONE, citNm, citCreationDt, updateDt);
239 }
240
241
242
243
244 @Override
245 public Object create(
246 final int transform,
247 String citNm,
248 Date citCreationDt,
249 Timestamp updateDt)
250 {
251 Citation entity = new CitationImpl();
252 entity.setCitNm(citNm);
253 entity.setCitCreationDt(citCreationDt);
254 entity.setUpdateDt(updateDt);
255 return this.create(transform, entity);
256 }
257
258
259
260
261 @Override
262 public Citation create(
263 Timestamp updateDt,
264 Status status)
265 {
266 return (Citation)this.create(CitationDao.TRANSFORM_NONE, updateDt, status);
267 }
268
269
270
271
272 @Override
273 public Object create(
274 final int transform,
275 Timestamp updateDt,
276 Status status)
277 {
278 Citation entity = new CitationImpl();
279 entity.setUpdateDt(updateDt);
280 entity.setStatus(status);
281 return this.create(transform, entity);
282 }
283
284
285
286
287 @Override
288 public void update(Citation citation)
289 {
290 if (citation == null)
291 {
292 throw new IllegalArgumentException(
293 "Citation.update - 'citation' can not be null");
294 }
295 this.getSessionFactory().getCurrentSession().update(citation);
296 }
297
298
299
300
301 @Override
302 public void update(final Collection<Citation> entities)
303 {
304 if (entities == null)
305 {
306 throw new IllegalArgumentException(
307 "Citation.update - 'entities' can not be null");
308 }
309 for (Citation entity : entities)
310 {
311 update(entity);
312 }
313 }
314
315
316
317
318 @Override
319 public void remove(Citation citation)
320 {
321 if (citation == null)
322 {
323 throw new IllegalArgumentException(
324 "Citation.remove - 'citation' can not be null");
325 }
326 this.getSessionFactory().getCurrentSession().delete(citation);
327 }
328
329
330
331
332 @Override
333 public void remove(Integer citId)
334 {
335 if (citId == null)
336 {
337 throw new IllegalArgumentException(
338 "Citation.remove - 'citId' can not be null");
339 }
340 Citation entity = this.get(citId);
341 if (entity != null)
342 {
343 this.remove(entity);
344 }
345 }
346
347
348
349
350 @Override
351 public void remove(Collection<Citation> entities)
352 {
353 if (entities == null)
354 {
355 throw new IllegalArgumentException(
356 "Citation.remove - 'entities' can not be null");
357 }
358 deleteAll(entities);
359 }
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374 public Object transformEntity(final int transform, final Citation entity)
375 {
376 Object target = null;
377 if (entity != null)
378 {
379 switch (transform)
380 {
381 case CitationDao.TRANSFORM_NONE :
382 default:
383 target = entity;
384 }
385 }
386 return target;
387 }
388
389
390
391
392 @Override
393 public void transformEntities(final int transform, final Collection<?> entities)
394 {
395 switch (transform)
396 {
397 case CitationDao.TRANSFORM_NONE :
398 default:
399
400 }
401 }
402
403
404
405
406 public void toEntities(final Collection<?> results)
407 {
408 if (results != null)
409 {
410 CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
411 }
412 }
413
414
415
416
417
418
419 private Transformer ENTITYTRANSFORMER =
420 new Transformer()
421 {
422 public Object transform(Object input)
423 {
424 Object result = null;
425 if (input instanceof Object[])
426 {
427 result = toEntity((Object[])input);
428 }
429 else if (input instanceof Citation)
430 {
431 result = input;
432 }
433 return result;
434 }
435 };
436
437
438
439
440
441 protected Citation toEntity(Object[] row)
442 {
443 Citation target = null;
444 if (row != null)
445 {
446 final int numberOfObjects = row.length;
447 for (int ctr = 0; ctr < numberOfObjects; ctr++)
448 {
449 final Object object = row[ctr];
450 if (object instanceof Citation)
451 {
452 target = (Citation)object;
453 break;
454 }
455 }
456 }
457 return target;
458 }
459
460
461
462
463
464
465
466 protected Principal getPrincipal()
467 {
468 return PrincipalStore.get();
469 }
470
471
472
473
474 @Override
475 @SuppressWarnings({ "unchecked" })
476 public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
477 {
478 try
479 {
480 search.setPageNumber(pageNumber);
481 search.setPageSize(pageSize);
482 final PropertySearch propertySearch = new PropertySearch(
483 this.getSession(), CitationImpl.class, search);
484 final List results = propertySearch.executeAsList();
485 this.transformEntities(transform, results);
486 return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
487 }
488 catch (HibernateException ex)
489 {
490 throw ex;
491 }
492 }
493
494
495
496
497 @Override
498 public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
499 {
500 return this.search(CitationDao.TRANSFORM_NONE, pageNumber, pageSize, search);
501 }
502
503
504
505
506 @Override
507 public Set<?> search(final int transform, final Search search)
508 {
509 try
510 {
511 final PropertySearch propertySearch = new PropertySearch(
512 this.getSession(), CitationImpl.class, search);
513 final Set<?> results = propertySearch.executeAsSet();
514 this.transformEntities(transform, results);
515 return results;
516 }
517 catch (HibernateException ex)
518 {
519 throw ex;
520 }
521 }
522
523
524
525
526 @Override
527 @SuppressWarnings("unchecked")
528 public Set<Citation> search(final Search search)
529 {
530 return (Set<Citation>) this.search(CitationDao.TRANSFORM_NONE, search);
531 }
532
533
534
535
536
537
538
539
540
541 @SuppressWarnings({ "unchecked" })
542 protected PaginationResult getPaginationResult(
543 final Query queryObject,
544 final int transform, int pageNumber, int pageSize)
545 {
546 try
547 {
548 final ScrollableResults scrollableResults = queryObject.scroll();
549 scrollableResults.last();
550 int totalCount = scrollableResults.getRowNumber();
551 totalCount = totalCount >= 0 ? totalCount + 1 : 0;
552 if (pageNumber > 0 && pageSize > 0)
553 {
554 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
555 queryObject.setMaxResults(pageSize);
556 }
557
558 Set results = new LinkedHashSet(queryObject.list());
559 transformEntities(transform, results);
560 return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
561 }
562 catch (HibernateException ex)
563 {
564 throw ex;
565 }
566 }
567
568
569 }