1
2
3
4
5
6 package fr.ifremer.quadrige2.core.dao.data.survey;
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.administration.user.Department;
33 import fr.ifremer.quadrige2.core.dao.referential.QualityFlag;
34 import fr.ifremer.quadrige2.core.dao.referential.monitoringLocation.MonitoringLocation;
35 import fr.ifremer.quadrige2.core.dao.technical.hibernate.HibernateDaoSupport;
36 import java.security.Principal;
37 import java.sql.Timestamp;
38 import java.util.Collection;
39 import java.util.Date;
40 import java.util.LinkedHashSet;
41 import java.util.List;
42 import java.util.Set;
43 import javax.annotation.Resource;
44 import org.andromda.spring.PaginationResult;
45 import org.apache.commons.collections.CollectionUtils;
46 import org.apache.commons.collections.Transformer;
47 import org.hibernate.Criteria;
48 import org.hibernate.HibernateException;
49 import org.hibernate.Query;
50 import org.hibernate.ScrollableResults;
51
52
53
54
55
56
57
58
59
60 public abstract class SurveyDaoBase
61 extends HibernateDaoSupport
62 implements SurveyDao
63 {
64
65
66
67 @Override
68 public Object get(final int transform, final Integer surveyId)
69 {
70 if (surveyId == null)
71 {
72 throw new IllegalArgumentException(
73 "Survey.get - 'surveyId' can not be null");
74 }
75 final Survey entity = get(SurveyImpl.class, surveyId);
76 return transformEntity(transform, entity);
77 }
78
79
80
81 @Override
82 public Survey get(Integer surveyId)
83 {
84 return (Survey)this.get(TRANSFORM_NONE, surveyId);
85 }
86
87
88
89
90 @Override
91 public Object load(final int transform, final Integer surveyId)
92 {
93 if (surveyId == null)
94 {
95 throw new IllegalArgumentException(
96 "Survey.load - 'surveyId' can not be null");
97 }
98 final Survey entity = get(SurveyImpl.class, surveyId);
99 return transformEntity(transform, entity);
100 }
101
102
103
104
105 @Override
106 public Survey load(Integer surveyId)
107 {
108 return (Survey)this.load(TRANSFORM_NONE, surveyId);
109 }
110
111
112
113
114 @Override
115 @SuppressWarnings({"unchecked"})
116 public Collection<Survey> loadAll()
117 {
118 return (Collection<Survey>) this.loadAll(SurveyDao.TRANSFORM_NONE);
119 }
120
121
122
123
124 @Override
125 public Collection<?> loadAll(final int transform)
126 {
127 return this.loadAll(transform, -1, -1);
128 }
129
130
131
132
133 @Override
134 public Collection<?> loadAll(final int pageNumber, final int pageSize)
135 {
136 return this.loadAll(SurveyDao.TRANSFORM_NONE, pageNumber, pageSize);
137 }
138
139
140
141
142 @Override
143 public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
144 {
145 try
146 {
147 final Criteria criteria = this.getSession().createCriteria(SurveyImpl.class);
148 if (pageNumber > 0 && pageSize > 0)
149 {
150 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
151 criteria.setMaxResults(pageSize);
152 }
153 final Collection<?> results = criteria.list();
154 this.transformEntities(transform, results);
155 return results;
156 }
157 catch (HibernateException ex)
158 {
159 throw ex;
160 }
161 }
162
163
164
165
166
167
168
169 protected int calculateFirstResult(int pageNumber, int pageSize)
170 {
171 int firstResult = 0;
172 if (pageNumber > 0)
173 {
174 firstResult = (pageNumber - 1) * pageSize;
175 }
176 return firstResult;
177 }
178
179
180
181
182 @Override
183 public Survey create(Survey survey)
184 {
185 return (Survey)this.create(SurveyDao.TRANSFORM_NONE, survey);
186 }
187
188
189
190
191 @Override
192 public Object create(final int transform, final Survey survey)
193 {
194 if (survey == null)
195 {
196 throw new IllegalArgumentException(
197 "Survey.create - 'survey' can not be null");
198 }
199 this.getSessionFactory().getCurrentSession().save(survey);
200 return this.transformEntity(transform, survey);
201 }
202
203
204
205
206 @Override
207 @SuppressWarnings({"unchecked"})
208 public Collection<Survey> create(final Collection<Survey> entities)
209 {
210 return (Collection<Survey>) create(SurveyDao.TRANSFORM_NONE, entities);
211 }
212
213
214
215
216 @Override
217 public Collection<?> create(final int transform, final Collection<Survey> entities)
218 {
219 if (entities == null)
220 {
221 throw new IllegalArgumentException(
222 "Survey.create - 'entities' can not be null");
223 }
224 for (Survey entity : entities)
225 {
226 create(transform, entity);
227 }
228 return entities;
229 }
230
231
232
233
234 @Override
235 public Survey create(
236 Date surveyDt,
237 Double surveyTime,
238 Double surveyNumberIndiv,
239 Float surveyBottomDepth,
240 String surveyLb,
241 String surveyCm,
242 Double surveyUtFormat,
243 Date surveyControlDt,
244 Date surveyValidDt,
245 String surveyValidCm,
246 Date surveyQualifDt,
247 String surveyQualifCm,
248 String surveyActualPosition,
249 String surveyPositionCm,
250 Date surveyGeometryValidDt,
251 String surveyScope,
252 String surveyHasMeas,
253 Timestamp updateDt)
254 {
255 return (Survey)this.create(SurveyDao.TRANSFORM_NONE, surveyDt, surveyTime, surveyNumberIndiv, surveyBottomDepth, surveyLb, surveyCm, surveyUtFormat, surveyControlDt, surveyValidDt, surveyValidCm, surveyQualifDt, surveyQualifCm, surveyActualPosition, surveyPositionCm, surveyGeometryValidDt, surveyScope, surveyHasMeas, updateDt);
256 }
257
258
259
260
261 @Override
262 public Object create(
263 final int transform,
264 Date surveyDt,
265 Double surveyTime,
266 Double surveyNumberIndiv,
267 Float surveyBottomDepth,
268 String surveyLb,
269 String surveyCm,
270 Double surveyUtFormat,
271 Date surveyControlDt,
272 Date surveyValidDt,
273 String surveyValidCm,
274 Date surveyQualifDt,
275 String surveyQualifCm,
276 String surveyActualPosition,
277 String surveyPositionCm,
278 Date surveyGeometryValidDt,
279 String surveyScope,
280 String surveyHasMeas,
281 Timestamp updateDt)
282 {
283 Survey entity = new SurveyImpl();
284 entity.setSurveyDt(surveyDt);
285 entity.setSurveyTime(surveyTime);
286 entity.setSurveyNumberIndiv(surveyNumberIndiv);
287 entity.setSurveyBottomDepth(surveyBottomDepth);
288 entity.setSurveyLb(surveyLb);
289 entity.setSurveyCm(surveyCm);
290 entity.setSurveyUtFormat(surveyUtFormat);
291 entity.setSurveyControlDt(surveyControlDt);
292 entity.setSurveyValidDt(surveyValidDt);
293 entity.setSurveyValidCm(surveyValidCm);
294 entity.setSurveyQualifDt(surveyQualifDt);
295 entity.setSurveyQualifCm(surveyQualifCm);
296 entity.setSurveyActualPosition(surveyActualPosition);
297 entity.setSurveyPositionCm(surveyPositionCm);
298 entity.setSurveyGeometryValidDt(surveyGeometryValidDt);
299 entity.setSurveyScope(surveyScope);
300 entity.setSurveyHasMeas(surveyHasMeas);
301 entity.setUpdateDt(updateDt);
302
303 return this.create(transform, entity);
304 }
305
306
307
308
309 @Override
310 public Survey create(
311 Date surveyDt,
312
313 Timestamp updateDt,
314 Department recorderDepartment,
315 MonitoringLocation monitoringLocation,
316 QualityFlag qualityFlag)
317 {
318 return (Survey)this.create(SurveyDao.TRANSFORM_NONE, surveyDt, updateDt, recorderDepartment, monitoringLocation, qualityFlag);
319 }
320
321
322
323
324 @Override
325 public Object create(
326 final int transform,
327 Date surveyDt,
328
329 Timestamp updateDt,
330 Department recorderDepartment,
331 MonitoringLocation monitoringLocation,
332 QualityFlag qualityFlag)
333 {
334 Survey entity = new SurveyImpl();
335 entity.setSurveyDt(surveyDt);
336
337 entity.setUpdateDt(updateDt);
338 entity.setRecorderDepartment(recorderDepartment);
339 entity.setMonitoringLocation(monitoringLocation);
340 entity.setQualityFlag(qualityFlag);
341 return this.create(transform, entity);
342 }
343
344
345
346
347 @Override
348 public void update(Survey survey)
349 {
350 if (survey == null)
351 {
352 throw new IllegalArgumentException(
353 "Survey.update - 'survey' can not be null");
354 }
355 this.getSessionFactory().getCurrentSession().update(survey);
356 }
357
358
359
360
361 @Override
362 public void update(final Collection<Survey> entities)
363 {
364 if (entities == null)
365 {
366 throw new IllegalArgumentException(
367 "Survey.update - 'entities' can not be null");
368 }
369 for (Survey entity : entities)
370 {
371 update(entity);
372 }
373 }
374
375
376
377
378 @Override
379 public void remove(Survey survey)
380 {
381 if (survey == null)
382 {
383 throw new IllegalArgumentException(
384 "Survey.remove - 'survey' can not be null");
385 }
386 this.getSessionFactory().getCurrentSession().delete(survey);
387 }
388
389
390
391
392 @Override
393 public void remove(Integer surveyId)
394 {
395 if (surveyId == null)
396 {
397 throw new IllegalArgumentException(
398 "Survey.remove - 'surveyId' can not be null");
399 }
400 Survey entity = this.get(surveyId);
401 if (entity != null)
402 {
403 this.remove(entity);
404 }
405 }
406
407
408
409
410 @Override
411 public void remove(Collection<Survey> entities)
412 {
413 if (entities == null)
414 {
415 throw new IllegalArgumentException(
416 "Survey.remove - 'entities' can not be null");
417 }
418 deleteAll(entities);
419 }
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434 public Object transformEntity(final int transform, final Survey entity)
435 {
436 Object target = null;
437 if (entity != null)
438 {
439 switch (transform)
440 {
441 case SurveyDao.TRANSFORM_NONE :
442 default:
443 target = entity;
444 }
445 }
446 return target;
447 }
448
449
450
451
452 @Override
453 public void transformEntities(final int transform, final Collection<?> entities)
454 {
455 switch (transform)
456 {
457 case SurveyDao.TRANSFORM_NONE :
458 default:
459
460 }
461 }
462
463
464
465
466 public void toEntities(final Collection<?> results)
467 {
468 if (results != null)
469 {
470 CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
471 }
472 }
473
474
475
476
477
478
479 private Transformer ENTITYTRANSFORMER =
480 new Transformer()
481 {
482 public Object transform(Object input)
483 {
484 Object result = null;
485 if (input instanceof Object[])
486 {
487 result = toEntity((Object[])input);
488 }
489 else if (input instanceof Survey)
490 {
491 result = input;
492 }
493 return result;
494 }
495 };
496
497
498
499
500
501 protected Survey toEntity(Object[] row)
502 {
503 Survey target = null;
504 if (row != null)
505 {
506 final int numberOfObjects = row.length;
507 for (int ctr = 0; ctr < numberOfObjects; ctr++)
508 {
509 final Object object = row[ctr];
510 if (object instanceof Survey)
511 {
512 target = (Survey)object;
513 break;
514 }
515 }
516 }
517 return target;
518 }
519
520
521
522
523
524
525
526 protected Principal getPrincipal()
527 {
528 return PrincipalStore.get();
529 }
530
531
532
533
534 @Override
535 @SuppressWarnings({ "unchecked" })
536 public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
537 {
538 try
539 {
540 search.setPageNumber(pageNumber);
541 search.setPageSize(pageSize);
542 final PropertySearch propertySearch = new PropertySearch(
543 this.getSession(), SurveyImpl.class, search);
544 final List results = propertySearch.executeAsList();
545 this.transformEntities(transform, results);
546 return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
547 }
548 catch (HibernateException ex)
549 {
550 throw ex;
551 }
552 }
553
554
555
556
557 @Override
558 public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
559 {
560 return this.search(SurveyDao.TRANSFORM_NONE, pageNumber, pageSize, search);
561 }
562
563
564
565
566 @Override
567 public Set<?> search(final int transform, final Search search)
568 {
569 try
570 {
571 final PropertySearch propertySearch = new PropertySearch(
572 this.getSession(), SurveyImpl.class, search);
573 final Set<?> results = propertySearch.executeAsSet();
574 this.transformEntities(transform, results);
575 return results;
576 }
577 catch (HibernateException ex)
578 {
579 throw ex;
580 }
581 }
582
583
584
585
586 @Override
587 @SuppressWarnings("unchecked")
588 public Set<Survey> search(final Search search)
589 {
590 return (Set<Survey>) this.search(SurveyDao.TRANSFORM_NONE, search);
591 }
592
593
594
595
596
597
598
599
600
601 @SuppressWarnings({ "unchecked" })
602 protected PaginationResult getPaginationResult(
603 final Query queryObject,
604 final int transform, int pageNumber, int pageSize)
605 {
606 try
607 {
608 final ScrollableResults scrollableResults = queryObject.scroll();
609 scrollableResults.last();
610 int totalCount = scrollableResults.getRowNumber();
611 totalCount = totalCount >= 0 ? totalCount + 1 : 0;
612 if (pageNumber > 0 && pageSize > 0)
613 {
614 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
615 queryObject.setMaxResults(pageSize);
616 }
617
618 Set results = new LinkedHashSet(queryObject.list());
619 transformEntities(transform, results);
620 return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
621 }
622 catch (HibernateException ex)
623 {
624 throw ex;
625 }
626 }
627
628
629 }