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