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