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