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