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