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