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