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