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