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