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