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