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.technical.hibernate.HibernateDaoSupport;
33 import fr.ifremer.quadrige2.core.vo.administration.strategy.AppliedPeriodVO;
34 import java.security.Principal;
35 import java.sql.Timestamp;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.Date;
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 AppliedPeriodDaoBase
61 extends HibernateDaoSupport
62 implements AppliedPeriodDao
63 {
64
65
66
67 @Override
68 public Object get(final int transform, final AppliedPeriodPK appliedPeriodPk)
69 {
70 if (appliedPeriodPk == null)
71 {
72 throw new IllegalArgumentException(
73 "AppliedPeriod.get - 'appliedPeriodPk' can not be null");
74 }
75 final AppliedPeriod entity = get(AppliedPeriodImpl.class, appliedPeriodPk);
76 return transformEntity(transform, entity);
77 }
78
79
80
81 @Override
82 public AppliedPeriod get(AppliedPeriodPK appliedPeriodPk)
83 {
84 return (AppliedPeriod)this.get(TRANSFORM_NONE, appliedPeriodPk);
85 }
86
87
88
89
90 @Override
91 public Object load(final int transform, final AppliedPeriodPK appliedPeriodPk)
92 {
93 if (appliedPeriodPk == null)
94 {
95 throw new IllegalArgumentException(
96 "AppliedPeriod.load - 'appliedPeriodPk' can not be null");
97 }
98 final AppliedPeriod entity = get(AppliedPeriodImpl.class, appliedPeriodPk);
99 return transformEntity(transform, entity);
100 }
101
102
103
104
105 @Override
106 public AppliedPeriod load(AppliedPeriodPK appliedPeriodPk)
107 {
108 return (AppliedPeriod)this.load(TRANSFORM_NONE, appliedPeriodPk);
109 }
110
111
112
113
114 @Override
115 @SuppressWarnings({"unchecked"})
116 public Collection<AppliedPeriod> loadAll()
117 {
118 return (Collection<AppliedPeriod>) this.loadAll(AppliedPeriodDao.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(AppliedPeriodDao.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(AppliedPeriodImpl.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 AppliedPeriod create(AppliedPeriod appliedPeriod)
184 {
185 return (AppliedPeriod)this.create(AppliedPeriodDao.TRANSFORM_NONE, appliedPeriod);
186 }
187
188
189
190
191 @Override
192 public Object create(final int transform, final AppliedPeriod appliedPeriod)
193 {
194 if (appliedPeriod == null)
195 {
196 throw new IllegalArgumentException(
197 "AppliedPeriod.create - 'appliedPeriod' can not be null");
198 }
199 this.getSessionFactory().getCurrentSession().save(appliedPeriod);
200 return this.transformEntity(transform, appliedPeriod);
201 }
202
203
204
205
206 @Override
207 @SuppressWarnings({"unchecked"})
208 public Collection<AppliedPeriod> create(final Collection<AppliedPeriod> entities)
209 {
210 return (Collection<AppliedPeriod>) create(AppliedPeriodDao.TRANSFORM_NONE, entities);
211 }
212
213
214
215
216 @Override
217 public Collection<?> create(final int transform, final Collection<AppliedPeriod> entities)
218 {
219 if (entities == null)
220 {
221 throw new IllegalArgumentException(
222 "AppliedPeriod.create - 'entities' can not be null");
223 }
224 for (AppliedPeriod entity : entities)
225 {
226 create(transform, entity);
227 }
228 return entities;
229 }
230
231
232
233
234 @Override
235 public AppliedPeriod create(
236 Date appliedPeriodEndDt,
237 Timestamp updateDt)
238 {
239 return (AppliedPeriod)this.create(AppliedPeriodDao.TRANSFORM_NONE, appliedPeriodEndDt, updateDt);
240 }
241
242
243
244
245 @Override
246 public Object create(
247 final int transform,
248 Date appliedPeriodEndDt,
249 Timestamp updateDt)
250 {
251 AppliedPeriod entity = new AppliedPeriodImpl();
252 entity.setAppliedPeriodEndDt(appliedPeriodEndDt);
253 entity.setUpdateDt(updateDt);
254 return this.create(transform, entity);
255 }
256
257
258
259
260 @Override
261 public AppliedPeriod create(
262 Date appliedPeriodEndDt,
263 Timestamp updateDt,
264 AppliedStrategy appliedStrategy)
265 {
266 return (AppliedPeriod)this.create(AppliedPeriodDao.TRANSFORM_NONE, appliedPeriodEndDt, updateDt, appliedStrategy);
267 }
268
269
270
271
272 @Override
273 public Object create(
274 final int transform,
275 Date appliedPeriodEndDt,
276 Timestamp updateDt,
277 AppliedStrategy appliedStrategy)
278 {
279 AppliedPeriod entity = new AppliedPeriodImpl();
280 entity.setAppliedPeriodEndDt(appliedPeriodEndDt);
281 entity.setUpdateDt(updateDt);
282 entity.setAppliedStrategy(appliedStrategy);
283 return this.create(transform, entity);
284 }
285
286
287
288
289 @Override
290 public void update(AppliedPeriod appliedPeriod)
291 {
292 if (appliedPeriod == null)
293 {
294 throw new IllegalArgumentException(
295 "AppliedPeriod.update - 'appliedPeriod' can not be null");
296 }
297 this.getSessionFactory().getCurrentSession().update(appliedPeriod);
298 }
299
300
301
302
303 @Override
304 public void update(final Collection<AppliedPeriod> entities)
305 {
306 if (entities == null)
307 {
308 throw new IllegalArgumentException(
309 "AppliedPeriod.update - 'entities' can not be null");
310 }
311 for (AppliedPeriod entity : entities)
312 {
313 update(entity);
314 }
315 }
316
317
318
319
320 @Override
321 public void remove(AppliedPeriod appliedPeriod)
322 {
323 if (appliedPeriod == null)
324 {
325 throw new IllegalArgumentException(
326 "AppliedPeriod.remove - 'appliedPeriod' can not be null");
327 }
328 this.getSessionFactory().getCurrentSession().delete(appliedPeriod);
329 }
330
331
332
333
334 @Override
335 public void remove(AppliedPeriodPK appliedPeriodPk)
336 {
337 if (appliedPeriodPk == null)
338 {
339 throw new IllegalArgumentException(
340 "AppliedPeriod.remove - 'appliedPeriodPk' can not be null");
341 }
342 AppliedPeriod entity = this.get(appliedPeriodPk);
343 if (entity != null)
344 {
345 this.remove(entity);
346 }
347 }
348
349
350
351
352 @Override
353 public void remove(Collection<AppliedPeriod> entities)
354 {
355 if (entities == null)
356 {
357 throw new IllegalArgumentException(
358 "AppliedPeriod.remove - 'entities' can not be null");
359 }
360 deleteAll(entities);
361 }
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382 public Object transformEntity(final int transform, final AppliedPeriod entity)
383 {
384 Object target = null;
385 if (entity != null)
386 {
387 switch (transform)
388 {
389 case TRANSFORM_APPLIEDPERIODVO :
390 target = toAppliedPeriodVO(entity);
391 break;
392 case AppliedPeriodDao.TRANSFORM_NONE :
393 default:
394 target = entity;
395 }
396 }
397 return target;
398 }
399
400
401
402
403 @Override
404 public void transformEntities(final int transform, final Collection<?> entities)
405 {
406 switch (transform)
407 {
408 case TRANSFORM_APPLIEDPERIODVO :
409 toAppliedPeriodVOCollection(entities);
410 break;
411 case AppliedPeriodDao.TRANSFORM_NONE :
412 default:
413
414 }
415 }
416
417
418
419
420 public void toEntities(final Collection<?> results)
421 {
422 if (results != null)
423 {
424 CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
425 }
426 }
427
428
429
430
431
432
433 private Transformer ENTITYTRANSFORMER =
434 new Transformer()
435 {
436 public Object transform(Object input)
437 {
438 Object result = null;
439 if (input instanceof Object[])
440 {
441 result = toEntity((Object[])input);
442 }
443 else if (input instanceof AppliedPeriod)
444 {
445 result = input;
446 }
447 return result;
448 }
449 };
450
451
452
453
454
455 protected AppliedPeriod toEntity(Object[] row)
456 {
457 AppliedPeriod target = null;
458 if (row != null)
459 {
460 final int numberOfObjects = row.length;
461 for (int ctr = 0; ctr < numberOfObjects; ctr++)
462 {
463 final Object object = row[ctr];
464 if (object instanceof AppliedPeriod)
465 {
466 target = (AppliedPeriod)object;
467 break;
468 }
469 }
470 }
471 return target;
472 }
473
474
475
476
477 @Override
478 @SuppressWarnings({"unchecked"})
479 public final Collection<AppliedPeriodVO> toAppliedPeriodVOCollection(Collection<?> entities)
480 {
481 Collection<AppliedPeriodVO> result = new ArrayList<AppliedPeriodVO>();
482 if (entities != null)
483 {
484 CollectionUtils.transform(entities, this.APPLIEDPERIODVO_TRANSFORMER);
485 result.addAll((Collection<? extends AppliedPeriodVO>) entities);
486 }
487 return result;
488 }
489
490
491
492
493 @Override
494 @SuppressWarnings({ "unchecked" })
495 public final AppliedPeriodVO[] toAppliedPeriodVOArray(Collection<?> entities)
496 {
497 AppliedPeriodVO[] result = null;
498 if (entities != null)
499 {
500
501 final Collection collection = new ArrayList(entities);
502 this.toAppliedPeriodVOCollection(collection);
503 result = (AppliedPeriodVO[]) collection.toArray(new AppliedPeriodVO[collection.size()]);
504 }
505 return result;
506 }
507
508
509
510
511
512
513
514
515
516 protected AppliedPeriodVO toAppliedPeriodVO(Object[] row)
517 {
518 return this.toAppliedPeriodVO(this.toEntity(row));
519 }
520
521
522
523
524
525
526 private Transformer APPLIEDPERIODVO_TRANSFORMER =
527 new Transformer()
528 {
529 public Object transform(Object input)
530 {
531 Object result = null;
532 if (input instanceof AppliedPeriod)
533 {
534 result = toAppliedPeriodVO((AppliedPeriod)input);
535 }
536 else if (input instanceof Object[])
537 {
538 result = toAppliedPeriodVO((Object[])input);
539 }
540 return result;
541 }
542 };
543
544
545
546
547 @Override
548 public final void appliedPeriodVOToEntityCollection(Collection<?> instances)
549 {
550 if (instances != null)
551 {
552 for (final Iterator<?> iterator = instances.iterator(); iterator.hasNext();)
553 {
554
555 if (!(iterator.next() instanceof AppliedPeriodVO))
556 {
557 iterator.remove();
558 }
559 }
560 CollectionUtils.transform(instances, this.AppliedPeriodVOToEntityTransformer);
561 }
562 }
563
564 private final Transformer AppliedPeriodVOToEntityTransformer =
565 new Transformer()
566 {
567 public Object transform(Object input)
568 {
569 return appliedPeriodVOToEntity((AppliedPeriodVO)input);
570 }
571 };
572
573
574
575
576
577 @Override
578 public void toAppliedPeriodVO(
579 AppliedPeriod source,
580 AppliedPeriodVO target)
581 {
582 target.setAppliedPeriodEndDt(source.getAppliedPeriodEndDt());
583 target.setUpdateDt(source.getUpdateDt());
584 }
585
586
587
588
589 @Override
590 public AppliedPeriodVO toAppliedPeriodVO(final AppliedPeriod entity)
591 {
592 AppliedPeriodVO target = null;
593 if (entity != null)
594 {
595 target = new AppliedPeriodVO();
596 this.toAppliedPeriodVO(entity, target);
597 }
598 return target;
599 }
600
601
602
603
604 @Override
605 public void appliedPeriodVOToEntity(
606 AppliedPeriodVO source,
607 AppliedPeriod target,
608 boolean copyIfNull)
609 {
610 if (copyIfNull || source.getAppliedPeriodEndDt() != null)
611 {
612 target.setAppliedPeriodEndDt(source.getAppliedPeriodEndDt());
613 }
614 if (copyIfNull || source.getUpdateDt() != null)
615 {
616 target.setUpdateDt(source.getUpdateDt());
617 }
618 }
619
620
621
622
623
624
625
626 protected Principal getPrincipal()
627 {
628 return PrincipalStore.get();
629 }
630
631
632
633
634 @Override
635 @SuppressWarnings({ "unchecked" })
636 public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
637 {
638 try
639 {
640 search.setPageNumber(pageNumber);
641 search.setPageSize(pageSize);
642 final PropertySearch propertySearch = new PropertySearch(
643 this.getSession(), AppliedPeriodImpl.class, search);
644 final List results = propertySearch.executeAsList();
645 this.transformEntities(transform, results);
646 return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
647 }
648 catch (HibernateException ex)
649 {
650 throw ex;
651 }
652 }
653
654
655
656
657 @Override
658 public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
659 {
660 return this.search(AppliedPeriodDao.TRANSFORM_NONE, pageNumber, pageSize, search);
661 }
662
663
664
665
666 @Override
667 public Set<?> search(final int transform, final Search search)
668 {
669 try
670 {
671 final PropertySearch propertySearch = new PropertySearch(
672 this.getSession(), AppliedPeriodImpl.class, search);
673 final Set<?> results = propertySearch.executeAsSet();
674 this.transformEntities(transform, results);
675 return results;
676 }
677 catch (HibernateException ex)
678 {
679 throw ex;
680 }
681 }
682
683
684
685
686 @Override
687 @SuppressWarnings("unchecked")
688 public Set<AppliedPeriod> search(final Search search)
689 {
690 return (Set<AppliedPeriod>) this.search(AppliedPeriodDao.TRANSFORM_NONE, search);
691 }
692
693
694
695
696
697
698
699
700
701 @SuppressWarnings({ "unchecked" })
702 protected PaginationResult getPaginationResult(
703 final Query queryObject,
704 final int transform, int pageNumber, int pageSize)
705 {
706 try
707 {
708 final ScrollableResults scrollableResults = queryObject.scroll();
709 scrollableResults.last();
710 int totalCount = scrollableResults.getRowNumber();
711 totalCount = totalCount >= 0 ? totalCount + 1 : 0;
712 if (pageNumber > 0 && pageSize > 0)
713 {
714 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
715 queryObject.setMaxResults(pageSize);
716 }
717
718 Set results = new LinkedHashSet(queryObject.list());
719 transformEntities(transform, results);
720 return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
721 }
722 catch (HibernateException ex)
723 {
724 throw ex;
725 }
726 }
727
728
729 }