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