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