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.Geometry;
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.referential.monitoringLocation.MonitoringLocation;
34 import fr.ifremer.quadrige2.core.dao.referential.monitoringLocation.PositionningSystem;
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.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 MonLocHisGeomDaoBase
60 extends HibernateDaoSupport
61 implements MonLocHisGeomDao
62 {
63
64
65
66 @Override
67 public Object get(final int transform, final Integer monLocHisGeomId)
68 {
69 if (monLocHisGeomId == null)
70 {
71 throw new IllegalArgumentException(
72 "MonLocHisGeom.get - 'monLocHisGeomId' can not be null");
73 }
74 final MonLocHisGeom entity = get(MonLocHisGeomImpl.class, monLocHisGeomId);
75 return transformEntity(transform, entity);
76 }
77
78
79
80 @Override
81 public MonLocHisGeom get(Integer monLocHisGeomId)
82 {
83 return (MonLocHisGeom)this.get(TRANSFORM_NONE, monLocHisGeomId);
84 }
85
86
87
88
89 @Override
90 public Object load(final int transform, final Integer monLocHisGeomId)
91 {
92 if (monLocHisGeomId == null)
93 {
94 throw new IllegalArgumentException(
95 "MonLocHisGeom.load - 'monLocHisGeomId' can not be null");
96 }
97 final MonLocHisGeom entity = get(MonLocHisGeomImpl.class, monLocHisGeomId);
98 return transformEntity(transform, entity);
99 }
100
101
102
103
104 @Override
105 public MonLocHisGeom load(Integer monLocHisGeomId)
106 {
107 return (MonLocHisGeom)this.load(TRANSFORM_NONE, monLocHisGeomId);
108 }
109
110
111
112
113 @Override
114 @SuppressWarnings({"unchecked"})
115 public Collection<MonLocHisGeom> loadAll()
116 {
117 return (Collection<MonLocHisGeom>) this.loadAll(MonLocHisGeomDao.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(MonLocHisGeomDao.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(MonLocHisGeomImpl.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 MonLocHisGeom create(MonLocHisGeom monLocHisGeom)
183 {
184 return (MonLocHisGeom)this.create(MonLocHisGeomDao.TRANSFORM_NONE, monLocHisGeom);
185 }
186
187
188
189
190 @Override
191 public Object create(final int transform, final MonLocHisGeom monLocHisGeom)
192 {
193 if (monLocHisGeom == null)
194 {
195 throw new IllegalArgumentException(
196 "MonLocHisGeom.create - 'monLocHisGeom' can not be null");
197 }
198 this.getSessionFactory().getCurrentSession().save(monLocHisGeom);
199 return this.transformEntity(transform, monLocHisGeom);
200 }
201
202
203
204
205 @Override
206 @SuppressWarnings({"unchecked"})
207 public Collection<MonLocHisGeom> create(final Collection<MonLocHisGeom> entities)
208 {
209 return (Collection<MonLocHisGeom>) create(MonLocHisGeomDao.TRANSFORM_NONE, entities);
210 }
211
212
213
214
215 @Override
216 public Collection<?> create(final int transform, final Collection<MonLocHisGeom> entities)
217 {
218 if (entities == null)
219 {
220 throw new IllegalArgumentException(
221 "MonLocHisGeom.create - 'entities' can not be null");
222 }
223 for (MonLocHisGeom entity : entities)
224 {
225 create(transform, entity);
226 }
227 return entities;
228 }
229
230
231
232
233 @Override
234 public MonLocHisGeom create(
235 Geometry monLocPosition,
236 Timestamp updateDt)
237 {
238 return (MonLocHisGeom)this.create(MonLocHisGeomDao.TRANSFORM_NONE, monLocPosition, updateDt);
239 }
240
241
242
243
244 @Override
245 public Object create(
246 final int transform,
247 Geometry monLocPosition,
248 Timestamp updateDt)
249 {
250 MonLocHisGeom entity = new MonLocHisGeomImpl();
251 entity.setMonLocPosition(monLocPosition);
252 entity.setUpdateDt(updateDt);
253 return this.create(transform, entity);
254 }
255
256
257
258
259 @Override
260 public MonLocHisGeom create(
261 Geometry monLocPosition,
262 Timestamp updateDt,
263 MonitoringLocation monitoringLocation,
264 PositionningSystem posSystemId)
265 {
266 return (MonLocHisGeom)this.create(MonLocHisGeomDao.TRANSFORM_NONE, monLocPosition, updateDt, monitoringLocation, posSystemId);
267 }
268
269
270
271
272 @Override
273 public Object create(
274 final int transform,
275 Geometry monLocPosition,
276 Timestamp updateDt,
277 MonitoringLocation monitoringLocation,
278 PositionningSystem posSystemId)
279 {
280 MonLocHisGeom entity = new MonLocHisGeomImpl();
281 entity.setMonLocPosition(monLocPosition);
282 entity.setUpdateDt(updateDt);
283 entity.setMonitoringLocation(monitoringLocation);
284 entity.setPosSystemId(posSystemId);
285 return this.create(transform, entity);
286 }
287
288
289
290
291 @Override
292 public void update(MonLocHisGeom monLocHisGeom)
293 {
294 if (monLocHisGeom == null)
295 {
296 throw new IllegalArgumentException(
297 "MonLocHisGeom.update - 'monLocHisGeom' can not be null");
298 }
299 this.getSessionFactory().getCurrentSession().update(monLocHisGeom);
300 }
301
302
303
304
305 @Override
306 public void update(final Collection<MonLocHisGeom> entities)
307 {
308 if (entities == null)
309 {
310 throw new IllegalArgumentException(
311 "MonLocHisGeom.update - 'entities' can not be null");
312 }
313 for (MonLocHisGeom entity : entities)
314 {
315 update(entity);
316 }
317 }
318
319
320
321
322 @Override
323 public void remove(MonLocHisGeom monLocHisGeom)
324 {
325 if (monLocHisGeom == null)
326 {
327 throw new IllegalArgumentException(
328 "MonLocHisGeom.remove - 'monLocHisGeom' can not be null");
329 }
330 this.getSessionFactory().getCurrentSession().delete(monLocHisGeom);
331 }
332
333
334
335
336 @Override
337 public void remove(Integer monLocHisGeomId)
338 {
339 if (monLocHisGeomId == null)
340 {
341 throw new IllegalArgumentException(
342 "MonLocHisGeom.remove - 'monLocHisGeomId' can not be null");
343 }
344 MonLocHisGeom entity = this.get(monLocHisGeomId);
345 if (entity != null)
346 {
347 this.remove(entity);
348 }
349 }
350
351
352
353
354 @Override
355 public void remove(Collection<MonLocHisGeom> entities)
356 {
357 if (entities == null)
358 {
359 throw new IllegalArgumentException(
360 "MonLocHisGeom.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 MonLocHisGeom entity)
379 {
380 Object target = null;
381 if (entity != null)
382 {
383 switch (transform)
384 {
385 case MonLocHisGeomDao.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 MonLocHisGeomDao.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 MonLocHisGeom)
434 {
435 result = input;
436 }
437 return result;
438 }
439 };
440
441
442
443
444
445 protected MonLocHisGeom toEntity(Object[] row)
446 {
447 MonLocHisGeom 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 MonLocHisGeom)
455 {
456 target = (MonLocHisGeom)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(), MonLocHisGeomImpl.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(MonLocHisGeomDao.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(), MonLocHisGeomImpl.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<MonLocHisGeom> search(final Search search)
533 {
534 return (Set<MonLocHisGeom>) this.search(MonLocHisGeomDao.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 }