View Javadoc
1   // license-header java merge-point
2   //
3   // Attention: Generated code! Do not modify by hand!
4   // Generated by: hibernate/SpringHibernateDaoBase.vsl in <project>/mda/src/main/cartridge/custom/...
5   //
6   package fr.ifremer.quadrige3.core.dao.data.photo;
7   
8   /*-
9    * #%L
10   * Quadrige3 Core :: Client API
11   * %%
12   * Copyright (C) 2017 - 2024 Ifremer
13   * %%
14   * This program is free software: you can redistribute it and/or modify
15   * it under the terms of the GNU Affero General Public License as published by
16   * the Free Software Foundation, either version 3 of the License, or
17   * (at your option) any later version.
18   * 
19   * This program is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22   * GNU General Public License for more details.
23   * 
24   * You should have received a copy of the GNU Affero General Public License
25   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26   * #L%
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.administration.user.Department;
32  import fr.ifremer.quadrige3.core.dao.referential.ObjectType;
33  import fr.ifremer.quadrige3.core.dao.technical.hibernate.HibernateDaoSupport;
34  import java.security.Principal;
35  import java.sql.Timestamp;
36  import java.util.Collection;
37  import java.util.Date;
38  import java.util.LinkedHashSet;
39  import java.util.List;
40  import java.util.Set;
41  import javax.annotation.Resource;
42  import org.andromda.spring.PaginationResult;
43  import org.apache.commons.collections.CollectionUtils;
44  import org.apache.commons.collections.Transformer;
45  import org.hibernate.Criteria;
46  import org.hibernate.HibernateException;
47  import org.hibernate.Query;
48  import org.hibernate.ScrollableResults;
49  
50  /**
51   * <p>
52   * Base Spring DAO Class: is able to create, update, remove, load, and find
53   * objects of type <code>Photo</code>.
54   * </p>
55   *
56   * @see Photo
57   */
58  public abstract class PhotoDaoBase
59      extends HibernateDaoSupport    
60      implements PhotoDao
61  {
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public Object get(final int transform, final Integer photoId)
67      {
68          if (photoId == null)
69          {
70              throw new IllegalArgumentException(
71                  "Photo.get - 'photoId' can not be null");
72          }
73          final Photo entity = get(PhotoImpl.class, photoId);
74          return transformEntity(transform, entity);
75      }
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public Photo get(Integer photoId)
81      {
82          return (Photo)this.get(TRANSFORM_NONE, photoId);
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public Object load(final int transform, final Integer photoId)
90      {
91          if (photoId == null)
92          {
93              throw new IllegalArgumentException(
94                  "Photo.load - 'photoId' can not be null");
95          }
96          final Photo entity = get(PhotoImpl.class, photoId);
97          return transformEntity(transform, entity);
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public Photo load(Integer photoId)
105     {
106         return (Photo)this.load(TRANSFORM_NONE, photoId);
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     @SuppressWarnings({"unchecked"})
114     public Collection<Photo> loadAll()
115     {
116         return (Collection<Photo>) this.loadAll(PhotoDao.TRANSFORM_NONE);
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public Collection<?> loadAll(final int transform)
124     {
125         return this.loadAll(transform, -1, -1);
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public Collection<?> loadAll(final int pageNumber, final int pageSize)
133     {
134         return this.loadAll(PhotoDao.TRANSFORM_NONE, pageNumber, pageSize);
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
142     {
143         try
144         {
145             final Criteria criteria = this.getSession().createCriteria(PhotoImpl.class);
146             if (pageNumber > 0 && pageSize > 0)
147             {
148                 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
149                 criteria.setMaxResults(pageSize);
150             }
151             final Collection<?> results = criteria.list();
152             this.transformEntities(transform, results);
153             return results;
154         }
155         catch (HibernateException ex)
156         {
157             throw ex;
158         }
159     }
160 
161     /**
162      * firstResult = (pageNumber - 1) * pageSize
163      * @param pageNumber
164      * @param pageSize
165      * @return firstResult
166      */
167     protected int calculateFirstResult(int pageNumber, int pageSize)
168     {
169         int firstResult = 0;
170         if (pageNumber > 0)
171         {
172             firstResult = (pageNumber - 1) * pageSize;
173         }
174         return firstResult;
175     }
176 
177     /**
178      * {@inheritDoc}
179      */
180     @Override
181     public Photo create(Photo photo)
182     {
183         return (Photo)this.create(PhotoDao.TRANSFORM_NONE, photo);
184     }
185 
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     public Object create(final int transform, final Photo photo)
191     {
192         if (photo == null)
193         {
194             throw new IllegalArgumentException(
195                 "Photo.create - 'photo' can not be null");
196         }
197         this.getSessionFactory().getCurrentSession().save(photo);
198         return this.transformEntity(transform, photo);
199     }
200 
201     /**
202      * {@inheritDoc}
203      */
204     @Override
205     @SuppressWarnings({"unchecked"})
206     public Collection<Photo> create(final Collection<Photo> entities)
207     {
208         return (Collection<Photo>) create(PhotoDao.TRANSFORM_NONE, entities);
209     }
210 
211     /**
212      * {@inheritDoc}
213      */
214     @Override
215     public Collection<?> create(final int transform, final Collection<Photo> entities)
216     {
217         if (entities == null)
218         {
219             throw new IllegalArgumentException(
220                 "Photo.create - 'entities' can not be null");
221         }
222                     for (Photo entity : entities)
223                     {
224                         create(transform, entity);
225                     }
226         return entities;
227     }
228 
229     /**
230      * {@inheritDoc}
231      */
232     @Override
233     public Photo create(
234         Integer objectId,
235         String photoNm,
236         String photoDirDc,
237         String photoCm,
238         String photoLk,
239         Date photoDt,
240         Date photoValidDt,
241         Date photoQualifDt,
242         String photoQualifCm,
243         Timestamp updateDt,
244         Integer remoteId)
245     {
246         return (Photo)this.create(PhotoDao.TRANSFORM_NONE, objectId, photoNm, photoDirDc, photoCm, photoLk, photoDt, photoValidDt, photoQualifDt, photoQualifCm, updateDt, remoteId);
247     }
248 
249     /**
250      * {@inheritDoc}
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         Integer remoteId)
266     {
267         Photo entity = new PhotoImpl();
268         entity.setObjectId(objectId);
269         entity.setPhotoNm(photoNm);
270         entity.setPhotoDirDc(photoDirDc);
271         entity.setPhotoCm(photoCm);
272         entity.setPhotoLk(photoLk);
273         entity.setPhotoDt(photoDt);
274         entity.setPhotoValidDt(photoValidDt);
275         entity.setPhotoQualifDt(photoQualifDt);
276         entity.setPhotoQualifCm(photoQualifCm);
277         entity.setUpdateDt(updateDt);
278         entity.setRemoteId(remoteId);
279         return this.create(transform, entity);
280     }
281 
282     /**
283      * {@inheritDoc}
284      */
285     @Override
286     public Photo create(
287         Integer objectId,
288         ObjectType objectType,
289         Department recorderDepartment)
290     {
291         return (Photo)this.create(PhotoDao.TRANSFORM_NONE, objectId, objectType, recorderDepartment);
292     }
293 
294     /**
295      * {@inheritDoc}
296      */
297     @Override
298     public Object create(
299         final int transform,
300         Integer objectId,
301         ObjectType objectType,
302         Department recorderDepartment)
303     {
304         Photo entity = new PhotoImpl();
305         entity.setObjectId(objectId);
306         entity.setObjectType(objectType);
307         entity.setRecorderDepartment(recorderDepartment);
308         return this.create(transform, entity);
309     }
310 
311     /**
312      * {@inheritDoc}
313      */
314     @Override
315     public void update(Photo photo)
316     {
317         if (photo == null)
318         {
319             throw new IllegalArgumentException(
320                 "Photo.update - 'photo' can not be null");
321         }
322         this.getSessionFactory().getCurrentSession().update(photo);
323     }
324 
325     /**
326      * {@inheritDoc}
327      */
328     @Override
329     public void update(final Collection<Photo> entities)
330     {
331         if (entities == null)
332         {
333             throw new IllegalArgumentException(
334                 "Photo.update - 'entities' can not be null");
335         }
336                     for (Photo entity : entities)
337                     {
338                         update(entity);
339                     }
340     }
341 
342     /**
343      * {@inheritDoc}
344      */
345     @Override
346     public void remove(Photo photo)
347     {
348         if (photo == null)
349         {
350             throw new IllegalArgumentException(
351                 "Photo.remove - 'photo' can not be null");
352         }
353         this.getSessionFactory().getCurrentSession().delete(photo);
354     }
355 
356     /**
357      * {@inheritDoc}
358      */
359     @Override
360     public void remove(Integer photoId)
361     {
362         if (photoId == null)
363         {
364             throw new IllegalArgumentException(
365                 "Photo.remove - 'photoId' can not be null");
366         }
367         Photo entity = this.get(photoId);
368         if (entity != null)
369         {
370             this.remove(entity);
371         }
372     }
373 
374     /**
375      * {@inheritDoc}
376      */
377     @Override
378     public void remove(Collection<Photo> entities)
379     {
380         if (entities == null)
381         {
382             throw new IllegalArgumentException(
383                 "Photo.remove - 'entities' can not be null");
384         }
385         deleteAll(entities);
386     }
387     /**
388      * Allows transformation of entities into value objects
389      * (or something else for that matter), when the <code>transform</code>
390      * flag is set to one of the constants defined in <code>PhotoDao</code>, please note
391      * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
392      * will be returned.
393      *
394      * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
395      *
396      * @param transform one of the constants declared in {@link PhotoDao}
397      * @param entity an entity that was found
398      * @return the transformed entity (i.e. new value object, etc)
399      * @see PhotoDao#transformEntity(int, Photo)
400      */
401     public Object transformEntity(final int transform, final Photo entity)
402     {
403         Object target = null;
404         if (entity != null)
405         {
406             switch (transform)
407             {
408                 case PhotoDao.TRANSFORM_NONE : // fall-through
409                 default:
410                     target = entity;
411             }
412         }
413         return target;
414     }
415 
416     /**
417      * {@inheritDoc}
418      */
419     @Override
420     public void transformEntities(final int transform, final Collection<?> entities)
421     {
422         switch (transform)
423         {
424             case PhotoDao.TRANSFORM_NONE : // fall-through
425                 default:
426                 // do nothing;
427         }
428     }
429 
430     /**
431      * @see PhotoDao#toEntities(Collection)
432      */
433     public void toEntities(final Collection<?> results)
434     {
435         if (results != null)
436         {
437             CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
438         }
439     }
440 
441     /**
442      * This anonymous transformer is designed to transform report query results
443      * (which result in an array of entities) to {@link Photo}
444      * using the Jakarta Commons-Collections Transformation API.
445      */
446     private Transformer ENTITYTRANSFORMER =
447         new Transformer()
448         {
449             public Object transform(Object input)
450             {
451                 Object result = null;
452                 if (input instanceof Object[])
453                 {
454                     result = toEntity((Object[])input);
455                 }
456                 else if (input instanceof Photo)
457                 {
458                     result = input;
459                 }
460                 return result;
461             }
462         };
463 
464     /**
465      * @param row
466      * @return Photo
467      */
468     protected Photo toEntity(Object[] row)
469     {
470         Photo target = null;
471         if (row != null)
472         {
473             final int numberOfObjects = row.length;
474             for (int ctr = 0; ctr < numberOfObjects; ctr++)
475             {
476                 final Object object = row[ctr];
477                 if (object instanceof Photo)
478                 {
479                     target = (Photo)object;
480                     break;
481                 }
482             }
483         }
484         return target;
485     }
486 
487     /**
488      * Gets the current <code>principal</code> if one has been set,
489      * otherwise returns <code>null</code>.
490      *
491      * @return the current principal
492      */
493     protected Principal getPrincipal()
494     {
495         return PrincipalStore.get();
496     }
497 
498     /**
499      * {@inheritDoc}
500      */
501     @Override
502     @SuppressWarnings({ "unchecked" })
503     public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
504     {
505         try
506         {
507             search.setPageNumber(pageNumber);
508             search.setPageSize(pageSize);
509             final PropertySearch propertySearch = new PropertySearch(
510                 this.getSession(), PhotoImpl.class, search);
511             final List results = propertySearch.executeAsList();
512             this.transformEntities(transform, results);
513             return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
514         }
515         catch (HibernateException ex)
516         {
517             throw ex; /*super.convertHibernateAccessException(ex);*/
518         }
519     }
520 
521     /**
522      * {@inheritDoc}
523      */
524     @Override
525     public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
526     {
527         return this.search(PhotoDao.TRANSFORM_NONE, pageNumber, pageSize, search);
528     }
529 
530     /**
531      * {@inheritDoc}
532      */
533     @Override
534     public Set<?> search(final int transform, final Search search)
535     {
536         try
537         {
538             final PropertySearch propertySearch = new PropertySearch(
539                 this.getSession(), PhotoImpl.class, search);
540             final Set<?> results = propertySearch.executeAsSet();
541             this.transformEntities(transform, results);
542             return results;
543         }
544         catch (HibernateException ex)
545         {
546             throw ex; /*super.convertHibernateAccessException(ex);*/
547         }
548     }
549 
550     /**
551      * {@inheritDoc}
552      */
553     @Override
554     @SuppressWarnings("unchecked")
555     public Set<Photo> search(final Search search)
556     {
557         return (Set<Photo>) this.search(PhotoDao.TRANSFORM_NONE, search);
558     }
559 
560     /**
561      * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
562      * @param queryObject
563      * @param transform
564      * @param pageNumber
565      * @param pageSize
566      * @return PaginationResult
567      */
568     @SuppressWarnings({ "unchecked" })
569     protected PaginationResult getPaginationResult(
570         final Query queryObject,
571         final int transform, int pageNumber, int pageSize)
572     {
573         try
574         {
575             final ScrollableResults scrollableResults = queryObject.scroll();
576             scrollableResults.last();
577             int totalCount = scrollableResults.getRowNumber();
578             totalCount = totalCount >= 0 ? totalCount + 1 : 0;
579             if (pageNumber > 0 && pageSize > 0)
580             {
581                 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
582                 queryObject.setMaxResults(pageSize);
583             }
584             // Unchecked transformation because Set object is reused, cannot be strongly typed.
585             Set results = new LinkedHashSet(queryObject.list());
586             transformEntities(transform, results);
587             return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
588         }
589         catch (HibernateException ex)
590         {
591             throw ex; /*super.convertHibernateAccessException(ex);*/
592         }
593     }
594 
595     // spring-hibernate-dao-base merge-point
596 }