1
2
3
4
5
6 package fr.ifremer.quadrige2.core.dao.administration.strategy;
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.pmfm.Pmfm;
33 import fr.ifremer.quadrige2.core.dao.technical.hibernate.HibernateDaoSupport;
34 import fr.ifremer.quadrige2.core.vo.administration.strategy.PmfmStrategyVO;
35 import java.security.Principal;
36 import java.sql.Timestamp;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.Iterator;
40 import java.util.LinkedHashSet;
41 import java.util.List;
42 import java.util.Set;
43 import javax.annotation.Resource;
44 import org.andromda.spring.PaginationResult;
45 import org.apache.commons.collections.CollectionUtils;
46 import org.apache.commons.collections.Transformer;
47 import org.hibernate.Criteria;
48 import org.hibernate.HibernateException;
49 import org.hibernate.Query;
50 import org.hibernate.ScrollableResults;
51
52
53
54
55
56
57
58
59
60 public abstract class PmfmStrategyDaoBase
61 extends HibernateDaoSupport
62 implements PmfmStrategyDao
63 {
64
65
66
67 @Override
68 public Object get(final int transform, final Integer pmfmStratId)
69 {
70 if (pmfmStratId == null)
71 {
72 throw new IllegalArgumentException(
73 "PmfmStrategy.get - 'pmfmStratId' can not be null");
74 }
75 final PmfmStrategy entity = get(PmfmStrategyImpl.class, pmfmStratId);
76 return transformEntity(transform, entity);
77 }
78
79
80
81 @Override
82 public PmfmStrategy get(Integer pmfmStratId)
83 {
84 return (PmfmStrategy)this.get(TRANSFORM_NONE, pmfmStratId);
85 }
86
87
88
89
90 @Override
91 public Object load(final int transform, final Integer pmfmStratId)
92 {
93 if (pmfmStratId == null)
94 {
95 throw new IllegalArgumentException(
96 "PmfmStrategy.load - 'pmfmStratId' can not be null");
97 }
98 final PmfmStrategy entity = get(PmfmStrategyImpl.class, pmfmStratId);
99 return transformEntity(transform, entity);
100 }
101
102
103
104
105 @Override
106 public PmfmStrategy load(Integer pmfmStratId)
107 {
108 return (PmfmStrategy)this.load(TRANSFORM_NONE, pmfmStratId);
109 }
110
111
112
113
114 @Override
115 @SuppressWarnings({"unchecked"})
116 public Collection<PmfmStrategy> loadAll()
117 {
118 return (Collection<PmfmStrategy>) this.loadAll(PmfmStrategyDao.TRANSFORM_NONE);
119 }
120
121
122
123
124 @Override
125 public Collection<?> loadAll(final int transform)
126 {
127 return this.loadAll(transform, -1, -1);
128 }
129
130
131
132
133 @Override
134 public Collection<?> loadAll(final int pageNumber, final int pageSize)
135 {
136 return this.loadAll(PmfmStrategyDao.TRANSFORM_NONE, pageNumber, pageSize);
137 }
138
139
140
141
142 @Override
143 public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
144 {
145 try
146 {
147 final Criteria criteria = this.getSession().createCriteria(PmfmStrategyImpl.class);
148 if (pageNumber > 0 && pageSize > 0)
149 {
150 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
151 criteria.setMaxResults(pageSize);
152 }
153 final Collection<?> results = criteria.list();
154 this.transformEntities(transform, results);
155 return results;
156 }
157 catch (HibernateException ex)
158 {
159 throw ex;
160 }
161 }
162
163
164
165
166
167
168
169 protected int calculateFirstResult(int pageNumber, int pageSize)
170 {
171 int firstResult = 0;
172 if (pageNumber > 0)
173 {
174 firstResult = (pageNumber - 1) * pageSize;
175 }
176 return firstResult;
177 }
178
179
180
181
182 @Override
183 public PmfmStrategy create(PmfmStrategy pmfmStrategy)
184 {
185 return (PmfmStrategy)this.create(PmfmStrategyDao.TRANSFORM_NONE, pmfmStrategy);
186 }
187
188
189
190
191 @Override
192 public Object create(final int transform, final PmfmStrategy pmfmStrategy)
193 {
194 if (pmfmStrategy == null)
195 {
196 throw new IllegalArgumentException(
197 "PmfmStrategy.create - 'pmfmStrategy' can not be null");
198 }
199 this.getSessionFactory().getCurrentSession().save(pmfmStrategy);
200 return this.transformEntity(transform, pmfmStrategy);
201 }
202
203
204
205
206 @Override
207 @SuppressWarnings({"unchecked"})
208 public Collection<PmfmStrategy> create(final Collection<PmfmStrategy> entities)
209 {
210 return (Collection<PmfmStrategy>) create(PmfmStrategyDao.TRANSFORM_NONE, entities);
211 }
212
213
214
215
216 @Override
217 public Collection<?> create(final int transform, final Collection<PmfmStrategy> entities)
218 {
219 if (entities == null)
220 {
221 throw new IllegalArgumentException(
222 "PmfmStrategy.create - 'entities' can not be null");
223 }
224 for (PmfmStrategy entity : entities)
225 {
226 create(transform, entity);
227 }
228 return entities;
229 }
230
231
232
233
234 @Override
235 public PmfmStrategy create(
236 Double pmfmStratParAcquisNumber,
237 Integer pmfmStratPresRk,
238 String pmfmStratParIsIndiv,
239 String pmfmStratIsUniqueByTaxon,
240 Timestamp updateDt)
241 {
242 return (PmfmStrategy)this.create(PmfmStrategyDao.TRANSFORM_NONE, pmfmStratParAcquisNumber, pmfmStratPresRk, pmfmStratParIsIndiv, pmfmStratIsUniqueByTaxon, updateDt);
243 }
244
245
246
247
248 @Override
249 public Object create(
250 final int transform,
251 Double pmfmStratParAcquisNumber,
252 Integer pmfmStratPresRk,
253 String pmfmStratParIsIndiv,
254 String pmfmStratIsUniqueByTaxon,
255 Timestamp updateDt)
256 {
257 PmfmStrategy entity = new PmfmStrategyImpl();
258 entity.setPmfmStratParAcquisNumber(pmfmStratParAcquisNumber);
259 entity.setPmfmStratPresRk(pmfmStratPresRk);
260 entity.setPmfmStratParIsIndiv(pmfmStratParIsIndiv);
261 entity.setPmfmStratIsUniqueByTaxon(pmfmStratIsUniqueByTaxon);
262 entity.setUpdateDt(updateDt);
263 return this.create(transform, entity);
264 }
265
266
267
268
269 @Override
270 public PmfmStrategy create(
271 Double pmfmStratParAcquisNumber,
272 Timestamp updateDt,
273 Pmfm pmfm,
274 Strategy strategy)
275 {
276 return (PmfmStrategy)this.create(PmfmStrategyDao.TRANSFORM_NONE, pmfmStratParAcquisNumber, updateDt, pmfm, strategy);
277 }
278
279
280
281
282 @Override
283 public Object create(
284 final int transform,
285 Double pmfmStratParAcquisNumber,
286 Timestamp updateDt,
287 Pmfm pmfm,
288 Strategy strategy)
289 {
290 PmfmStrategy entity = new PmfmStrategyImpl();
291 entity.setPmfmStratParAcquisNumber(pmfmStratParAcquisNumber);
292 entity.setUpdateDt(updateDt);
293 entity.setPmfm(pmfm);
294 entity.setStrategy(strategy);
295 return this.create(transform, entity);
296 }
297
298
299
300
301 @Override
302 public void update(PmfmStrategy pmfmStrategy)
303 {
304 if (pmfmStrategy == null)
305 {
306 throw new IllegalArgumentException(
307 "PmfmStrategy.update - 'pmfmStrategy' can not be null");
308 }
309 this.getSessionFactory().getCurrentSession().update(pmfmStrategy);
310 }
311
312
313
314
315 @Override
316 public void update(final Collection<PmfmStrategy> entities)
317 {
318 if (entities == null)
319 {
320 throw new IllegalArgumentException(
321 "PmfmStrategy.update - 'entities' can not be null");
322 }
323 for (PmfmStrategy entity : entities)
324 {
325 update(entity);
326 }
327 }
328
329
330
331
332 @Override
333 public void remove(PmfmStrategy pmfmStrategy)
334 {
335 if (pmfmStrategy == null)
336 {
337 throw new IllegalArgumentException(
338 "PmfmStrategy.remove - 'pmfmStrategy' can not be null");
339 }
340 this.getSessionFactory().getCurrentSession().delete(pmfmStrategy);
341 }
342
343
344
345
346 @Override
347 public void remove(Integer pmfmStratId)
348 {
349 if (pmfmStratId == null)
350 {
351 throw new IllegalArgumentException(
352 "PmfmStrategy.remove - 'pmfmStratId' can not be null");
353 }
354 PmfmStrategy entity = this.get(pmfmStratId);
355 if (entity != null)
356 {
357 this.remove(entity);
358 }
359 }
360
361
362
363
364 @Override
365 public void remove(Collection<PmfmStrategy> entities)
366 {
367 if (entities == null)
368 {
369 throw new IllegalArgumentException(
370 "PmfmStrategy.remove - 'entities' can not be null");
371 }
372 deleteAll(entities);
373 }
374
375
376
377 @Override
378 public PmfmStrategyVO save(final PmfmStrategyVO pmfmStrategy, final Timestamp updateDt)
379 {
380 if (pmfmStrategy == null)
381 {
382 throw new IllegalArgumentException(
383 "fr.ifremer.quadrige2.core.dao.administration.strategy.PmfmStrategyDao.save(PmfmStrategyVO pmfmStrategy, Timestamp updateDt) - 'pmfmStrategy' can not be null");
384 }
385 try
386 {
387 return this.handleSave(pmfmStrategy, updateDt);
388 }
389 catch (Throwable th)
390 {
391 throw new RuntimeException(
392 "Error performing 'PmfmStrategyDao.save(PmfmStrategyVO pmfmStrategy, Timestamp updateDt)' --> " + th,
393 th);
394 }
395 }
396
397
398
399
400
401
402
403
404 protected abstract PmfmStrategyVO handleSave(PmfmStrategyVO pmfmStrategy, Timestamp updateDt)
405 throws Exception;
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427 public Object transformEntity(final int transform, final PmfmStrategy entity)
428 {
429 Object target = null;
430 if (entity != null)
431 {
432 switch (transform)
433 {
434 case TRANSFORM_PMFMSTRATEGYVO :
435 target = toPmfmStrategyVO(entity);
436 break;
437 case PmfmStrategyDao.TRANSFORM_NONE :
438 default:
439 target = entity;
440 }
441 }
442 return target;
443 }
444
445
446
447
448 @Override
449 public void transformEntities(final int transform, final Collection<?> entities)
450 {
451 switch (transform)
452 {
453 case TRANSFORM_PMFMSTRATEGYVO :
454 toPmfmStrategyVOCollection(entities);
455 break;
456 case PmfmStrategyDao.TRANSFORM_NONE :
457 default:
458
459 }
460 }
461
462
463
464
465 public void toEntities(final Collection<?> results)
466 {
467 if (results != null)
468 {
469 CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
470 }
471 }
472
473
474
475
476
477
478 private Transformer ENTITYTRANSFORMER =
479 new Transformer()
480 {
481 public Object transform(Object input)
482 {
483 Object result = null;
484 if (input instanceof Object[])
485 {
486 result = toEntity((Object[])input);
487 }
488 else if (input instanceof PmfmStrategy)
489 {
490 result = input;
491 }
492 return result;
493 }
494 };
495
496
497
498
499
500 protected PmfmStrategy toEntity(Object[] row)
501 {
502 PmfmStrategy target = null;
503 if (row != null)
504 {
505 final int numberOfObjects = row.length;
506 for (int ctr = 0; ctr < numberOfObjects; ctr++)
507 {
508 final Object object = row[ctr];
509 if (object instanceof PmfmStrategy)
510 {
511 target = (PmfmStrategy)object;
512 break;
513 }
514 }
515 }
516 return target;
517 }
518
519
520
521
522 @Override
523 @SuppressWarnings({"unchecked"})
524 public final Collection<PmfmStrategyVO> toPmfmStrategyVOCollection(Collection<?> entities)
525 {
526 Collection<PmfmStrategyVO> result = new ArrayList<PmfmStrategyVO>();
527 if (entities != null)
528 {
529 CollectionUtils.transform(entities, this.PMFMSTRATEGYVO_TRANSFORMER);
530 result.addAll((Collection<? extends PmfmStrategyVO>) entities);
531 }
532 return result;
533 }
534
535
536
537
538 @Override
539 @SuppressWarnings({ "unchecked" })
540 public final PmfmStrategyVO[] toPmfmStrategyVOArray(Collection<?> entities)
541 {
542 PmfmStrategyVO[] result = null;
543 if (entities != null)
544 {
545
546 final Collection collection = new ArrayList(entities);
547 this.toPmfmStrategyVOCollection(collection);
548 result = (PmfmStrategyVO[]) collection.toArray(new PmfmStrategyVO[collection.size()]);
549 }
550 return result;
551 }
552
553
554
555
556
557
558
559
560
561 protected PmfmStrategyVO toPmfmStrategyVO(Object[] row)
562 {
563 return this.toPmfmStrategyVO(this.toEntity(row));
564 }
565
566
567
568
569
570
571 private Transformer PMFMSTRATEGYVO_TRANSFORMER =
572 new Transformer()
573 {
574 public Object transform(Object input)
575 {
576 Object result = null;
577 if (input instanceof PmfmStrategy)
578 {
579 result = toPmfmStrategyVO((PmfmStrategy)input);
580 }
581 else if (input instanceof Object[])
582 {
583 result = toPmfmStrategyVO((Object[])input);
584 }
585 return result;
586 }
587 };
588
589
590
591
592 @Override
593 public final void pmfmStrategyVOToEntityCollection(Collection<?> instances)
594 {
595 if (instances != null)
596 {
597 for (final Iterator<?> iterator = instances.iterator(); iterator.hasNext();)
598 {
599
600 if (!(iterator.next() instanceof PmfmStrategyVO))
601 {
602 iterator.remove();
603 }
604 }
605 CollectionUtils.transform(instances, this.PmfmStrategyVOToEntityTransformer);
606 }
607 }
608
609 private final Transformer PmfmStrategyVOToEntityTransformer =
610 new Transformer()
611 {
612 public Object transform(Object input)
613 {
614 return pmfmStrategyVOToEntity((PmfmStrategyVO)input);
615 }
616 };
617
618
619
620
621
622 @Override
623 public void toPmfmStrategyVO(
624 PmfmStrategy source,
625 PmfmStrategyVO target)
626 {
627 target.setPmfmStratId(source.getPmfmStratId());
628 target.setPmfmStratParAcquisNumber(source.getPmfmStratParAcquisNumber());
629 target.setPmfmStratPresRk(source.getPmfmStratPresRk());
630 target.setPmfmStratParIsIndiv(source.getPmfmStratParIsIndiv());
631 target.setPmfmStratIsUniqueByTaxon(source.getPmfmStratIsUniqueByTaxon());
632 target.setUpdateDt(source.getUpdateDt());
633 }
634
635
636
637
638 @Override
639 public PmfmStrategyVO toPmfmStrategyVO(final PmfmStrategy entity)
640 {
641 PmfmStrategyVO target = null;
642 if (entity != null)
643 {
644 target = new PmfmStrategyVO();
645 this.toPmfmStrategyVO(entity, target);
646 }
647 return target;
648 }
649
650
651
652
653 @Override
654 public void pmfmStrategyVOToEntity(
655 PmfmStrategyVO source,
656 PmfmStrategy target,
657 boolean copyIfNull)
658 {
659 if (copyIfNull || source.getPmfmStratParAcquisNumber() != null)
660 {
661 target.setPmfmStratParAcquisNumber(source.getPmfmStratParAcquisNumber());
662 }
663 if (copyIfNull || source.getPmfmStratPresRk() != null)
664 {
665 target.setPmfmStratPresRk(source.getPmfmStratPresRk());
666 }
667 if (copyIfNull || source.getPmfmStratParIsIndiv() != null)
668 {
669 target.setPmfmStratParIsIndiv(source.getPmfmStratParIsIndiv());
670 }
671 if (copyIfNull || source.getPmfmStratIsUniqueByTaxon() != null)
672 {
673 target.setPmfmStratIsUniqueByTaxon(source.getPmfmStratIsUniqueByTaxon());
674 }
675 if (copyIfNull || source.getUpdateDt() != null)
676 {
677 target.setUpdateDt(source.getUpdateDt());
678 }
679 }
680
681
682
683
684
685
686
687 protected Principal getPrincipal()
688 {
689 return PrincipalStore.get();
690 }
691
692
693
694
695 @Override
696 @SuppressWarnings({ "unchecked" })
697 public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
698 {
699 try
700 {
701 search.setPageNumber(pageNumber);
702 search.setPageSize(pageSize);
703 final PropertySearch propertySearch = new PropertySearch(
704 this.getSession(), PmfmStrategyImpl.class, search);
705 final List results = propertySearch.executeAsList();
706 this.transformEntities(transform, results);
707 return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
708 }
709 catch (HibernateException ex)
710 {
711 throw ex;
712 }
713 }
714
715
716
717
718 @Override
719 public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
720 {
721 return this.search(PmfmStrategyDao.TRANSFORM_NONE, pageNumber, pageSize, search);
722 }
723
724
725
726
727 @Override
728 public Set<?> search(final int transform, final Search search)
729 {
730 try
731 {
732 final PropertySearch propertySearch = new PropertySearch(
733 this.getSession(), PmfmStrategyImpl.class, search);
734 final Set<?> results = propertySearch.executeAsSet();
735 this.transformEntities(transform, results);
736 return results;
737 }
738 catch (HibernateException ex)
739 {
740 throw ex;
741 }
742 }
743
744
745
746
747 @Override
748 @SuppressWarnings("unchecked")
749 public Set<PmfmStrategy> search(final Search search)
750 {
751 return (Set<PmfmStrategy>) this.search(PmfmStrategyDao.TRANSFORM_NONE, search);
752 }
753
754
755
756
757
758
759
760
761
762 @SuppressWarnings({ "unchecked" })
763 protected PaginationResult getPaginationResult(
764 final Query queryObject,
765 final int transform, int pageNumber, int pageSize)
766 {
767 try
768 {
769 final ScrollableResults scrollableResults = queryObject.scroll();
770 scrollableResults.last();
771 int totalCount = scrollableResults.getRowNumber();
772 totalCount = totalCount >= 0 ? totalCount + 1 : 0;
773 if (pageNumber > 0 && pageSize > 0)
774 {
775 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
776 queryObject.setMaxResults(pageSize);
777 }
778
779 Set results = new LinkedHashSet(queryObject.list());
780 transformEntities(transform, results);
781 return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
782 }
783 catch (HibernateException ex)
784 {
785 throw ex;
786 }
787 }
788
789
790 }