1
2
3
4
5
6 package fr.ifremer.quadrige2.core.dao.administration.user;
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.user.LightQuserVO;
35 import fr.ifremer.quadrige2.core.vo.administration.user.QuserVO;
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 QuserDaoBase
63 extends HibernateDaoSupport
64 implements QuserDao
65 {
66
67
68
69 @Override
70 public Object get(final int transform, final Integer quserId)
71 {
72 if (quserId == null)
73 {
74 throw new IllegalArgumentException(
75 "Quser.get - 'quserId' can not be null");
76 }
77 final Quser entity = get(QuserImpl.class, quserId);
78 return transformEntity(transform, entity);
79 }
80
81
82
83 @Override
84 public Quser get(Integer quserId)
85 {
86 return (Quser)this.get(TRANSFORM_NONE, quserId);
87 }
88
89
90
91
92 @Override
93 public Object load(final int transform, final Integer quserId)
94 {
95 if (quserId == null)
96 {
97 throw new IllegalArgumentException(
98 "Quser.load - 'quserId' can not be null");
99 }
100 final Quser entity = get(QuserImpl.class, quserId);
101 return transformEntity(transform, entity);
102 }
103
104
105
106
107 @Override
108 public Quser load(Integer quserId)
109 {
110 return (Quser)this.load(TRANSFORM_NONE, quserId);
111 }
112
113
114
115
116 @Override
117 @SuppressWarnings({"unchecked"})
118 public Collection<Quser> loadAll()
119 {
120 return (Collection<Quser>) this.loadAll(QuserDao.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(QuserDao.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(QuserImpl.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 Quser create(Quser quser)
186 {
187 return (Quser)this.create(QuserDao.TRANSFORM_NONE, quser);
188 }
189
190
191
192
193 @Override
194 public Object create(final int transform, final Quser quser)
195 {
196 if (quser == null)
197 {
198 throw new IllegalArgumentException(
199 "Quser.create - 'quser' can not be null");
200 }
201 this.getSessionFactory().getCurrentSession().save(quser);
202 return this.transformEntity(transform, quser);
203 }
204
205
206
207
208 @Override
209 @SuppressWarnings({"unchecked"})
210 public Collection<Quser> create(final Collection<Quser> entities)
211 {
212 return (Collection<Quser>) create(QuserDao.TRANSFORM_NONE, entities);
213 }
214
215
216
217
218 @Override
219 public Collection<?> create(final int transform, final Collection<Quser> entities)
220 {
221 if (entities == null)
222 {
223 throw new IllegalArgumentException(
224 "Quser.create - 'entities' can not be null");
225 }
226 for (Quser entity : entities)
227 {
228 create(transform, entity);
229 }
230 return entities;
231 }
232
233
234
235
236 @Override
237 public Quser create(
238 String quserCd,
239 String quserLastNm,
240 String quserFirstNm,
241 String quserIntranetLg,
242 String quserExtranetLg,
243 String quserEMail,
244 String quserAddress,
245 String quserPhone,
246 String quserOrgan,
247 String quserAdminCenter,
248 String quserSite,
249 String quserLdapPresent,
250 String quserCryptPassword,
251 Date quserCreationDt,
252 Timestamp updateDt)
253 {
254 return (Quser)this.create(QuserDao.TRANSFORM_NONE, quserCd, quserLastNm, quserFirstNm, quserIntranetLg, quserExtranetLg, quserEMail, quserAddress, quserPhone, quserOrgan, quserAdminCenter, quserSite, quserLdapPresent, quserCryptPassword, quserCreationDt, updateDt);
255 }
256
257
258
259
260 @Override
261 public Object create(
262 final int transform,
263 String quserCd,
264 String quserLastNm,
265 String quserFirstNm,
266 String quserIntranetLg,
267 String quserExtranetLg,
268 String quserEMail,
269 String quserAddress,
270 String quserPhone,
271 String quserOrgan,
272 String quserAdminCenter,
273 String quserSite,
274 String quserLdapPresent,
275 String quserCryptPassword,
276 Date quserCreationDt,
277 Timestamp updateDt)
278 {
279 Quser entity = new QuserImpl();
280 entity.setQuserCd(quserCd);
281 entity.setQuserLastNm(quserLastNm);
282 entity.setQuserFirstNm(quserFirstNm);
283 entity.setQuserIntranetLg(quserIntranetLg);
284 entity.setQuserExtranetLg(quserExtranetLg);
285 entity.setQuserEMail(quserEMail);
286 entity.setQuserAddress(quserAddress);
287 entity.setQuserPhone(quserPhone);
288 entity.setQuserOrgan(quserOrgan);
289 entity.setQuserAdminCenter(quserAdminCenter);
290 entity.setQuserSite(quserSite);
291 entity.setQuserLdapPresent(quserLdapPresent);
292
293 entity.setQuserCreationDt(quserCreationDt);
294 entity.setUpdateDt(updateDt);
295 return this.create(transform, entity);
296 }
297
298
299
300
301 @Override
302 public Quser create(
303 String quserFirstNm,
304 String quserIntranetLg,
305 String quserLastNm,
306 Timestamp updateDt,
307 Department department,
308 Status status)
309 {
310 return (Quser)this.create(QuserDao.TRANSFORM_NONE, quserFirstNm, quserIntranetLg, quserLastNm, updateDt, department, status);
311 }
312
313
314
315
316 @Override
317 public Object create(
318 final int transform,
319 String quserFirstNm,
320 String quserIntranetLg,
321 String quserLastNm,
322 Timestamp updateDt,
323 Department department,
324 Status status)
325 {
326 Quser entity = new QuserImpl();
327 entity.setQuserFirstNm(quserFirstNm);
328 entity.setQuserIntranetLg(quserIntranetLg);
329 entity.setQuserLastNm(quserLastNm);
330 entity.setUpdateDt(updateDt);
331 entity.setDepartment(department);
332 entity.setStatus(status);
333 return this.create(transform, entity);
334 }
335
336
337
338
339 @Override
340 public void update(Quser quser)
341 {
342 if (quser == null)
343 {
344 throw new IllegalArgumentException(
345 "Quser.update - 'quser' can not be null");
346 }
347 this.getSessionFactory().getCurrentSession().update(quser);
348 }
349
350
351
352
353 @Override
354 public void update(final Collection<Quser> entities)
355 {
356 if (entities == null)
357 {
358 throw new IllegalArgumentException(
359 "Quser.update - 'entities' can not be null");
360 }
361 for (Quser entity : entities)
362 {
363 update(entity);
364 }
365 }
366
367
368
369
370 @Override
371 public void remove(Quser quser)
372 {
373 if (quser == null)
374 {
375 throw new IllegalArgumentException(
376 "Quser.remove - 'quser' can not be null");
377 }
378 this.getSessionFactory().getCurrentSession().delete(quser);
379 }
380
381
382
383
384 @Override
385 public void remove(Integer quserId)
386 {
387 if (quserId == null)
388 {
389 throw new IllegalArgumentException(
390 "Quser.remove - 'quserId' can not be null");
391 }
392 Quser entity = this.get(quserId);
393 if (entity != null)
394 {
395 this.remove(entity);
396 }
397 }
398
399
400
401
402 @Override
403 public void remove(Collection<Quser> entities)
404 {
405 if (entities == null)
406 {
407 throw new IllegalArgumentException(
408 "Quser.remove - 'entities' can not be null");
409 }
410 deleteAll(entities);
411 }
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433 public Object transformEntity(final int transform, final Quser entity)
434 {
435 Object target = null;
436 if (entity != null)
437 {
438 switch (transform)
439 {
440 case TRANSFORM_QUSERVO :
441 target = toQuserVO(entity);
442 break;
443 case TRANSFORM_LIGHTQUSERVO :
444 target = toLightQuserVO(entity);
445 break;
446 case QuserDao.TRANSFORM_NONE :
447 default:
448 target = entity;
449 }
450 }
451 return target;
452 }
453
454
455
456
457 @Override
458 public void transformEntities(final int transform, final Collection<?> entities)
459 {
460 switch (transform)
461 {
462 case TRANSFORM_QUSERVO :
463 toQuserVOCollection(entities);
464 break;
465 case TRANSFORM_LIGHTQUSERVO :
466 toLightQuserVOCollection(entities);
467 break;
468 case QuserDao.TRANSFORM_NONE :
469 default:
470
471 }
472 }
473
474
475
476
477 public void toEntities(final Collection<?> results)
478 {
479 if (results != null)
480 {
481 CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
482 }
483 }
484
485
486
487
488
489
490 private Transformer ENTITYTRANSFORMER =
491 new Transformer()
492 {
493 public Object transform(Object input)
494 {
495 Object result = null;
496 if (input instanceof Object[])
497 {
498 result = toEntity((Object[])input);
499 }
500 else if (input instanceof Quser)
501 {
502 result = input;
503 }
504 return result;
505 }
506 };
507
508
509
510
511
512 protected Quser toEntity(Object[] row)
513 {
514 Quser target = null;
515 if (row != null)
516 {
517 final int numberOfObjects = row.length;
518 for (int ctr = 0; ctr < numberOfObjects; ctr++)
519 {
520 final Object object = row[ctr];
521 if (object instanceof Quser)
522 {
523 target = (Quser)object;
524 break;
525 }
526 }
527 }
528 return target;
529 }
530
531
532
533
534 @Override
535 @SuppressWarnings({"unchecked"})
536 public final Collection<QuserVO> toQuserVOCollection(Collection<?> entities)
537 {
538 Collection<QuserVO> result = new ArrayList<QuserVO>();
539 if (entities != null)
540 {
541 CollectionUtils.transform(entities, this.QUSERVO_TRANSFORMER);
542 result.addAll((Collection<? extends QuserVO>) entities);
543 }
544 return result;
545 }
546
547
548
549
550 @Override
551 @SuppressWarnings({ "unchecked" })
552 public final QuserVO[] toQuserVOArray(Collection<?> entities)
553 {
554 QuserVO[] result = null;
555 if (entities != null)
556 {
557
558 final Collection collection = new ArrayList(entities);
559 this.toQuserVOCollection(collection);
560 result = (QuserVO[]) collection.toArray(new QuserVO[collection.size()]);
561 }
562 return result;
563 }
564
565
566
567
568
569
570
571
572
573 protected QuserVO toQuserVO(Object[] row)
574 {
575 return this.toQuserVO(this.toEntity(row));
576 }
577
578
579
580
581
582
583 private Transformer QUSERVO_TRANSFORMER =
584 new Transformer()
585 {
586 public Object transform(Object input)
587 {
588 Object result = null;
589 if (input instanceof Quser)
590 {
591 result = toQuserVO((Quser)input);
592 }
593 else if (input instanceof Object[])
594 {
595 result = toQuserVO((Object[])input);
596 }
597 return result;
598 }
599 };
600
601
602
603
604 @Override
605 public final void quserVOToEntityCollection(Collection<?> instances)
606 {
607 if (instances != null)
608 {
609 for (final Iterator<?> iterator = instances.iterator(); iterator.hasNext();)
610 {
611
612 if (!(iterator.next() instanceof QuserVO))
613 {
614 iterator.remove();
615 }
616 }
617 CollectionUtils.transform(instances, this.QuserVOToEntityTransformer);
618 }
619 }
620
621 private final Transformer QuserVOToEntityTransformer =
622 new Transformer()
623 {
624 public Object transform(Object input)
625 {
626 return quserVOToEntity((QuserVO)input);
627 }
628 };
629
630
631
632
633
634 @Override
635 public void toQuserVO(
636 Quser source,
637 QuserVO target)
638 {
639 target.setQuserId(source.getQuserId());
640 target.setQuserCd(source.getQuserCd());
641 target.setQuserLastNm(source.getQuserLastNm());
642 target.setQuserFirstNm(source.getQuserFirstNm());
643 target.setQuserIntranetLg(source.getQuserIntranetLg());
644 target.setQuserExtranetLg(source.getQuserExtranetLg());
645 target.setQuserEMail(source.getQuserEMail());
646 target.setQuserAddress(source.getQuserAddress());
647 target.setQuserPhone(source.getQuserPhone());
648 target.setQuserOrgan(source.getQuserOrgan());
649 target.setQuserAdminCenter(source.getQuserAdminCenter());
650 target.setQuserSite(source.getQuserSite());
651 target.setQuserLdapPresent(source.getQuserLdapPresent());
652 target.setQuserCreationDt(source.getQuserCreationDt());
653 target.setUpdateDt(source.getUpdateDt());
654
655
656 }
657
658
659
660
661 @Override
662 public QuserVO toQuserVO(final Quser entity)
663 {
664 QuserVO target = null;
665 if (entity != null)
666 {
667 target = new QuserVO();
668 this.toQuserVO(entity, target);
669 }
670 return target;
671 }
672
673
674
675
676 @Override
677 public void quserVOToEntity(
678 QuserVO source,
679 Quser target,
680 boolean copyIfNull)
681 {
682 if (copyIfNull || source.getQuserCd() != null)
683 {
684 target.setQuserCd(source.getQuserCd());
685 }
686 if (copyIfNull || source.getQuserLastNm() != null)
687 {
688 target.setQuserLastNm(source.getQuserLastNm());
689 }
690 if (copyIfNull || source.getQuserFirstNm() != null)
691 {
692 target.setQuserFirstNm(source.getQuserFirstNm());
693 }
694 if (copyIfNull || source.getQuserIntranetLg() != null)
695 {
696 target.setQuserIntranetLg(source.getQuserIntranetLg());
697 }
698 if (copyIfNull || source.getQuserExtranetLg() != null)
699 {
700 target.setQuserExtranetLg(source.getQuserExtranetLg());
701 }
702 if (copyIfNull || source.getQuserEMail() != null)
703 {
704 target.setQuserEMail(source.getQuserEMail());
705 }
706 if (copyIfNull || source.getQuserAddress() != null)
707 {
708 target.setQuserAddress(source.getQuserAddress());
709 }
710 if (copyIfNull || source.getQuserPhone() != null)
711 {
712 target.setQuserPhone(source.getQuserPhone());
713 }
714 if (copyIfNull || source.getQuserOrgan() != null)
715 {
716 target.setQuserOrgan(source.getQuserOrgan());
717 }
718 if (copyIfNull || source.getQuserAdminCenter() != null)
719 {
720 target.setQuserAdminCenter(source.getQuserAdminCenter());
721 }
722 if (copyIfNull || source.getQuserSite() != null)
723 {
724 target.setQuserSite(source.getQuserSite());
725 }
726 if (copyIfNull || source.getQuserLdapPresent() != null)
727 {
728 target.setQuserLdapPresent(source.getQuserLdapPresent());
729 }
730 if (copyIfNull || source.getQuserCreationDt() != null)
731 {
732 target.setQuserCreationDt(source.getQuserCreationDt());
733 }
734 if (copyIfNull || source.getUpdateDt() != null)
735 {
736 target.setUpdateDt(source.getUpdateDt());
737 }
738 }
739
740
741
742
743 @Override
744 @SuppressWarnings({"unchecked"})
745 public final Collection<LightQuserVO> toLightQuserVOCollection(Collection<?> entities)
746 {
747 Collection<LightQuserVO> result = new ArrayList<LightQuserVO>();
748 if (entities != null)
749 {
750 CollectionUtils.transform(entities, this.LIGHTQUSERVO_TRANSFORMER);
751 result.addAll((Collection<? extends LightQuserVO>) entities);
752 }
753 return result;
754 }
755
756
757
758
759 @Override
760 @SuppressWarnings({ "unchecked" })
761 public final LightQuserVO[] toLightQuserVOArray(Collection<?> entities)
762 {
763 LightQuserVO[] result = null;
764 if (entities != null)
765 {
766
767 final Collection collection = new ArrayList(entities);
768 this.toLightQuserVOCollection(collection);
769 result = (LightQuserVO[]) collection.toArray(new LightQuserVO[collection.size()]);
770 }
771 return result;
772 }
773
774
775
776
777
778
779
780
781
782 protected LightQuserVO toLightQuserVO(Object[] row)
783 {
784 return this.toLightQuserVO(this.toEntity(row));
785 }
786
787
788
789
790
791
792 private Transformer LIGHTQUSERVO_TRANSFORMER =
793 new Transformer()
794 {
795 public Object transform(Object input)
796 {
797 Object result = null;
798 if (input instanceof Quser)
799 {
800 result = toLightQuserVO((Quser)input);
801 }
802 else if (input instanceof Object[])
803 {
804 result = toLightQuserVO((Object[])input);
805 }
806 return result;
807 }
808 };
809
810
811
812
813 @Override
814 public final void lightQuserVOToEntityCollection(Collection<?> instances)
815 {
816 if (instances != null)
817 {
818 for (final Iterator<?> iterator = instances.iterator(); iterator.hasNext();)
819 {
820
821 if (!(iterator.next() instanceof LightQuserVO))
822 {
823 iterator.remove();
824 }
825 }
826 CollectionUtils.transform(instances, this.LightQuserVOToEntityTransformer);
827 }
828 }
829
830 private final Transformer LightQuserVOToEntityTransformer =
831 new Transformer()
832 {
833 public Object transform(Object input)
834 {
835 return lightQuserVOToEntity((LightQuserVO)input);
836 }
837 };
838
839
840
841
842
843 @Override
844 public void toLightQuserVO(
845 Quser source,
846 LightQuserVO target)
847 {
848 }
849
850
851
852
853 @Override
854 public LightQuserVO toLightQuserVO(final Quser entity)
855 {
856 LightQuserVO target = null;
857 if (entity != null)
858 {
859 target = new LightQuserVO();
860 this.toLightQuserVO(entity, target);
861 }
862 return target;
863 }
864
865
866
867
868 @Override
869 public void lightQuserVOToEntity(
870 LightQuserVO source,
871 Quser target,
872 boolean copyIfNull)
873 {
874 }
875
876
877
878
879
880
881
882 protected Principal getPrincipal()
883 {
884 return PrincipalStore.get();
885 }
886
887
888
889
890 @Override
891 @SuppressWarnings({ "unchecked" })
892 public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
893 {
894 try
895 {
896 search.setPageNumber(pageNumber);
897 search.setPageSize(pageSize);
898 final PropertySearch propertySearch = new PropertySearch(
899 this.getSession(), QuserImpl.class, search);
900 final List results = propertySearch.executeAsList();
901 this.transformEntities(transform, results);
902 return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
903 }
904 catch (HibernateException ex)
905 {
906 throw ex;
907 }
908 }
909
910
911
912
913 @Override
914 public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
915 {
916 return this.search(QuserDao.TRANSFORM_NONE, pageNumber, pageSize, search);
917 }
918
919
920
921
922 @Override
923 public Set<?> search(final int transform, final Search search)
924 {
925 try
926 {
927 final PropertySearch propertySearch = new PropertySearch(
928 this.getSession(), QuserImpl.class, search);
929 final Set<?> results = propertySearch.executeAsSet();
930 this.transformEntities(transform, results);
931 return results;
932 }
933 catch (HibernateException ex)
934 {
935 throw ex;
936 }
937 }
938
939
940
941
942 @Override
943 @SuppressWarnings("unchecked")
944 public Set<Quser> search(final Search search)
945 {
946 return (Set<Quser>) this.search(QuserDao.TRANSFORM_NONE, search);
947 }
948
949
950
951
952
953
954
955
956
957 @SuppressWarnings({ "unchecked" })
958 protected PaginationResult getPaginationResult(
959 final Query queryObject,
960 final int transform, int pageNumber, int pageSize)
961 {
962 try
963 {
964 final ScrollableResults scrollableResults = queryObject.scroll();
965 scrollableResults.last();
966 int totalCount = scrollableResults.getRowNumber();
967 totalCount = totalCount >= 0 ? totalCount + 1 : 0;
968 if (pageNumber > 0 && pageSize > 0)
969 {
970 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
971 queryObject.setMaxResults(pageSize);
972 }
973
974 Set results = new LinkedHashSet(queryObject.list());
975 transformEntities(transform, results);
976 return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
977 }
978 catch (HibernateException ex)
979 {
980 throw ex;
981 }
982 }
983
984
985 }