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