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