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