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