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