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