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