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