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