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