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