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