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