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