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