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