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