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