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