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