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