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