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