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