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