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