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