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