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