1
2
3
4
5
6 package fr.ifremer.quadrige2.core.dao.data.photo;
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.administration.user.Department;
33 import fr.ifremer.quadrige2.core.dao.referential.ObjectType;
34 import fr.ifremer.quadrige2.core.dao.technical.hibernate.HibernateDaoSupport;
35 import java.security.Principal;
36 import java.sql.Timestamp;
37 import java.util.Collection;
38 import java.util.Date;
39 import java.util.LinkedHashSet;
40 import java.util.List;
41 import java.util.Set;
42 import javax.annotation.Resource;
43 import org.andromda.spring.PaginationResult;
44 import org.apache.commons.collections.CollectionUtils;
45 import org.apache.commons.collections.Transformer;
46 import org.hibernate.Criteria;
47 import org.hibernate.HibernateException;
48 import org.hibernate.Query;
49 import org.hibernate.ScrollableResults;
50
51
52
53
54
55
56
57
58
59 public abstract class PhotoDaoBase
60 extends HibernateDaoSupport
61 implements PhotoDao
62 {
63
64
65
66 @Override
67 public Object get(final int transform, final Integer photoId)
68 {
69 if (photoId == null)
70 {
71 throw new IllegalArgumentException(
72 "Photo.get - 'photoId' can not be null");
73 }
74 final Photo entity = get(PhotoImpl.class, photoId);
75 return transformEntity(transform, entity);
76 }
77
78
79
80 @Override
81 public Photo get(Integer photoId)
82 {
83 return (Photo)this.get(TRANSFORM_NONE, photoId);
84 }
85
86
87
88
89 @Override
90 public Object load(final int transform, final Integer photoId)
91 {
92 if (photoId == null)
93 {
94 throw new IllegalArgumentException(
95 "Photo.load - 'photoId' can not be null");
96 }
97 final Photo entity = get(PhotoImpl.class, photoId);
98 return transformEntity(transform, entity);
99 }
100
101
102
103
104 @Override
105 public Photo load(Integer photoId)
106 {
107 return (Photo)this.load(TRANSFORM_NONE, photoId);
108 }
109
110
111
112
113 @Override
114 @SuppressWarnings({"unchecked"})
115 public Collection<Photo> loadAll()
116 {
117 return (Collection<Photo>) this.loadAll(PhotoDao.TRANSFORM_NONE);
118 }
119
120
121
122
123 @Override
124 public Collection<?> loadAll(final int transform)
125 {
126 return this.loadAll(transform, -1, -1);
127 }
128
129
130
131
132 @Override
133 public Collection<?> loadAll(final int pageNumber, final int pageSize)
134 {
135 return this.loadAll(PhotoDao.TRANSFORM_NONE, pageNumber, pageSize);
136 }
137
138
139
140
141 @Override
142 public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
143 {
144 try
145 {
146 final Criteria criteria = this.getSession().createCriteria(PhotoImpl.class);
147 if (pageNumber > 0 && pageSize > 0)
148 {
149 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
150 criteria.setMaxResults(pageSize);
151 }
152 final Collection<?> results = criteria.list();
153 this.transformEntities(transform, results);
154 return results;
155 }
156 catch (HibernateException ex)
157 {
158 throw ex;
159 }
160 }
161
162
163
164
165
166
167
168 protected int calculateFirstResult(int pageNumber, int pageSize)
169 {
170 int firstResult = 0;
171 if (pageNumber > 0)
172 {
173 firstResult = (pageNumber - 1) * pageSize;
174 }
175 return firstResult;
176 }
177
178
179
180
181 @Override
182 public Photo create(Photo photo)
183 {
184 return (Photo)this.create(PhotoDao.TRANSFORM_NONE, photo);
185 }
186
187
188
189
190 @Override
191 public Object create(final int transform, final Photo photo)
192 {
193 if (photo == null)
194 {
195 throw new IllegalArgumentException(
196 "Photo.create - 'photo' can not be null");
197 }
198 this.getSessionFactory().getCurrentSession().save(photo);
199 return this.transformEntity(transform, photo);
200 }
201
202
203
204
205 @Override
206 @SuppressWarnings({"unchecked"})
207 public Collection<Photo> create(final Collection<Photo> entities)
208 {
209 return (Collection<Photo>) create(PhotoDao.TRANSFORM_NONE, entities);
210 }
211
212
213
214
215 @Override
216 public Collection<?> create(final int transform, final Collection<Photo> entities)
217 {
218 if (entities == null)
219 {
220 throw new IllegalArgumentException(
221 "Photo.create - 'entities' can not be null");
222 }
223 for (Photo entity : entities)
224 {
225 create(transform, entity);
226 }
227 return entities;
228 }
229
230
231
232
233 @Override
234 public Photo create(
235 Integer objectId,
236 String photoNm,
237 String photoDirDc,
238 String photoCm,
239 String photoLk,
240 Date photoDt,
241 Date photoValidDt,
242 Date photoQualifDt,
243 String photoQualifCm,
244 Timestamp updateDt)
245 {
246 return (Photo)this.create(PhotoDao.TRANSFORM_NONE, objectId, photoNm, photoDirDc, photoCm, photoLk, photoDt, photoValidDt, photoQualifDt, photoQualifCm, updateDt);
247 }
248
249
250
251
252 @Override
253 public Object create(
254 final int transform,
255 Integer objectId,
256 String photoNm,
257 String photoDirDc,
258 String photoCm,
259 String photoLk,
260 Date photoDt,
261 Date photoValidDt,
262 Date photoQualifDt,
263 String photoQualifCm,
264 Timestamp updateDt)
265 {
266 Photo entity = new PhotoImpl();
267 entity.setObjectId(objectId);
268 entity.setPhotoNm(photoNm);
269 entity.setPhotoDirDc(photoDirDc);
270 entity.setPhotoCm(photoCm);
271 entity.setPhotoLk(photoLk);
272 entity.setPhotoDt(photoDt);
273 entity.setPhotoValidDt(photoValidDt);
274 entity.setPhotoQualifDt(photoQualifDt);
275 entity.setPhotoQualifCm(photoQualifCm);
276 entity.setUpdateDt(updateDt);
277 return this.create(transform, entity);
278 }
279
280
281
282
283 @Override
284 public Photo create(
285 Integer objectId,
286 Timestamp updateDt,
287 ObjectType objectType,
288 Department recorderDepartment)
289 {
290 return (Photo)this.create(PhotoDao.TRANSFORM_NONE, objectId, updateDt, objectType, recorderDepartment);
291 }
292
293
294
295
296 @Override
297 public Object create(
298 final int transform,
299 Integer objectId,
300 Timestamp updateDt,
301 ObjectType objectType,
302 Department recorderDepartment)
303 {
304 Photo entity = new PhotoImpl();
305 entity.setObjectId(objectId);
306 entity.setUpdateDt(updateDt);
307 entity.setObjectType(objectType);
308 entity.setRecorderDepartment(recorderDepartment);
309 return this.create(transform, entity);
310 }
311
312
313
314
315 @Override
316 public void update(Photo photo)
317 {
318 if (photo == null)
319 {
320 throw new IllegalArgumentException(
321 "Photo.update - 'photo' can not be null");
322 }
323 this.getSessionFactory().getCurrentSession().update(photo);
324 }
325
326
327
328
329 @Override
330 public void update(final Collection<Photo> entities)
331 {
332 if (entities == null)
333 {
334 throw new IllegalArgumentException(
335 "Photo.update - 'entities' can not be null");
336 }
337 for (Photo entity : entities)
338 {
339 update(entity);
340 }
341 }
342
343
344
345
346 @Override
347 public void remove(Photo photo)
348 {
349 if (photo == null)
350 {
351 throw new IllegalArgumentException(
352 "Photo.remove - 'photo' can not be null");
353 }
354 this.getSessionFactory().getCurrentSession().delete(photo);
355 }
356
357
358
359
360 @Override
361 public void remove(Integer photoId)
362 {
363 if (photoId == null)
364 {
365 throw new IllegalArgumentException(
366 "Photo.remove - 'photoId' can not be null");
367 }
368 Photo entity = this.get(photoId);
369 if (entity != null)
370 {
371 this.remove(entity);
372 }
373 }
374
375
376
377
378 @Override
379 public void remove(Collection<Photo> entities)
380 {
381 if (entities == null)
382 {
383 throw new IllegalArgumentException(
384 "Photo.remove - 'entities' can not be null");
385 }
386 deleteAll(entities);
387 }
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402 public Object transformEntity(final int transform, final Photo entity)
403 {
404 Object target = null;
405 if (entity != null)
406 {
407 switch (transform)
408 {
409 case PhotoDao.TRANSFORM_NONE :
410 default:
411 target = entity;
412 }
413 }
414 return target;
415 }
416
417
418
419
420 @Override
421 public void transformEntities(final int transform, final Collection<?> entities)
422 {
423 switch (transform)
424 {
425 case PhotoDao.TRANSFORM_NONE :
426 default:
427
428 }
429 }
430
431
432
433
434 public void toEntities(final Collection<?> results)
435 {
436 if (results != null)
437 {
438 CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
439 }
440 }
441
442
443
444
445
446
447 private Transformer ENTITYTRANSFORMER =
448 new Transformer()
449 {
450 public Object transform(Object input)
451 {
452 Object result = null;
453 if (input instanceof Object[])
454 {
455 result = toEntity((Object[])input);
456 }
457 else if (input instanceof Photo)
458 {
459 result = input;
460 }
461 return result;
462 }
463 };
464
465
466
467
468
469 protected Photo toEntity(Object[] row)
470 {
471 Photo target = null;
472 if (row != null)
473 {
474 final int numberOfObjects = row.length;
475 for (int ctr = 0; ctr < numberOfObjects; ctr++)
476 {
477 final Object object = row[ctr];
478 if (object instanceof Photo)
479 {
480 target = (Photo)object;
481 break;
482 }
483 }
484 }
485 return target;
486 }
487
488
489
490
491
492
493
494 protected Principal getPrincipal()
495 {
496 return PrincipalStore.get();
497 }
498
499
500
501
502 @Override
503 @SuppressWarnings({ "unchecked" })
504 public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
505 {
506 try
507 {
508 search.setPageNumber(pageNumber);
509 search.setPageSize(pageSize);
510 final PropertySearch propertySearch = new PropertySearch(
511 this.getSession(), PhotoImpl.class, search);
512 final List results = propertySearch.executeAsList();
513 this.transformEntities(transform, results);
514 return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
515 }
516 catch (HibernateException ex)
517 {
518 throw ex;
519 }
520 }
521
522
523
524
525 @Override
526 public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
527 {
528 return this.search(PhotoDao.TRANSFORM_NONE, pageNumber, pageSize, search);
529 }
530
531
532
533
534 @Override
535 public Set<?> search(final int transform, final Search search)
536 {
537 try
538 {
539 final PropertySearch propertySearch = new PropertySearch(
540 this.getSession(), PhotoImpl.class, search);
541 final Set<?> results = propertySearch.executeAsSet();
542 this.transformEntities(transform, results);
543 return results;
544 }
545 catch (HibernateException ex)
546 {
547 throw ex;
548 }
549 }
550
551
552
553
554 @Override
555 @SuppressWarnings("unchecked")
556 public Set<Photo> search(final Search search)
557 {
558 return (Set<Photo>) this.search(PhotoDao.TRANSFORM_NONE, search);
559 }
560
561
562
563
564
565
566
567
568
569 @SuppressWarnings({ "unchecked" })
570 protected PaginationResult getPaginationResult(
571 final Query queryObject,
572 final int transform, int pageNumber, int pageSize)
573 {
574 try
575 {
576 final ScrollableResults scrollableResults = queryObject.scroll();
577 scrollableResults.last();
578 int totalCount = scrollableResults.getRowNumber();
579 totalCount = totalCount >= 0 ? totalCount + 1 : 0;
580 if (pageNumber > 0 && pageSize > 0)
581 {
582 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
583 queryObject.setMaxResults(pageSize);
584 }
585
586 Set results = new LinkedHashSet(queryObject.list());
587 transformEntities(transform, results);
588 return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
589 }
590 catch (HibernateException ex)
591 {
592 throw ex;
593 }
594 }
595
596
597 }