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.administration.strategy;
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 fr.ifremer.quadrige2.core.vo.administration.strategy.AppliedPeriodVO;
34  import java.security.Principal;
35  import java.sql.Timestamp;
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Date;
39  import java.util.Iterator;
40  import java.util.LinkedHashSet;
41  import java.util.List;
42  import java.util.Set;
43  import javax.annotation.Resource;
44  import org.andromda.spring.PaginationResult;
45  import org.apache.commons.collections.CollectionUtils;
46  import org.apache.commons.collections.Transformer;
47  import org.hibernate.Criteria;
48  import org.hibernate.HibernateException;
49  import org.hibernate.Query;
50  import org.hibernate.ScrollableResults;
51  
52  /**
53   * <p>
54   * Base Spring DAO Class: is able to create, update, remove, load, and find
55   * objects of type <code>AppliedPeriod</code>.
56   * </p>
57   *
58   * @see AppliedPeriod
59   */
60  public abstract class AppliedPeriodDaoBase
61      extends HibernateDaoSupport    
62      implements AppliedPeriodDao
63  {
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public Object get(final int transform, final AppliedPeriodPK appliedPeriodPk)
69      {
70          if (appliedPeriodPk == null)
71          {
72              throw new IllegalArgumentException(
73                  "AppliedPeriod.get - 'appliedPeriodPk' can not be null");
74          }
75          final AppliedPeriod entity = get(AppliedPeriodImpl.class, appliedPeriodPk);
76          return transformEntity(transform, entity);
77      }
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public AppliedPeriod get(AppliedPeriodPK appliedPeriodPk)
83      {
84          return (AppliedPeriod)this.get(TRANSFORM_NONE, appliedPeriodPk);
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public Object load(final int transform, final AppliedPeriodPK appliedPeriodPk)
92      {
93          if (appliedPeriodPk == null)
94          {
95              throw new IllegalArgumentException(
96                  "AppliedPeriod.load - 'appliedPeriodPk' can not be null");
97          }
98          final AppliedPeriod entity = get(AppliedPeriodImpl.class, appliedPeriodPk);
99          return transformEntity(transform, entity);
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public AppliedPeriod load(AppliedPeriodPK appliedPeriodPk)
107     {
108         return (AppliedPeriod)this.load(TRANSFORM_NONE, appliedPeriodPk);
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     @SuppressWarnings({"unchecked"})
116     public Collection<AppliedPeriod> loadAll()
117     {
118         return (Collection<AppliedPeriod>) this.loadAll(AppliedPeriodDao.TRANSFORM_NONE);
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public Collection<?> loadAll(final int transform)
126     {
127         return this.loadAll(transform, -1, -1);
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public Collection<?> loadAll(final int pageNumber, final int pageSize)
135     {
136         return this.loadAll(AppliedPeriodDao.TRANSFORM_NONE, pageNumber, pageSize);
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
144     {
145         try
146         {
147             final Criteria criteria = this.getSession().createCriteria(AppliedPeriodImpl.class);
148             if (pageNumber > 0 && pageSize > 0)
149             {
150                 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
151                 criteria.setMaxResults(pageSize);
152             }
153             final Collection<?> results = criteria.list();
154             this.transformEntities(transform, results);
155             return results;
156         }
157         catch (HibernateException ex)
158         {
159             throw ex;
160         }
161     }
162 
163     /**
164      * firstResult = (pageNumber - 1) * pageSize
165      * @param pageNumber
166      * @param pageSize
167      * @return firstResult
168      */
169     protected int calculateFirstResult(int pageNumber, int pageSize)
170     {
171         int firstResult = 0;
172         if (pageNumber > 0)
173         {
174             firstResult = (pageNumber - 1) * pageSize;
175         }
176         return firstResult;
177     }
178 
179     /**
180      * {@inheritDoc}
181      */
182     @Override
183     public AppliedPeriod create(AppliedPeriod appliedPeriod)
184     {
185         return (AppliedPeriod)this.create(AppliedPeriodDao.TRANSFORM_NONE, appliedPeriod);
186     }
187 
188     /**
189      * {@inheritDoc}
190      */
191     @Override
192     public Object create(final int transform, final AppliedPeriod appliedPeriod)
193     {
194         if (appliedPeriod == null)
195         {
196             throw new IllegalArgumentException(
197                 "AppliedPeriod.create - 'appliedPeriod' can not be null");
198         }
199         this.getSessionFactory().getCurrentSession().save(appliedPeriod);
200         return this.transformEntity(transform, appliedPeriod);
201     }
202 
203     /**
204      * {@inheritDoc}
205      */
206     @Override
207     @SuppressWarnings({"unchecked"})
208     public Collection<AppliedPeriod> create(final Collection<AppliedPeriod> entities)
209     {
210         return (Collection<AppliedPeriod>) create(AppliedPeriodDao.TRANSFORM_NONE, entities);
211     }
212 
213     /**
214      * {@inheritDoc}
215      */
216     @Override
217     public Collection<?> create(final int transform, final Collection<AppliedPeriod> entities)
218     {
219         if (entities == null)
220         {
221             throw new IllegalArgumentException(
222                 "AppliedPeriod.create - 'entities' can not be null");
223         }
224                     for (AppliedPeriod entity : entities)
225                     {
226                         create(transform, entity);
227                     }
228         return entities;
229     }
230 
231     /**
232      * {@inheritDoc}
233      */
234     @Override
235     public AppliedPeriod create(
236         Date appliedPeriodEndDt,
237         Timestamp updateDt)
238     {
239         return (AppliedPeriod)this.create(AppliedPeriodDao.TRANSFORM_NONE, appliedPeriodEndDt, updateDt);
240     }
241 
242     /**
243      * {@inheritDoc}
244      */
245     @Override
246     public Object create(
247         final int transform,
248         Date appliedPeriodEndDt,
249         Timestamp updateDt)
250     {
251         AppliedPeriod entity = new AppliedPeriodImpl();
252         entity.setAppliedPeriodEndDt(appliedPeriodEndDt);
253         entity.setUpdateDt(updateDt);
254         return this.create(transform, entity);
255     }
256 
257     /**
258      * {@inheritDoc}
259      */
260     @Override
261     public AppliedPeriod create(
262         Date appliedPeriodEndDt,
263         Timestamp updateDt,
264         AppliedStrategy appliedStrategy)
265     {
266         return (AppliedPeriod)this.create(AppliedPeriodDao.TRANSFORM_NONE, appliedPeriodEndDt, updateDt, appliedStrategy);
267     }
268 
269     /**
270      * {@inheritDoc}
271      */
272     @Override
273     public Object create(
274         final int transform,
275         Date appliedPeriodEndDt,
276         Timestamp updateDt,
277         AppliedStrategy appliedStrategy)
278     {
279         AppliedPeriod entity = new AppliedPeriodImpl();
280         entity.setAppliedPeriodEndDt(appliedPeriodEndDt);
281         entity.setUpdateDt(updateDt);
282         entity.setAppliedStrategy(appliedStrategy);
283         return this.create(transform, entity);
284     }
285 
286     /**
287      * {@inheritDoc}
288      */
289     @Override
290     public void update(AppliedPeriod appliedPeriod)
291     {
292         if (appliedPeriod == null)
293         {
294             throw new IllegalArgumentException(
295                 "AppliedPeriod.update - 'appliedPeriod' can not be null");
296         }
297         this.getSessionFactory().getCurrentSession().update(appliedPeriod);
298     }
299 
300     /**
301      * {@inheritDoc}
302      */
303     @Override
304     public void update(final Collection<AppliedPeriod> entities)
305     {
306         if (entities == null)
307         {
308             throw new IllegalArgumentException(
309                 "AppliedPeriod.update - 'entities' can not be null");
310         }
311                     for (AppliedPeriod entity : entities)
312                     {
313                         update(entity);
314                     }
315     }
316 
317     /**
318      * {@inheritDoc}
319      */
320     @Override
321     public void remove(AppliedPeriod appliedPeriod)
322     {
323         if (appliedPeriod == null)
324         {
325             throw new IllegalArgumentException(
326                 "AppliedPeriod.remove - 'appliedPeriod' can not be null");
327         }
328         this.getSessionFactory().getCurrentSession().delete(appliedPeriod);
329     }
330 
331     /**
332      * {@inheritDoc}
333      */
334     @Override
335     public void remove(AppliedPeriodPK appliedPeriodPk)
336     {
337         if (appliedPeriodPk == null)
338         {
339             throw new IllegalArgumentException(
340                 "AppliedPeriod.remove - 'appliedPeriodPk' can not be null");
341         }
342         AppliedPeriod entity = this.get(appliedPeriodPk);
343         if (entity != null)
344         {
345             this.remove(entity);
346         }
347     }
348 
349     /**
350      * {@inheritDoc}
351      */
352     @Override
353     public void remove(Collection<AppliedPeriod> entities)
354     {
355         if (entities == null)
356         {
357             throw new IllegalArgumentException(
358                 "AppliedPeriod.remove - 'entities' can not be null");
359         }
360         deleteAll(entities);
361     }
362     /**
363      * Allows transformation of entities into value objects
364      * (or something else for that matter), when the <code>transform</code>
365      * flag is set to one of the constants defined in <code>AppliedPeriodDao</code>, please note
366      * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
367      * will be returned.
368      * <p>
369      * This method will return instances of these types:
370      * <ul>
371      *   <li>{@link AppliedPeriod} - {@link #TRANSFORM_NONE}</li>
372      *   <li>{@link AppliedPeriodVO} - {@link #TRANSFORM_APPLIEDPERIODVO}</li>
373      * </ul>
374      *
375      * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
376      *
377      * @param transform one of the constants declared in {@link AppliedPeriodDao}
378      * @param entity an entity that was found
379      * @return the transformed entity (i.e. new value object, etc)
380      * @see AppliedPeriodDao#transformEntity(int, AppliedPeriod)
381      */
382     public Object transformEntity(final int transform, final AppliedPeriod entity)
383     {
384         Object target = null;
385         if (entity != null)
386         {
387             switch (transform)
388             {
389                 case TRANSFORM_APPLIEDPERIODVO :
390                     target = toAppliedPeriodVO(entity);
391                     break;
392                 case AppliedPeriodDao.TRANSFORM_NONE : // fall-through
393                 default:
394                     target = entity;
395             }
396         }
397         return target;
398     }
399 
400     /**
401      * {@inheritDoc}
402      */
403     @Override
404     public void transformEntities(final int transform, final Collection<?> entities)
405     {
406         switch (transform)
407         {
408             case TRANSFORM_APPLIEDPERIODVO :
409                 toAppliedPeriodVOCollection(entities);
410                 break;
411             case AppliedPeriodDao.TRANSFORM_NONE : // fall-through
412                 default:
413                 // do nothing;
414         }
415     }
416 
417     /**
418      * @see AppliedPeriodDao#toEntities(Collection)
419      */
420     public void toEntities(final Collection<?> results)
421     {
422         if (results != null)
423         {
424             CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
425         }
426     }
427 
428     /**
429      * This anonymous transformer is designed to transform report query results
430      * (which result in an array of entities) to {@link AppliedPeriod}
431      * using the Jakarta Commons-Collections Transformation API.
432      */
433     private Transformer ENTITYTRANSFORMER =
434         new Transformer()
435         {
436             public Object transform(Object input)
437             {
438                 Object result = null;
439                 if (input instanceof Object[])
440                 {
441                     result = toEntity((Object[])input);
442                 }
443                 else if (input instanceof AppliedPeriod)
444                 {
445                     result = input;
446                 }
447                 return result;
448             }
449         };
450 
451     /**
452      * @param row
453      * @return AppliedPeriod
454      */
455     protected AppliedPeriod toEntity(Object[] row)
456     {
457         AppliedPeriod target = null;
458         if (row != null)
459         {
460             final int numberOfObjects = row.length;
461             for (int ctr = 0; ctr < numberOfObjects; ctr++)
462             {
463                 final Object object = row[ctr];
464                 if (object instanceof AppliedPeriod)
465                 {
466                     target = (AppliedPeriod)object;
467                     break;
468                 }
469             }
470         }
471         return target;
472     }
473 
474     /**
475      * {@inheritDoc}
476      */
477     @Override
478     @SuppressWarnings({"unchecked"})
479     public final Collection<AppliedPeriodVO> toAppliedPeriodVOCollection(Collection<?> entities)
480     {
481         Collection<AppliedPeriodVO> result = new ArrayList<AppliedPeriodVO>();
482         if (entities != null)
483         {
484             CollectionUtils.transform(entities, this.APPLIEDPERIODVO_TRANSFORMER);
485             result.addAll((Collection<? extends AppliedPeriodVO>) entities);
486         }
487         return result;
488     }
489 
490     /**
491      * {@inheritDoc}
492      */
493     @Override
494     @SuppressWarnings({ "unchecked" })
495     public final AppliedPeriodVO[] toAppliedPeriodVOArray(Collection<?> entities)
496     {
497         AppliedPeriodVO[] result = null;
498         if (entities != null)
499         {
500             // Unchecked transformation: reuses entities Collection
501             final Collection collection = new ArrayList(entities);
502             this.toAppliedPeriodVOCollection(collection);
503             result = (AppliedPeriodVO[]) collection.toArray(new AppliedPeriodVO[collection.size()]);
504         }
505         return result;
506     }
507 
508     /**
509      * Default implementation for transforming the results of a report query into a value object. This
510      * implementation exists for convenience reasons only. It needs only be overridden in the
511      * {@link AppliedPeriodDaoImpl} class if you intend to use reporting queries.
512      * @param row
513      * @return toAppliedPeriodVO(this.toEntity(row))
514      * @see AppliedPeriodDao#toAppliedPeriodVO(AppliedPeriod)
515      */
516     protected AppliedPeriodVO toAppliedPeriodVO(Object[] row)
517     {
518         return this.toAppliedPeriodVO(this.toEntity(row));
519     }
520 
521     /**
522      * This anonymous transformer is designed to transform entities or report query results
523      * (which result in an array of objects) to {@link AppliedPeriodVO}
524      * using the Jakarta Commons-Collections Transformation API.
525      */
526     private Transformer APPLIEDPERIODVO_TRANSFORMER =
527         new Transformer()
528         {
529             public Object transform(Object input)
530             {
531                 Object result = null;
532                 if (input instanceof AppliedPeriod)
533                 {
534                     result = toAppliedPeriodVO((AppliedPeriod)input);
535                 }
536                 else if (input instanceof Object[])
537                 {
538                     result = toAppliedPeriodVO((Object[])input);
539                 }
540                 return result;
541             }
542         };
543 
544     /**
545      * {@inheritDoc}
546      */
547     @Override
548     public final void appliedPeriodVOToEntityCollection(Collection<?> instances)
549     {
550         if (instances != null)
551         {
552             for (final Iterator<?> iterator = instances.iterator(); iterator.hasNext();)
553             {
554                 // - remove an objects that are null or not of the correct instance
555                 if (!(iterator.next() instanceof AppliedPeriodVO))
556                 {
557                     iterator.remove();
558                 }
559             }
560             CollectionUtils.transform(instances, this.AppliedPeriodVOToEntityTransformer);
561         }
562     }
563 
564     private final Transformer AppliedPeriodVOToEntityTransformer =
565         new Transformer()
566         {
567             public Object transform(Object input)
568             {
569                 return appliedPeriodVOToEntity((AppliedPeriodVO)input);
570             }
571         };
572 
573 
574     /**
575      * {@inheritDoc}
576      */
577     @Override
578     public void toAppliedPeriodVO(
579         AppliedPeriod source,
580         AppliedPeriodVO target)
581     {
582         target.setAppliedPeriodEndDt(source.getAppliedPeriodEndDt());
583         target.setUpdateDt(source.getUpdateDt());
584     }
585 
586     /**
587      * {@inheritDoc}
588      */
589     @Override
590     public AppliedPeriodVO toAppliedPeriodVO(final AppliedPeriod entity)
591     {
592         AppliedPeriodVO target = null;
593         if (entity != null)
594         {
595             target =  new AppliedPeriodVO();
596             this.toAppliedPeriodVO(entity, target);
597         }
598         return target;
599     }
600 
601     /**
602      * {@inheritDoc}
603      */
604     @Override
605     public void appliedPeriodVOToEntity(
606         AppliedPeriodVO source,
607         AppliedPeriod target,
608         boolean copyIfNull)
609     {
610         if (copyIfNull || source.getAppliedPeriodEndDt() != null)
611         {
612             target.setAppliedPeriodEndDt(source.getAppliedPeriodEndDt());
613         }
614         if (copyIfNull || source.getUpdateDt() != null)
615         {
616             target.setUpdateDt(source.getUpdateDt());
617         }
618     }
619 
620     /**
621      * Gets the current <code>principal</code> if one has been set,
622      * otherwise returns <code>null</code>.
623      *
624      * @return the current principal
625      */
626     protected Principal getPrincipal()
627     {
628         return PrincipalStore.get();
629     }
630 
631     /**
632      * {@inheritDoc}
633      */
634     @Override
635     @SuppressWarnings({ "unchecked" })
636     public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
637     {
638         try
639         {
640             search.setPageNumber(pageNumber);
641             search.setPageSize(pageSize);
642             final PropertySearch propertySearch = new PropertySearch(
643                 this.getSession(), AppliedPeriodImpl.class, search);
644             final List results = propertySearch.executeAsList();
645             this.transformEntities(transform, results);
646             return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
647         }
648         catch (HibernateException ex)
649         {
650             throw ex; /*super.convertHibernateAccessException(ex);*/
651         }
652     }
653 
654     /**
655      * {@inheritDoc}
656      */
657     @Override
658     public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
659     {
660         return this.search(AppliedPeriodDao.TRANSFORM_NONE, pageNumber, pageSize, search);
661     }
662 
663     /**
664      * {@inheritDoc}
665      */
666     @Override
667     public Set<?> search(final int transform, final Search search)
668     {
669         try
670         {
671             final PropertySearch propertySearch = new PropertySearch(
672                 this.getSession(), AppliedPeriodImpl.class, search);
673             final Set<?> results = propertySearch.executeAsSet();
674             this.transformEntities(transform, results);
675             return results;
676         }
677         catch (HibernateException ex)
678         {
679             throw ex; /*super.convertHibernateAccessException(ex);*/
680         }
681     }
682 
683     /**
684      * {@inheritDoc}
685      */
686     @Override
687     @SuppressWarnings("unchecked")
688     public Set<AppliedPeriod> search(final Search search)
689     {
690         return (Set<AppliedPeriod>) this.search(AppliedPeriodDao.TRANSFORM_NONE, search);
691     }
692 
693     /**
694      * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
695      * @param queryObject
696      * @param transform
697      * @param pageNumber
698      * @param pageSize
699      * @return PaginationResult
700      */
701     @SuppressWarnings({ "unchecked" })
702     protected PaginationResult getPaginationResult(
703         final Query queryObject,
704         final int transform, int pageNumber, int pageSize)
705     {
706         try
707         {
708             final ScrollableResults scrollableResults = queryObject.scroll();
709             scrollableResults.last();
710             int totalCount = scrollableResults.getRowNumber();
711             totalCount = totalCount >= 0 ? totalCount + 1 : 0;
712             if (pageNumber > 0 && pageSize > 0)
713             {
714                 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
715                 queryObject.setMaxResults(pageSize);
716             }
717             // Unchecked transformation because Set object is reused, cannot be strongly typed.
718             Set results = new LinkedHashSet(queryObject.list());
719             transformEntities(transform, results);
720             return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
721         }
722         catch (HibernateException ex)
723         {
724             throw ex; /*super.convertHibernateAccessException(ex);*/
725         }
726     }
727 
728     // spring-hibernate-dao-base merge-point
729 }