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