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