1
2
3
4
5
6 package fr.ifremer.quadrige3.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 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 NumericalPrecisionDaoBase
56 extends HibernateDaoSupport
57 implements NumericalPrecisionDao
58 {
59
60
61
62 @Override
63 public Object get(final int transform, final Integer numPrecId)
64 {
65 if (numPrecId == null)
66 {
67 throw new IllegalArgumentException(
68 "NumericalPrecision.get - 'numPrecId' can not be null");
69 }
70 final NumericalPrecision entity = get(NumericalPrecisionImpl.class, numPrecId);
71 return transformEntity(transform, entity);
72 }
73
74
75
76 @Override
77 public NumericalPrecision get(Integer numPrecId)
78 {
79 return (NumericalPrecision)this.get(TRANSFORM_NONE, numPrecId);
80 }
81
82
83
84
85 @Override
86 public Object load(final int transform, final Integer numPrecId)
87 {
88 if (numPrecId == null)
89 {
90 throw new IllegalArgumentException(
91 "NumericalPrecision.load - 'numPrecId' can not be null");
92 }
93 final NumericalPrecision entity = get(NumericalPrecisionImpl.class, numPrecId);
94 return transformEntity(transform, entity);
95 }
96
97
98
99
100 @Override
101 public NumericalPrecision load(Integer numPrecId)
102 {
103 return (NumericalPrecision)this.load(TRANSFORM_NONE, numPrecId);
104 }
105
106
107
108
109 @Override
110 @SuppressWarnings({"unchecked"})
111 public Collection<NumericalPrecision> loadAll()
112 {
113 return (Collection<NumericalPrecision>) this.loadAll(NumericalPrecisionDao.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(NumericalPrecisionDao.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(NumericalPrecisionImpl.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 NumericalPrecision create(NumericalPrecision numericalPrecision)
179 {
180 return (NumericalPrecision)this.create(NumericalPrecisionDao.TRANSFORM_NONE, numericalPrecision);
181 }
182
183
184
185
186 @Override
187 public Object create(final int transform, final NumericalPrecision numericalPrecision)
188 {
189 if (numericalPrecision == null)
190 {
191 throw new IllegalArgumentException(
192 "NumericalPrecision.create - 'numericalPrecision' can not be null");
193 }
194 this.getSessionFactory().getCurrentSession().save(numericalPrecision);
195 return this.transformEntity(transform, numericalPrecision);
196 }
197
198
199
200
201 @Override
202 @SuppressWarnings({"unchecked"})
203 public Collection<NumericalPrecision> create(final Collection<NumericalPrecision> entities)
204 {
205 return (Collection<NumericalPrecision>) create(NumericalPrecisionDao.TRANSFORM_NONE, entities);
206 }
207
208
209
210
211 @Override
212 public Collection<?> create(final int transform, final Collection<NumericalPrecision> entities)
213 {
214 if (entities == null)
215 {
216 throw new IllegalArgumentException(
217 "NumericalPrecision.create - 'entities' can not be null");
218 }
219 for (NumericalPrecision entity : entities)
220 {
221 create(transform, entity);
222 }
223 return entities;
224 }
225
226
227
228
229 @Override
230 public NumericalPrecision create(
231 String numPrecNm,
232 String numPrecDc,
233 Timestamp updateDt)
234 {
235 return (NumericalPrecision)this.create(NumericalPrecisionDao.TRANSFORM_NONE, numPrecNm, numPrecDc, updateDt);
236 }
237
238
239
240
241 @Override
242 public Object create(
243 final int transform,
244 String numPrecNm,
245 String numPrecDc,
246 Timestamp updateDt)
247 {
248 NumericalPrecision entity = new NumericalPrecisionImpl();
249 entity.setNumPrecNm(numPrecNm);
250 entity.setNumPrecDc(numPrecDc);
251 entity.setUpdateDt(updateDt);
252 return this.create(transform, entity);
253 }
254
255
256
257
258 @Override
259 public NumericalPrecision create(
260 String numPrecNm,
261 Status status)
262 {
263 return (NumericalPrecision)this.create(NumericalPrecisionDao.TRANSFORM_NONE, numPrecNm, status);
264 }
265
266
267
268
269 @Override
270 public Object create(
271 final int transform,
272 String numPrecNm,
273 Status status)
274 {
275 NumericalPrecision entity = new NumericalPrecisionImpl();
276 entity.setNumPrecNm(numPrecNm);
277 entity.setStatus(status);
278 return this.create(transform, entity);
279 }
280
281
282
283
284 @Override
285 public void update(NumericalPrecision numericalPrecision)
286 {
287 if (numericalPrecision == null)
288 {
289 throw new IllegalArgumentException(
290 "NumericalPrecision.update - 'numericalPrecision' can not be null");
291 }
292 this.getSessionFactory().getCurrentSession().update(numericalPrecision);
293 }
294
295
296
297
298 @Override
299 public void update(final Collection<NumericalPrecision> entities)
300 {
301 if (entities == null)
302 {
303 throw new IllegalArgumentException(
304 "NumericalPrecision.update - 'entities' can not be null");
305 }
306 for (NumericalPrecision entity : entities)
307 {
308 update(entity);
309 }
310 }
311
312
313
314
315 @Override
316 public void remove(NumericalPrecision numericalPrecision)
317 {
318 if (numericalPrecision == null)
319 {
320 throw new IllegalArgumentException(
321 "NumericalPrecision.remove - 'numericalPrecision' can not be null");
322 }
323 this.getSessionFactory().getCurrentSession().delete(numericalPrecision);
324 }
325
326
327
328
329 @Override
330 public void remove(Integer numPrecId)
331 {
332 if (numPrecId == null)
333 {
334 throw new IllegalArgumentException(
335 "NumericalPrecision.remove - 'numPrecId' can not be null");
336 }
337 NumericalPrecision entity = this.get(numPrecId);
338 if (entity != null)
339 {
340 this.remove(entity);
341 }
342 }
343
344
345
346
347 @Override
348 public void remove(Collection<NumericalPrecision> entities)
349 {
350 if (entities == null)
351 {
352 throw new IllegalArgumentException(
353 "NumericalPrecision.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 NumericalPrecision entity)
372 {
373 Object target = null;
374 if (entity != null)
375 {
376 switch (transform)
377 {
378 case NumericalPrecisionDao.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 NumericalPrecisionDao.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 NumericalPrecision)
427 {
428 result = input;
429 }
430 return result;
431 }
432 };
433
434
435
436
437
438 protected NumericalPrecision toEntity(Object[] row)
439 {
440 NumericalPrecision 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 NumericalPrecision)
448 {
449 target = (NumericalPrecision)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(), NumericalPrecisionImpl.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(NumericalPrecisionDao.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(), NumericalPrecisionImpl.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<NumericalPrecision> search(final Search search)
526 {
527 return (Set<NumericalPrecision>) this.search(NumericalPrecisionDao.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 }