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