1
2
3
4
5
6 package fr.ifremer.quadrige2.core.dao.data.survey;
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.user.Department;
33 import fr.ifremer.quadrige2.core.dao.administration.user.Quser;
34 import fr.ifremer.quadrige2.core.dao.technical.hibernate.HibernateDaoSupport;
35 import fr.ifremer.quadrige2.core.vo.data.survey.CampaignVO;
36 import java.security.Principal;
37 import java.sql.Timestamp;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Date;
41 import java.util.Iterator;
42 import java.util.LinkedHashSet;
43 import java.util.List;
44 import java.util.Set;
45 import javax.annotation.Resource;
46 import org.andromda.spring.PaginationResult;
47 import org.apache.commons.collections.CollectionUtils;
48 import org.apache.commons.collections.Transformer;
49 import org.hibernate.Criteria;
50 import org.hibernate.HibernateException;
51 import org.hibernate.Query;
52 import org.hibernate.ScrollableResults;
53
54
55
56
57
58
59
60
61
62 public abstract class CampaignDaoBase
63 extends HibernateDaoSupport
64 implements CampaignDao
65 {
66
67
68
69 @Override
70 public Object get(final int transform, final Integer campaignId)
71 {
72 if (campaignId == null)
73 {
74 throw new IllegalArgumentException(
75 "Campaign.get - 'campaignId' can not be null");
76 }
77 final Campaign entity = get(CampaignImpl.class, campaignId);
78 return transformEntity(transform, entity);
79 }
80
81
82
83 @Override
84 public Campaign get(Integer campaignId)
85 {
86 return (Campaign)this.get(TRANSFORM_NONE, campaignId);
87 }
88
89
90
91
92 @Override
93 public Object load(final int transform, final Integer campaignId)
94 {
95 if (campaignId == null)
96 {
97 throw new IllegalArgumentException(
98 "Campaign.load - 'campaignId' can not be null");
99 }
100 final Campaign entity = get(CampaignImpl.class, campaignId);
101 return transformEntity(transform, entity);
102 }
103
104
105
106
107 @Override
108 public Campaign load(Integer campaignId)
109 {
110 return (Campaign)this.load(TRANSFORM_NONE, campaignId);
111 }
112
113
114
115
116 @Override
117 @SuppressWarnings({"unchecked"})
118 public Collection<Campaign> loadAll()
119 {
120 return (Collection<Campaign>) this.loadAll(CampaignDao.TRANSFORM_NONE);
121 }
122
123
124
125
126 @Override
127 public Collection<?> loadAll(final int transform)
128 {
129 return this.loadAll(transform, -1, -1);
130 }
131
132
133
134
135 @Override
136 public Collection<?> loadAll(final int pageNumber, final int pageSize)
137 {
138 return this.loadAll(CampaignDao.TRANSFORM_NONE, pageNumber, pageSize);
139 }
140
141
142
143
144 @Override
145 public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
146 {
147 try
148 {
149 final Criteria criteria = this.getSession().createCriteria(CampaignImpl.class);
150 if (pageNumber > 0 && pageSize > 0)
151 {
152 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
153 criteria.setMaxResults(pageSize);
154 }
155 final Collection<?> results = criteria.list();
156 this.transformEntities(transform, results);
157 return results;
158 }
159 catch (HibernateException ex)
160 {
161 throw ex;
162 }
163 }
164
165
166
167
168
169
170
171 protected int calculateFirstResult(int pageNumber, int pageSize)
172 {
173 int firstResult = 0;
174 if (pageNumber > 0)
175 {
176 firstResult = (pageNumber - 1) * pageSize;
177 }
178 return firstResult;
179 }
180
181
182
183
184 @Override
185 public Campaign create(Campaign campaign)
186 {
187 return (Campaign)this.create(CampaignDao.TRANSFORM_NONE, campaign);
188 }
189
190
191
192
193 @Override
194 public Object create(final int transform, final Campaign campaign)
195 {
196 if (campaign == null)
197 {
198 throw new IllegalArgumentException(
199 "Campaign.create - 'campaign' can not be null");
200 }
201 this.getSessionFactory().getCurrentSession().save(campaign);
202 return this.transformEntity(transform, campaign);
203 }
204
205
206
207
208 @Override
209 @SuppressWarnings({"unchecked"})
210 public Collection<Campaign> create(final Collection<Campaign> entities)
211 {
212 return (Collection<Campaign>) create(CampaignDao.TRANSFORM_NONE, entities);
213 }
214
215
216
217
218 @Override
219 public Collection<?> create(final int transform, final Collection<Campaign> entities)
220 {
221 if (entities == null)
222 {
223 throw new IllegalArgumentException(
224 "Campaign.create - 'entities' can not be null");
225 }
226 for (Campaign entity : entities)
227 {
228 create(transform, entity);
229 }
230 return entities;
231 }
232
233
234
235
236 @Override
237 public Campaign create(
238 String campaignSismerLk,
239 Date campaignStartDt,
240 Date campaignEndDt,
241 String campaignNm,
242 String campaignCm,
243 String campaignPositionCm,
244 Timestamp updateDt)
245 {
246 return (Campaign)this.create(CampaignDao.TRANSFORM_NONE, campaignSismerLk, campaignStartDt, campaignEndDt, campaignNm, campaignCm, campaignPositionCm, updateDt);
247 }
248
249
250
251
252 @Override
253 public Object create(
254 final int transform,
255 String campaignSismerLk,
256 Date campaignStartDt,
257 Date campaignEndDt,
258 String campaignNm,
259 String campaignCm,
260 String campaignPositionCm,
261 Timestamp updateDt)
262 {
263 Campaign entity = new CampaignImpl();
264 entity.setCampaignSismerLk(campaignSismerLk);
265 entity.setCampaignStartDt(campaignStartDt);
266 entity.setCampaignEndDt(campaignEndDt);
267 entity.setCampaignNm(campaignNm);
268 entity.setCampaignCm(campaignCm);
269 entity.setCampaignPositionCm(campaignPositionCm);
270 entity.setUpdateDt(updateDt);
271 return this.create(transform, entity);
272 }
273
274
275
276
277 @Override
278 public Campaign create(
279 String campaignNm,
280 Date campaignStartDt,
281 Timestamp updateDt,
282 Department recorderDepartment,
283 Quser quser)
284 {
285 return (Campaign)this.create(CampaignDao.TRANSFORM_NONE, campaignNm, campaignStartDt, updateDt, recorderDepartment, quser);
286 }
287
288
289
290
291 @Override
292 public Object create(
293 final int transform,
294 String campaignNm,
295 Date campaignStartDt,
296 Timestamp updateDt,
297 Department recorderDepartment,
298 Quser quser)
299 {
300 Campaign entity = new CampaignImpl();
301 entity.setCampaignNm(campaignNm);
302 entity.setCampaignStartDt(campaignStartDt);
303 entity.setUpdateDt(updateDt);
304 entity.setRecorderDepartment(recorderDepartment);
305 entity.setQuser(quser);
306 return this.create(transform, entity);
307 }
308
309
310
311
312 @Override
313 public void update(Campaign campaign)
314 {
315 if (campaign == null)
316 {
317 throw new IllegalArgumentException(
318 "Campaign.update - 'campaign' can not be null");
319 }
320 this.getSessionFactory().getCurrentSession().update(campaign);
321 }
322
323
324
325
326 @Override
327 public void update(final Collection<Campaign> entities)
328 {
329 if (entities == null)
330 {
331 throw new IllegalArgumentException(
332 "Campaign.update - 'entities' can not be null");
333 }
334 for (Campaign entity : entities)
335 {
336 update(entity);
337 }
338 }
339
340
341
342
343 @Override
344 public void remove(Campaign campaign)
345 {
346 if (campaign == null)
347 {
348 throw new IllegalArgumentException(
349 "Campaign.remove - 'campaign' can not be null");
350 }
351 this.getSessionFactory().getCurrentSession().delete(campaign);
352 }
353
354
355
356
357 @Override
358 public void remove(Integer campaignId)
359 {
360 if (campaignId == null)
361 {
362 throw new IllegalArgumentException(
363 "Campaign.remove - 'campaignId' can not be null");
364 }
365 Campaign entity = this.get(campaignId);
366 if (entity != null)
367 {
368 this.remove(entity);
369 }
370 }
371
372
373
374
375 @Override
376 public void remove(Collection<Campaign> entities)
377 {
378 if (entities == null)
379 {
380 throw new IllegalArgumentException(
381 "Campaign.remove - 'entities' can not be null");
382 }
383 deleteAll(entities);
384 }
385
386
387
388 @Override
389 public CampaignVO save(final CampaignVO campaign)
390 {
391 if (campaign == null)
392 {
393 throw new IllegalArgumentException(
394 "fr.ifremer.quadrige2.core.dao.data.survey.CampaignDao.save(CampaignVO campaign) - 'campaign' can not be null");
395 }
396 try
397 {
398 return this.handleSave(campaign);
399 }
400 catch (Throwable th)
401 {
402 throw new RuntimeException(
403 "Error performing 'CampaignDao.save(CampaignVO campaign)' --> " + th,
404 th);
405 }
406 }
407
408
409
410
411
412
413
414 protected abstract CampaignVO handleSave(CampaignVO campaign)
415 throws Exception;
416
417
418
419
420 @Override
421 public Long countSurveyUsage(final int campaignId)
422 {
423 try
424 {
425 return this.handleCountSurveyUsage(campaignId);
426 }
427 catch (Throwable th)
428 {
429 throw new RuntimeException(
430 "Error performing 'CampaignDao.countSurveyUsage(int campaignId)' --> " + th,
431 th);
432 }
433 }
434
435
436
437
438
439
440
441 protected abstract Long handleCountSurveyUsage(int campaignId)
442 throws Exception;
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464 public Object transformEntity(final int transform, final Campaign entity)
465 {
466 Object target = null;
467 if (entity != null)
468 {
469 switch (transform)
470 {
471 case TRANSFORM_CAMPAIGNVO :
472 target = toCampaignVO(entity);
473 break;
474 case CampaignDao.TRANSFORM_NONE :
475 default:
476 target = entity;
477 }
478 }
479 return target;
480 }
481
482
483
484
485 @Override
486 public void transformEntities(final int transform, final Collection<?> entities)
487 {
488 switch (transform)
489 {
490 case TRANSFORM_CAMPAIGNVO :
491 toCampaignVOCollection(entities);
492 break;
493 case CampaignDao.TRANSFORM_NONE :
494 default:
495
496 }
497 }
498
499
500
501
502 public void toEntities(final Collection<?> results)
503 {
504 if (results != null)
505 {
506 CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
507 }
508 }
509
510
511
512
513
514
515 private Transformer ENTITYTRANSFORMER =
516 new Transformer()
517 {
518 public Object transform(Object input)
519 {
520 Object result = null;
521 if (input instanceof Object[])
522 {
523 result = toEntity((Object[])input);
524 }
525 else if (input instanceof Campaign)
526 {
527 result = input;
528 }
529 return result;
530 }
531 };
532
533
534
535
536
537 protected Campaign toEntity(Object[] row)
538 {
539 Campaign target = null;
540 if (row != null)
541 {
542 final int numberOfObjects = row.length;
543 for (int ctr = 0; ctr < numberOfObjects; ctr++)
544 {
545 final Object object = row[ctr];
546 if (object instanceof Campaign)
547 {
548 target = (Campaign)object;
549 break;
550 }
551 }
552 }
553 return target;
554 }
555
556
557
558
559 @Override
560 @SuppressWarnings({"unchecked"})
561 public final Collection<CampaignVO> toCampaignVOCollection(Collection<?> entities)
562 {
563 Collection<CampaignVO> result = new ArrayList<CampaignVO>();
564 if (entities != null)
565 {
566 CollectionUtils.transform(entities, this.CAMPAIGNVO_TRANSFORMER);
567 result.addAll((Collection<? extends CampaignVO>) entities);
568 }
569 return result;
570 }
571
572
573
574
575 @Override
576 @SuppressWarnings({ "unchecked" })
577 public final CampaignVO[] toCampaignVOArray(Collection<?> entities)
578 {
579 CampaignVO[] result = null;
580 if (entities != null)
581 {
582
583 final Collection collection = new ArrayList(entities);
584 this.toCampaignVOCollection(collection);
585 result = (CampaignVO[]) collection.toArray(new CampaignVO[collection.size()]);
586 }
587 return result;
588 }
589
590
591
592
593
594
595
596
597
598 protected CampaignVO toCampaignVO(Object[] row)
599 {
600 return this.toCampaignVO(this.toEntity(row));
601 }
602
603
604
605
606
607
608 private Transformer CAMPAIGNVO_TRANSFORMER =
609 new Transformer()
610 {
611 public Object transform(Object input)
612 {
613 Object result = null;
614 if (input instanceof Campaign)
615 {
616 result = toCampaignVO((Campaign)input);
617 }
618 else if (input instanceof Object[])
619 {
620 result = toCampaignVO((Object[])input);
621 }
622 return result;
623 }
624 };
625
626
627
628
629 @Override
630 public final void campaignVOToEntityCollection(Collection<?> instances)
631 {
632 if (instances != null)
633 {
634 for (final Iterator<?> iterator = instances.iterator(); iterator.hasNext();)
635 {
636
637 if (!(iterator.next() instanceof CampaignVO))
638 {
639 iterator.remove();
640 }
641 }
642 CollectionUtils.transform(instances, this.CampaignVOToEntityTransformer);
643 }
644 }
645
646 private final Transformer CampaignVOToEntityTransformer =
647 new Transformer()
648 {
649 public Object transform(Object input)
650 {
651 return campaignVOToEntity((CampaignVO)input);
652 }
653 };
654
655
656
657
658
659 @Override
660 public void toCampaignVO(
661 Campaign source,
662 CampaignVO target)
663 {
664 target.setCampaignId(source.getCampaignId());
665 target.setCampaignNm(source.getCampaignNm());
666 target.setCampaignStartDt(source.getCampaignStartDt());
667 target.setCampaignEndDt(source.getCampaignEndDt());
668 target.setCampaignSismerLk(source.getCampaignSismerLk());
669 target.setCampaignCm(source.getCampaignCm());
670 target.setCampaignPositionCm(source.getCampaignPositionCm());
671 target.setUpdateDt(source.getUpdateDt());
672 }
673
674
675
676
677 @Override
678 public CampaignVO toCampaignVO(final Campaign entity)
679 {
680 CampaignVO target = null;
681 if (entity != null)
682 {
683 target = new CampaignVO();
684 this.toCampaignVO(entity, target);
685 }
686 return target;
687 }
688
689
690
691
692 @Override
693 public void campaignVOToEntity(
694 CampaignVO source,
695 Campaign target,
696 boolean copyIfNull)
697 {
698 if (copyIfNull || source.getCampaignSismerLk() != null)
699 {
700 target.setCampaignSismerLk(source.getCampaignSismerLk());
701 }
702 if (copyIfNull || source.getCampaignStartDt() != null)
703 {
704 target.setCampaignStartDt(source.getCampaignStartDt());
705 }
706 if (copyIfNull || source.getCampaignEndDt() != null)
707 {
708 target.setCampaignEndDt(source.getCampaignEndDt());
709 }
710 if (copyIfNull || source.getCampaignNm() != null)
711 {
712 target.setCampaignNm(source.getCampaignNm());
713 }
714 if (copyIfNull || source.getCampaignCm() != null)
715 {
716 target.setCampaignCm(source.getCampaignCm());
717 }
718 if (copyIfNull || source.getCampaignPositionCm() != null)
719 {
720 target.setCampaignPositionCm(source.getCampaignPositionCm());
721 }
722 if (copyIfNull || source.getUpdateDt() != null)
723 {
724 target.setUpdateDt(source.getUpdateDt());
725 }
726 }
727
728
729
730
731
732
733
734 protected Principal getPrincipal()
735 {
736 return PrincipalStore.get();
737 }
738
739
740
741
742 @Override
743 @SuppressWarnings({ "unchecked" })
744 public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
745 {
746 try
747 {
748 search.setPageNumber(pageNumber);
749 search.setPageSize(pageSize);
750 final PropertySearch propertySearch = new PropertySearch(
751 this.getSession(), CampaignImpl.class, search);
752 final List results = propertySearch.executeAsList();
753 this.transformEntities(transform, results);
754 return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
755 }
756 catch (HibernateException ex)
757 {
758 throw ex;
759 }
760 }
761
762
763
764
765 @Override
766 public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
767 {
768 return this.search(CampaignDao.TRANSFORM_NONE, pageNumber, pageSize, search);
769 }
770
771
772
773
774 @Override
775 public Set<?> search(final int transform, final Search search)
776 {
777 try
778 {
779 final PropertySearch propertySearch = new PropertySearch(
780 this.getSession(), CampaignImpl.class, search);
781 final Set<?> results = propertySearch.executeAsSet();
782 this.transformEntities(transform, results);
783 return results;
784 }
785 catch (HibernateException ex)
786 {
787 throw ex;
788 }
789 }
790
791
792
793
794 @Override
795 @SuppressWarnings("unchecked")
796 public Set<Campaign> search(final Search search)
797 {
798 return (Set<Campaign>) this.search(CampaignDao.TRANSFORM_NONE, search);
799 }
800
801
802
803
804
805
806
807
808
809 @SuppressWarnings({ "unchecked" })
810 protected PaginationResult getPaginationResult(
811 final Query queryObject,
812 final int transform, int pageNumber, int pageSize)
813 {
814 try
815 {
816 final ScrollableResults scrollableResults = queryObject.scroll();
817 scrollableResults.last();
818 int totalCount = scrollableResults.getRowNumber();
819 totalCount = totalCount >= 0 ? totalCount + 1 : 0;
820 if (pageNumber > 0 && pageSize > 0)
821 {
822 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
823 queryObject.setMaxResults(pageSize);
824 }
825
826 Set results = new LinkedHashSet(queryObject.list());
827 transformEntities(transform, results);
828 return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
829 }
830 catch (HibernateException ex)
831 {
832 throw ex;
833 }
834 }
835
836
837 }