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