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