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