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