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