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