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