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