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