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