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.survey;
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.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   * <p>
51   * Base Spring DAO Class: is able to create, update, remove, load, and find
52   * objects of type <code>Video</code>.
53   * </p>
54   *
55   * @see Video
56   */
57  public abstract class VideoDaoBase
58      extends HibernateDaoSupport    
59      implements VideoDao
60  {
61      /**
62       * {@inheritDoc}
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       * {@inheritDoc}
77       */
78      @Override
79      public Video get(Integer videoId)
80      {
81          return (Video)this.get(TRANSFORM_NONE, videoId);
82      }
83  
84      /**
85       * {@inheritDoc}
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      * {@inheritDoc}
101      */
102     @Override
103     public Video load(Integer videoId)
104     {
105         return (Video)this.load(TRANSFORM_NONE, videoId);
106     }
107 
108     /**
109      * {@inheritDoc}
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      * {@inheritDoc}
120      */
121     @Override
122     public Collection<?> loadAll(final int transform)
123     {
124         return this.loadAll(transform, -1, -1);
125     }
126 
127     /**
128      * {@inheritDoc}
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      * {@inheritDoc}
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      * firstResult = (pageNumber - 1) * pageSize
162      * @param pageNumber
163      * @param pageSize
164      * @return firstResult
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      * {@inheritDoc}
178      */
179     @Override
180     public Video create(Video video)
181     {
182         return (Video)this.create(VideoDao.TRANSFORM_NONE, video);
183     }
184 
185     /**
186      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * Allows transformation of entities into value objects
347      * (or something else for that matter), when the <code>transform</code>
348      * flag is set to one of the constants defined in <code>VideoDao</code>, please note
349      * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
350      * will be returned.
351      *
352      * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
353      *
354      * @param transform one of the constants declared in {@link VideoDao}
355      * @param entity an entity that was found
356      * @return the transformed entity (i.e. new value object, etc)
357      * @see VideoDao#transformEntity(int, Video)
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 : // fall-through
367                 default:
368                     target = entity;
369             }
370         }
371         return target;
372     }
373 
374     /**
375      * {@inheritDoc}
376      */
377     @Override
378     public void transformEntities(final int transform, final Collection<?> entities)
379     {
380         switch (transform)
381         {
382             case VideoDao.TRANSFORM_NONE : // fall-through
383                 default:
384                 // do nothing;
385         }
386     }
387 
388     /**
389      * @see VideoDao#toEntities(Collection)
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      * This anonymous transformer is designed to transform report query results
401      * (which result in an array of entities) to {@link Video}
402      * using the Jakarta Commons-Collections Transformation API.
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      * @param row
424      * @return Video
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      * Gets the current <code>principal</code> if one has been set,
447      * otherwise returns <code>null</code>.
448      *
449      * @return the current principal
450      */
451     protected Principal getPrincipal()
452     {
453         return PrincipalStore.get();
454     }
455 
456     /**
457      * {@inheritDoc}
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; /*super.convertHibernateAccessException(ex);*/
476         }
477     }
478 
479     /**
480      * {@inheritDoc}
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      * {@inheritDoc}
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; /*super.convertHibernateAccessException(ex);*/
505         }
506     }
507 
508     /**
509      * {@inheritDoc}
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      * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
520      * @param queryObject
521      * @param transform
522      * @param pageNumber
523      * @param pageSize
524      * @return PaginationResult
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             // Unchecked transformation because Set object is reused, cannot be strongly typed.
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; /*super.convertHibernateAccessException(ex);*/
550         }
551     }
552 
553     // spring-hibernate-dao-base merge-point
554 }