1
2
3
4
5
6 package fr.ifremer.quadrige2.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
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.administration.program.Program;
33 import fr.ifremer.quadrige2.core.dao.technical.hibernate.HibernateDaoSupport;
34 import fr.ifremer.quadrige2.core.vo.administration.strategy.StrategyVO;
35 import java.security.Principal;
36 import java.sql.Timestamp;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.Date;
40 import java.util.Iterator;
41 import java.util.LinkedHashSet;
42 import java.util.List;
43 import java.util.Set;
44 import javax.annotation.Resource;
45 import org.andromda.spring.PaginationResult;
46 import org.apache.commons.collections.CollectionUtils;
47 import org.apache.commons.collections.Transformer;
48 import org.hibernate.Criteria;
49 import org.hibernate.HibernateException;
50 import org.hibernate.Query;
51 import org.hibernate.ScrollableResults;
52
53
54
55
56
57
58
59
60
61 public abstract class StrategyDaoBase
62 extends HibernateDaoSupport
63 implements StrategyDao
64 {
65
66
67
68 @Override
69 public Object get(final int transform, final Integer stratId)
70 {
71 if (stratId == null)
72 {
73 throw new IllegalArgumentException(
74 "Strategy.get - 'stratId' can not be null");
75 }
76 final Strategy entity = get(StrategyImpl.class, stratId);
77 return transformEntity(transform, entity);
78 }
79
80
81
82 @Override
83 public Strategy get(Integer stratId)
84 {
85 return (Strategy)this.get(TRANSFORM_NONE, stratId);
86 }
87
88
89
90
91 @Override
92 public Object load(final int transform, final Integer stratId)
93 {
94 if (stratId == null)
95 {
96 throw new IllegalArgumentException(
97 "Strategy.load - 'stratId' can not be null");
98 }
99 final Strategy entity = get(StrategyImpl.class, stratId);
100 return transformEntity(transform, entity);
101 }
102
103
104
105
106 @Override
107 public Strategy load(Integer stratId)
108 {
109 return (Strategy)this.load(TRANSFORM_NONE, stratId);
110 }
111
112
113
114
115 @Override
116 @SuppressWarnings({"unchecked"})
117 public Collection<Strategy> loadAll()
118 {
119 return (Collection<Strategy>) this.loadAll(StrategyDao.TRANSFORM_NONE);
120 }
121
122
123
124
125 @Override
126 public Collection<?> loadAll(final int transform)
127 {
128 return this.loadAll(transform, -1, -1);
129 }
130
131
132
133
134 @Override
135 public Collection<?> loadAll(final int pageNumber, final int pageSize)
136 {
137 return this.loadAll(StrategyDao.TRANSFORM_NONE, pageNumber, pageSize);
138 }
139
140
141
142
143 @Override
144 public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
145 {
146 try
147 {
148 final Criteria criteria = this.getSession().createCriteria(StrategyImpl.class);
149 if (pageNumber > 0 && pageSize > 0)
150 {
151 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
152 criteria.setMaxResults(pageSize);
153 }
154 final Collection<?> results = criteria.list();
155 this.transformEntities(transform, results);
156 return results;
157 }
158 catch (HibernateException ex)
159 {
160 throw ex;
161 }
162 }
163
164
165
166
167
168
169
170 protected int calculateFirstResult(int pageNumber, int pageSize)
171 {
172 int firstResult = 0;
173 if (pageNumber > 0)
174 {
175 firstResult = (pageNumber - 1) * pageSize;
176 }
177 return firstResult;
178 }
179
180
181
182
183 @Override
184 public Strategy create(Strategy strategy)
185 {
186 return (Strategy)this.create(StrategyDao.TRANSFORM_NONE, strategy);
187 }
188
189
190
191
192 @Override
193 public Object create(final int transform, final Strategy strategy)
194 {
195 if (strategy == null)
196 {
197 throw new IllegalArgumentException(
198 "Strategy.create - 'strategy' can not be null");
199 }
200 this.getSessionFactory().getCurrentSession().save(strategy);
201 return this.transformEntity(transform, strategy);
202 }
203
204
205
206
207 @Override
208 @SuppressWarnings({"unchecked"})
209 public Collection<Strategy> create(final Collection<Strategy> entities)
210 {
211 return (Collection<Strategy>) create(StrategyDao.TRANSFORM_NONE, entities);
212 }
213
214
215
216
217 @Override
218 public Collection<?> create(final int transform, final Collection<Strategy> entities)
219 {
220 if (entities == null)
221 {
222 throw new IllegalArgumentException(
223 "Strategy.create - 'entities' can not be null");
224 }
225 for (Strategy entity : entities)
226 {
227 create(transform, entity);
228 }
229 return entities;
230 }
231
232
233
234
235 @Override
236 public Strategy create(
237 String stratNm,
238 String stratDc,
239 Date stratCreationDt,
240 Timestamp updateDt)
241 {
242 return (Strategy)this.create(StrategyDao.TRANSFORM_NONE, stratNm, stratDc, stratCreationDt, updateDt);
243 }
244
245
246
247
248 @Override
249 public Object create(
250 final int transform,
251 String stratNm,
252 String stratDc,
253 Date stratCreationDt,
254 Timestamp updateDt)
255 {
256 Strategy entity = new StrategyImpl();
257 entity.setStratNm(stratNm);
258 entity.setStratDc(stratDc);
259 entity.setStratCreationDt(stratCreationDt);
260 entity.setUpdateDt(updateDt);
261 return this.create(transform, entity);
262 }
263
264
265
266
267 @Override
268 public Strategy create(
269 String stratNm,
270 Timestamp updateDt,
271 Program program)
272 {
273 return (Strategy)this.create(StrategyDao.TRANSFORM_NONE, stratNm, updateDt, program);
274 }
275
276
277
278
279 @Override
280 public Object create(
281 final int transform,
282 String stratNm,
283 Timestamp updateDt,
284 Program program)
285 {
286 Strategy entity = new StrategyImpl();
287 entity.setStratNm(stratNm);
288 entity.setUpdateDt(updateDt);
289 entity.setProgram(program);
290 return this.create(transform, entity);
291 }
292
293
294
295
296 @Override
297 public void update(Strategy strategy)
298 {
299 if (strategy == null)
300 {
301 throw new IllegalArgumentException(
302 "Strategy.update - 'strategy' can not be null");
303 }
304 this.getSessionFactory().getCurrentSession().update(strategy);
305 }
306
307
308
309
310 @Override
311 public void update(final Collection<Strategy> entities)
312 {
313 if (entities == null)
314 {
315 throw new IllegalArgumentException(
316 "Strategy.update - 'entities' can not be null");
317 }
318 for (Strategy entity : entities)
319 {
320 update(entity);
321 }
322 }
323
324
325
326
327 @Override
328 public void remove(Strategy strategy)
329 {
330 if (strategy == null)
331 {
332 throw new IllegalArgumentException(
333 "Strategy.remove - 'strategy' can not be null");
334 }
335 this.getSessionFactory().getCurrentSession().delete(strategy);
336 }
337
338
339
340
341 @Override
342 public void remove(Integer stratId)
343 {
344 if (stratId == null)
345 {
346 throw new IllegalArgumentException(
347 "Strategy.remove - 'stratId' can not be null");
348 }
349 Strategy entity = this.get(stratId);
350 if (entity != null)
351 {
352 this.remove(entity);
353 }
354 }
355
356
357
358
359 @Override
360 public void remove(Collection<Strategy> entities)
361 {
362 if (entities == null)
363 {
364 throw new IllegalArgumentException(
365 "Strategy.remove - 'entities' can not be null");
366 }
367 deleteAll(entities);
368 }
369
370
371
372 @Override
373 public StrategyVO save(final StrategyVO strategy, final Timestamp updateDt)
374 {
375 if (strategy == null)
376 {
377 throw new IllegalArgumentException(
378 "fr.ifremer.quadrige2.core.dao.administration.strategy.StrategyDao.save(StrategyVO strategy, Timestamp updateDt) - 'strategy' can not be null");
379 }
380 try
381 {
382 return this.handleSave(strategy, updateDt);
383 }
384 catch (Throwable th)
385 {
386 throw new RuntimeException(
387 "Error performing 'StrategyDao.save(StrategyVO strategy, Timestamp updateDt)' --> " + th,
388 th);
389 }
390 }
391
392
393
394
395
396
397
398
399 protected abstract StrategyVO handleSave(StrategyVO strategy, Timestamp updateDt)
400 throws Exception;
401
402
403
404
405 @Override
406 public void removeByIds(final Collection<Integer> stratIds)
407 {
408 if (stratIds == null)
409 {
410 throw new IllegalArgumentException(
411 "fr.ifremer.quadrige2.core.dao.administration.strategy.StrategyDao.removeByIds(Collection<Integer> stratIds) - 'stratIds' can not be null");
412 }
413 try
414 {
415 this.handleRemoveByIds(stratIds);
416 }
417 catch (Throwable th)
418 {
419 throw new RuntimeException(
420 "Error performing 'StrategyDao.removeByIds(Collection<Integer> stratIds)' --> " + th,
421 th);
422 }
423 }
424
425
426
427
428
429
430
431 protected abstract void handleRemoveByIds(Collection<Integer> stratIds)
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 Strategy entity)
455 {
456 Object target = null;
457 if (entity != null)
458 {
459 switch (transform)
460 {
461 case TRANSFORM_STRATEGYVO :
462 target = toStrategyVO(entity);
463 break;
464 case StrategyDao.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_STRATEGYVO :
481 toStrategyVOCollection(entities);
482 break;
483 case StrategyDao.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 Strategy)
516 {
517 result = input;
518 }
519 return result;
520 }
521 };
522
523
524
525
526
527 protected Strategy toEntity(Object[] row)
528 {
529 Strategy 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 Strategy)
537 {
538 target = (Strategy)object;
539 break;
540 }
541 }
542 }
543 return target;
544 }
545
546
547
548
549 @Override
550 @SuppressWarnings({"unchecked"})
551 public final Collection<StrategyVO> toStrategyVOCollection(Collection<?> entities)
552 {
553 Collection<StrategyVO> result = new ArrayList<StrategyVO>();
554 if (entities != null)
555 {
556 CollectionUtils.transform(entities, this.STRATEGYVO_TRANSFORMER);
557 result.addAll((Collection<? extends StrategyVO>) entities);
558 }
559 return result;
560 }
561
562
563
564
565 @Override
566 @SuppressWarnings({ "unchecked" })
567 public final StrategyVO[] toStrategyVOArray(Collection<?> entities)
568 {
569 StrategyVO[] result = null;
570 if (entities != null)
571 {
572
573 final Collection collection = new ArrayList(entities);
574 this.toStrategyVOCollection(collection);
575 result = (StrategyVO[]) collection.toArray(new StrategyVO[collection.size()]);
576 }
577 return result;
578 }
579
580
581
582
583
584
585
586
587
588 protected StrategyVO toStrategyVO(Object[] row)
589 {
590 return this.toStrategyVO(this.toEntity(row));
591 }
592
593
594
595
596
597
598 private Transformer STRATEGYVO_TRANSFORMER =
599 new Transformer()
600 {
601 public Object transform(Object input)
602 {
603 Object result = null;
604 if (input instanceof Strategy)
605 {
606 result = toStrategyVO((Strategy)input);
607 }
608 else if (input instanceof Object[])
609 {
610 result = toStrategyVO((Object[])input);
611 }
612 return result;
613 }
614 };
615
616
617
618
619 @Override
620 public final void strategyVOToEntityCollection(Collection<?> instances)
621 {
622 if (instances != null)
623 {
624 for (final Iterator<?> iterator = instances.iterator(); iterator.hasNext();)
625 {
626
627 if (!(iterator.next() instanceof StrategyVO))
628 {
629 iterator.remove();
630 }
631 }
632 CollectionUtils.transform(instances, this.StrategyVOToEntityTransformer);
633 }
634 }
635
636 private final Transformer StrategyVOToEntityTransformer =
637 new Transformer()
638 {
639 public Object transform(Object input)
640 {
641 return strategyVOToEntity((StrategyVO)input);
642 }
643 };
644
645
646
647
648
649 @Override
650 public void toStrategyVO(
651 Strategy source,
652 StrategyVO target)
653 {
654 target.setStratId(source.getStratId());
655 target.setStratNm(source.getStratNm());
656 target.setStratDc(source.getStratDc());
657 target.setStratCreationDt(source.getStratCreationDt());
658 target.setUpdateDt(source.getUpdateDt());
659 }
660
661
662
663
664 @Override
665 public StrategyVO toStrategyVO(final Strategy entity)
666 {
667 StrategyVO target = null;
668 if (entity != null)
669 {
670 target = new StrategyVO();
671 this.toStrategyVO(entity, target);
672 }
673 return target;
674 }
675
676
677
678
679 @Override
680 public void strategyVOToEntity(
681 StrategyVO source,
682 Strategy target,
683 boolean copyIfNull)
684 {
685 if (copyIfNull || source.getStratNm() != null)
686 {
687 target.setStratNm(source.getStratNm());
688 }
689 if (copyIfNull || source.getStratDc() != null)
690 {
691 target.setStratDc(source.getStratDc());
692 }
693 if (copyIfNull || source.getStratCreationDt() != null)
694 {
695 target.setStratCreationDt(source.getStratCreationDt());
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(), StrategyImpl.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(StrategyDao.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(), StrategyImpl.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<Strategy> search(final Search search)
772 {
773 return (Set<Strategy>) this.search(StrategyDao.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 }