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.quadrige2.core.dao.data.photo;
7   
8   /*-
9    * #%L
10   * Quadrige2 Core :: Server API
11   * %%
12   * Copyright (C) 2017 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  
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   * <p>
53   * Base Spring DAO Class: is able to create, update, remove, load, and find
54   * objects of type <code>Photo</code>.
55   * </p>
56   *
57   * @see Photo
58   */
59  public abstract class PhotoDaoBase
60      extends HibernateDaoSupport    
61      implements PhotoDao
62  {
63      /**
64       * {@inheritDoc}
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       * {@inheritDoc}
79       */
80      @Override
81      public Photo get(Integer photoId)
82      {
83          return (Photo)this.get(TRANSFORM_NONE, photoId);
84      }
85  
86      /**
87       * {@inheritDoc}
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      * {@inheritDoc}
103      */
104     @Override
105     public Photo load(Integer photoId)
106     {
107         return (Photo)this.load(TRANSFORM_NONE, photoId);
108     }
109 
110     /**
111      * {@inheritDoc}
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      * {@inheritDoc}
122      */
123     @Override
124     public Collection<?> loadAll(final int transform)
125     {
126         return this.loadAll(transform, -1, -1);
127     }
128 
129     /**
130      * {@inheritDoc}
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      * {@inheritDoc}
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      * firstResult = (pageNumber - 1) * pageSize
164      * @param pageNumber
165      * @param pageSize
166      * @return firstResult
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      * {@inheritDoc}
180      */
181     @Override
182     public Photo create(Photo photo)
183     {
184         return (Photo)this.create(PhotoDao.TRANSFORM_NONE, photo);
185     }
186 
187     /**
188      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@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     {
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * Allows transformation of entities into value objects
390      * (or something else for that matter), when the <code>transform</code>
391      * flag is set to one of the constants defined in <code>PhotoDao</code>, please note
392      * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
393      * will be returned.
394      *
395      * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
396      *
397      * @param transform one of the constants declared in {@link PhotoDao}
398      * @param entity an entity that was found
399      * @return the transformed entity (i.e. new value object, etc)
400      * @see PhotoDao#transformEntity(int, Photo)
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 : // fall-through
410                 default:
411                     target = entity;
412             }
413         }
414         return target;
415     }
416 
417     /**
418      * {@inheritDoc}
419      */
420     @Override
421     public void transformEntities(final int transform, final Collection<?> entities)
422     {
423         switch (transform)
424         {
425             case PhotoDao.TRANSFORM_NONE : // fall-through
426                 default:
427                 // do nothing;
428         }
429     }
430 
431     /**
432      * @see PhotoDao#toEntities(Collection)
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      * This anonymous transformer is designed to transform report query results
444      * (which result in an array of entities) to {@link Photo}
445      * using the Jakarta Commons-Collections Transformation API.
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      * @param row
467      * @return Photo
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      * Gets the current <code>principal</code> if one has been set,
490      * otherwise returns <code>null</code>.
491      *
492      * @return the current principal
493      */
494     protected Principal getPrincipal()
495     {
496         return PrincipalStore.get();
497     }
498 
499     /**
500      * {@inheritDoc}
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; /*super.convertHibernateAccessException(ex);*/
519         }
520     }
521 
522     /**
523      * {@inheritDoc}
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      * {@inheritDoc}
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; /*super.convertHibernateAccessException(ex);*/
548         }
549     }
550 
551     /**
552      * {@inheritDoc}
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      * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
563      * @param queryObject
564      * @param transform
565      * @param pageNumber
566      * @param pageSize
567      * @return PaginationResult
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             // Unchecked transformation because Set object is reused, cannot be strongly typed.
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; /*super.convertHibernateAccessException(ex);*/
593         }
594     }
595 
596     // spring-hibernate-dao-base merge-point
597 }