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