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