1
2
3
4
5
6 package fr.ifremer.quadrige2.core.dao.data.sample;
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.administration.user.Department;
33 import fr.ifremer.quadrige2.core.dao.data.samplingoperation.SamplingOperation;
34 import fr.ifremer.quadrige2.core.dao.referential.QualityFlag;
35 import fr.ifremer.quadrige2.core.dao.referential.pmfm.Matrix;
36 import fr.ifremer.quadrige2.core.dao.technical.hibernate.HibernateDaoSupport;
37 import java.security.Principal;
38 import java.sql.Timestamp;
39 import java.util.Collection;
40 import java.util.Date;
41 import java.util.LinkedHashSet;
42 import java.util.List;
43 import java.util.Set;
44 import javax.annotation.Resource;
45 import org.andromda.spring.PaginationResult;
46 import org.apache.commons.collections.CollectionUtils;
47 import org.apache.commons.collections.Transformer;
48 import org.hibernate.Criteria;
49 import org.hibernate.HibernateException;
50 import org.hibernate.Query;
51 import org.hibernate.ScrollableResults;
52
53
54
55
56
57
58
59
60
61 public abstract class SampleDaoBase
62 extends HibernateDaoSupport
63 implements SampleDao
64 {
65
66
67
68 @Override
69 public Object get(final int transform, final Integer sampleId)
70 {
71 if (sampleId == null)
72 {
73 throw new IllegalArgumentException(
74 "Sample.get - 'sampleId' can not be null");
75 }
76 final Sample entity = get(SampleImpl.class, sampleId);
77 return transformEntity(transform, entity);
78 }
79
80
81
82 @Override
83 public Sample get(Integer sampleId)
84 {
85 return (Sample)this.get(TRANSFORM_NONE, sampleId);
86 }
87
88
89
90
91 @Override
92 public Object load(final int transform, final Integer sampleId)
93 {
94 if (sampleId == null)
95 {
96 throw new IllegalArgumentException(
97 "Sample.load - 'sampleId' can not be null");
98 }
99 final Sample entity = get(SampleImpl.class, sampleId);
100 return transformEntity(transform, entity);
101 }
102
103
104
105
106 @Override
107 public Sample load(Integer sampleId)
108 {
109 return (Sample)this.load(TRANSFORM_NONE, sampleId);
110 }
111
112
113
114
115 @Override
116 @SuppressWarnings({"unchecked"})
117 public Collection<Sample> loadAll()
118 {
119 return (Collection<Sample>) this.loadAll(SampleDao.TRANSFORM_NONE);
120 }
121
122
123
124
125 @Override
126 public Collection<?> loadAll(final int transform)
127 {
128 return this.loadAll(transform, -1, -1);
129 }
130
131
132
133
134 @Override
135 public Collection<?> loadAll(final int pageNumber, final int pageSize)
136 {
137 return this.loadAll(SampleDao.TRANSFORM_NONE, pageNumber, pageSize);
138 }
139
140
141
142
143 @Override
144 public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
145 {
146 try
147 {
148 final Criteria criteria = this.getSession().createCriteria(SampleImpl.class);
149 if (pageNumber > 0 && pageSize > 0)
150 {
151 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
152 criteria.setMaxResults(pageSize);
153 }
154 final Collection<?> results = criteria.list();
155 this.transformEntities(transform, results);
156 return results;
157 }
158 catch (HibernateException ex)
159 {
160 throw ex;
161 }
162 }
163
164
165
166
167
168
169
170 protected int calculateFirstResult(int pageNumber, int pageSize)
171 {
172 int firstResult = 0;
173 if (pageNumber > 0)
174 {
175 firstResult = (pageNumber - 1) * pageSize;
176 }
177 return firstResult;
178 }
179
180
181
182
183 @Override
184 public Sample create(Sample sample)
185 {
186 return (Sample)this.create(SampleDao.TRANSFORM_NONE, sample);
187 }
188
189
190
191
192 @Override
193 public Object create(final int transform, final Sample sample)
194 {
195 if (sample == null)
196 {
197 throw new IllegalArgumentException(
198 "Sample.create - 'sample' can not be null");
199 }
200 this.getSessionFactory().getCurrentSession().save(sample);
201 return this.transformEntity(transform, sample);
202 }
203
204
205
206
207 @Override
208 @SuppressWarnings({"unchecked"})
209 public Collection<Sample> create(final Collection<Sample> entities)
210 {
211 return (Collection<Sample>) create(SampleDao.TRANSFORM_NONE, entities);
212 }
213
214
215
216
217 @Override
218 public Collection<?> create(final int transform, final Collection<Sample> entities)
219 {
220 if (entities == null)
221 {
222 throw new IllegalArgumentException(
223 "Sample.create - 'entities' can not be null");
224 }
225 for (Sample entity : entities)
226 {
227 create(transform, entity);
228 }
229 return entities;
230 }
231
232
233
234
235 @Override
236 public Sample create(
237 String sampleLb,
238 Double sampleNumberIndiv,
239 Float sampleSize,
240 String sampleCm,
241 Date sampleControlDt,
242 Date sampleValidDt,
243 Date sampleQualifDt,
244 String sampleQualifCm,
245 String sampleScope,
246 String sampleHasMeas,
247 Timestamp updateDt)
248 {
249 return (Sample)this.create(SampleDao.TRANSFORM_NONE, sampleLb, sampleNumberIndiv, sampleSize, sampleCm, sampleControlDt, sampleValidDt, sampleQualifDt, sampleQualifCm, sampleScope, sampleHasMeas, updateDt);
250 }
251
252
253
254
255 @Override
256 public Object create(
257 final int transform,
258 String sampleLb,
259 Double sampleNumberIndiv,
260 Float sampleSize,
261 String sampleCm,
262 Date sampleControlDt,
263 Date sampleValidDt,
264 Date sampleQualifDt,
265 String sampleQualifCm,
266 String sampleScope,
267 String sampleHasMeas,
268 Timestamp updateDt)
269 {
270 Sample entity = new SampleImpl();
271 entity.setSampleLb(sampleLb);
272 entity.setSampleNumberIndiv(sampleNumberIndiv);
273 entity.setSampleSize(sampleSize);
274 entity.setSampleCm(sampleCm);
275 entity.setSampleControlDt(sampleControlDt);
276 entity.setSampleValidDt(sampleValidDt);
277 entity.setSampleQualifDt(sampleQualifDt);
278 entity.setSampleQualifCm(sampleQualifCm);
279 entity.setSampleScope(sampleScope);
280 entity.setSampleHasMeas(sampleHasMeas);
281 entity.setUpdateDt(updateDt);
282 return this.create(transform, entity);
283 }
284
285
286
287
288 @Override
289 public Sample create(
290 Timestamp updateDt,
291 Department recorderDepartment,
292 Matrix matrix,
293 QualityFlag qualityFlag,
294 SamplingOperation samplingOperation)
295 {
296 return (Sample)this.create(SampleDao.TRANSFORM_NONE, updateDt, recorderDepartment, matrix, qualityFlag, samplingOperation);
297 }
298
299
300
301
302 @Override
303 public Object create(
304 final int transform,
305 Timestamp updateDt,
306 Department recorderDepartment,
307 Matrix matrix,
308 QualityFlag qualityFlag,
309 SamplingOperation samplingOperation)
310 {
311 Sample entity = new SampleImpl();
312 entity.setUpdateDt(updateDt);
313 entity.setRecorderDepartment(recorderDepartment);
314 entity.setMatrix(matrix);
315 entity.setQualityFlag(qualityFlag);
316 entity.setSamplingOperation(samplingOperation);
317 return this.create(transform, entity);
318 }
319
320
321
322
323 @Override
324 public void update(Sample sample)
325 {
326 if (sample == null)
327 {
328 throw new IllegalArgumentException(
329 "Sample.update - 'sample' can not be null");
330 }
331 this.getSessionFactory().getCurrentSession().update(sample);
332 }
333
334
335
336
337 @Override
338 public void update(final Collection<Sample> entities)
339 {
340 if (entities == null)
341 {
342 throw new IllegalArgumentException(
343 "Sample.update - 'entities' can not be null");
344 }
345 for (Sample entity : entities)
346 {
347 update(entity);
348 }
349 }
350
351
352
353
354 @Override
355 public void remove(Sample sample)
356 {
357 if (sample == null)
358 {
359 throw new IllegalArgumentException(
360 "Sample.remove - 'sample' can not be null");
361 }
362 this.getSessionFactory().getCurrentSession().delete(sample);
363 }
364
365
366
367
368 @Override
369 public void remove(Integer sampleId)
370 {
371 if (sampleId == null)
372 {
373 throw new IllegalArgumentException(
374 "Sample.remove - 'sampleId' can not be null");
375 }
376 Sample entity = this.get(sampleId);
377 if (entity != null)
378 {
379 this.remove(entity);
380 }
381 }
382
383
384
385
386 @Override
387 public void remove(Collection<Sample> entities)
388 {
389 if (entities == null)
390 {
391 throw new IllegalArgumentException(
392 "Sample.remove - 'entities' can not be null");
393 }
394 deleteAll(entities);
395 }
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410 public Object transformEntity(final int transform, final Sample entity)
411 {
412 Object target = null;
413 if (entity != null)
414 {
415 switch (transform)
416 {
417 case SampleDao.TRANSFORM_NONE :
418 default:
419 target = entity;
420 }
421 }
422 return target;
423 }
424
425
426
427
428 @Override
429 public void transformEntities(final int transform, final Collection<?> entities)
430 {
431 switch (transform)
432 {
433 case SampleDao.TRANSFORM_NONE :
434 default:
435
436 }
437 }
438
439
440
441
442 public void toEntities(final Collection<?> results)
443 {
444 if (results != null)
445 {
446 CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
447 }
448 }
449
450
451
452
453
454
455 private Transformer ENTITYTRANSFORMER =
456 new Transformer()
457 {
458 public Object transform(Object input)
459 {
460 Object result = null;
461 if (input instanceof Object[])
462 {
463 result = toEntity((Object[])input);
464 }
465 else if (input instanceof Sample)
466 {
467 result = input;
468 }
469 return result;
470 }
471 };
472
473
474
475
476
477 protected Sample toEntity(Object[] row)
478 {
479 Sample target = null;
480 if (row != null)
481 {
482 final int numberOfObjects = row.length;
483 for (int ctr = 0; ctr < numberOfObjects; ctr++)
484 {
485 final Object object = row[ctr];
486 if (object instanceof Sample)
487 {
488 target = (Sample)object;
489 break;
490 }
491 }
492 }
493 return target;
494 }
495
496
497
498
499
500
501
502 protected Principal getPrincipal()
503 {
504 return PrincipalStore.get();
505 }
506
507
508
509
510 @Override
511 @SuppressWarnings({ "unchecked" })
512 public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
513 {
514 try
515 {
516 search.setPageNumber(pageNumber);
517 search.setPageSize(pageSize);
518 final PropertySearch propertySearch = new PropertySearch(
519 this.getSession(), SampleImpl.class, search);
520 final List results = propertySearch.executeAsList();
521 this.transformEntities(transform, results);
522 return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
523 }
524 catch (HibernateException ex)
525 {
526 throw ex;
527 }
528 }
529
530
531
532
533 @Override
534 public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
535 {
536 return this.search(SampleDao.TRANSFORM_NONE, pageNumber, pageSize, search);
537 }
538
539
540
541
542 @Override
543 public Set<?> search(final int transform, final Search search)
544 {
545 try
546 {
547 final PropertySearch propertySearch = new PropertySearch(
548 this.getSession(), SampleImpl.class, search);
549 final Set<?> results = propertySearch.executeAsSet();
550 this.transformEntities(transform, results);
551 return results;
552 }
553 catch (HibernateException ex)
554 {
555 throw ex;
556 }
557 }
558
559
560
561
562 @Override
563 @SuppressWarnings("unchecked")
564 public Set<Sample> search(final Search search)
565 {
566 return (Set<Sample>) this.search(SampleDao.TRANSFORM_NONE, search);
567 }
568
569
570
571
572
573
574
575
576
577 @SuppressWarnings({ "unchecked" })
578 protected PaginationResult getPaginationResult(
579 final Query queryObject,
580 final int transform, int pageNumber, int pageSize)
581 {
582 try
583 {
584 final ScrollableResults scrollableResults = queryObject.scroll();
585 scrollableResults.last();
586 int totalCount = scrollableResults.getRowNumber();
587 totalCount = totalCount >= 0 ? totalCount + 1 : 0;
588 if (pageNumber > 0 && pageSize > 0)
589 {
590 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
591 queryObject.setMaxResults(pageSize);
592 }
593
594 Set results = new LinkedHashSet(queryObject.list());
595 transformEntities(transform, results);
596 return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
597 }
598 catch (HibernateException ex)
599 {
600 throw ex;
601 }
602 }
603
604
605 }