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