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