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