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