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