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