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