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