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 java.security.Principal;
34  import java.sql.Timestamp;
35  import java.util.Collection;
36  import java.util.LinkedHashSet;
37  import java.util.List;
38  import java.util.Set;
39  import javax.annotation.Resource;
40  import org.andromda.spring.PaginationResult;
41  import org.apache.commons.collections.CollectionUtils;
42  import org.apache.commons.collections.Transformer;
43  import org.hibernate.Criteria;
44  import org.hibernate.HibernateException;
45  import org.hibernate.Query;
46  import org.hibernate.ScrollableResults;
47  
48  /**
49   * <p>
50   * Base Spring DAO Class: is able to create, update, remove, load, and find
51   * objects of type <code>RulePrecondition</code>.
52   * </p>
53   *
54   * @see RulePrecondition
55   */
56  public abstract class RulePreconditionDaoBase
57      extends HibernateDaoSupport    
58      implements RulePreconditionDao
59  {
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public Object get(final int transform, final Integer rulePrecondId)
65      {
66          if (rulePrecondId == null)
67          {
68              throw new IllegalArgumentException(
69                  "RulePrecondition.get - 'rulePrecondId' can not be null");
70          }
71          final RulePrecondition entity = get(RulePreconditionImpl.class, rulePrecondId);
72          return transformEntity(transform, entity);
73      }
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public RulePrecondition get(Integer rulePrecondId)
79      {
80          return (RulePrecondition)this.get(TRANSFORM_NONE, rulePrecondId);
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public Object load(final int transform, final Integer rulePrecondId)
88      {
89          if (rulePrecondId == null)
90          {
91              throw new IllegalArgumentException(
92                  "RulePrecondition.load - 'rulePrecondId' can not be null");
93          }
94          final RulePrecondition entity = get(RulePreconditionImpl.class, rulePrecondId);
95          return transformEntity(transform, entity);
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public RulePrecondition load(Integer rulePrecondId)
103     {
104         return (RulePrecondition)this.load(TRANSFORM_NONE, rulePrecondId);
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     @SuppressWarnings({"unchecked"})
112     public Collection<RulePrecondition> loadAll()
113     {
114         return (Collection<RulePrecondition>) this.loadAll(RulePreconditionDao.TRANSFORM_NONE);
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public Collection<?> loadAll(final int transform)
122     {
123         return this.loadAll(transform, -1, -1);
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public Collection<?> loadAll(final int pageNumber, final int pageSize)
131     {
132         return this.loadAll(RulePreconditionDao.TRANSFORM_NONE, pageNumber, pageSize);
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
140     {
141         try
142         {
143             final Criteria criteria = this.getSession().createCriteria(RulePreconditionImpl.class);
144             if (pageNumber > 0 && pageSize > 0)
145             {
146                 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
147                 criteria.setMaxResults(pageSize);
148             }
149             final Collection<?> results = criteria.list();
150             this.transformEntities(transform, results);
151             return results;
152         }
153         catch (HibernateException ex)
154         {
155             throw ex;
156         }
157     }
158 
159     /**
160      * firstResult = (pageNumber - 1) * pageSize
161      * @param pageNumber
162      * @param pageSize
163      * @return firstResult
164      */
165     protected int calculateFirstResult(int pageNumber, int pageSize)
166     {
167         int firstResult = 0;
168         if (pageNumber > 0)
169         {
170             firstResult = (pageNumber - 1) * pageSize;
171         }
172         return firstResult;
173     }
174 
175     /**
176      * {@inheritDoc}
177      */
178     @Override
179     public RulePrecondition create(RulePrecondition rulePrecondition)
180     {
181         return (RulePrecondition)this.create(RulePreconditionDao.TRANSFORM_NONE, rulePrecondition);
182     }
183 
184     /**
185      * {@inheritDoc}
186      */
187     @Override
188     public Object create(final int transform, final RulePrecondition rulePrecondition)
189     {
190         if (rulePrecondition == null)
191         {
192             throw new IllegalArgumentException(
193                 "RulePrecondition.create - 'rulePrecondition' can not be null");
194         }
195         this.getSessionFactory().getCurrentSession().save(rulePrecondition);
196         return this.transformEntity(transform, rulePrecondition);
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     @Override
203     @SuppressWarnings({"unchecked"})
204     public Collection<RulePrecondition> create(final Collection<RulePrecondition> entities)
205     {
206         return (Collection<RulePrecondition>) create(RulePreconditionDao.TRANSFORM_NONE, entities);
207     }
208 
209     /**
210      * {@inheritDoc}
211      */
212     @Override
213     public Collection<?> create(final int transform, final Collection<RulePrecondition> entities)
214     {
215         if (entities == null)
216         {
217             throw new IllegalArgumentException(
218                 "RulePrecondition.create - 'entities' can not be null");
219         }
220                     for (RulePrecondition entity : entities)
221                     {
222                         create(transform, entity);
223                     }
224         return entities;
225     }
226 
227     /**
228      * {@inheritDoc}
229      */
230     @Override
231     public RulePrecondition create(
232         String rulePrecondLb,
233         String rulePrecondIsBidir,
234         String rulePrecondIsActive,
235         Timestamp updateDt)
236     {
237         return (RulePrecondition)this.create(RulePreconditionDao.TRANSFORM_NONE, rulePrecondLb, rulePrecondIsBidir, rulePrecondIsActive, updateDt);
238     }
239 
240     /**
241      * {@inheritDoc}
242      */
243     @Override
244     public Object create(
245         final int transform,
246         String rulePrecondLb,
247         String rulePrecondIsBidir,
248         String rulePrecondIsActive,
249         Timestamp updateDt)
250     {
251         RulePrecondition entity = new RulePreconditionImpl();
252         entity.setRulePrecondLb(rulePrecondLb);
253         entity.setRulePrecondIsBidir(rulePrecondIsBidir);
254         entity.setRulePrecondIsActive(rulePrecondIsActive);
255         entity.setUpdateDt(updateDt);
256         return this.create(transform, entity);
257     }
258 
259     /**
260      * {@inheritDoc}
261      */
262     @Override
263     public RulePrecondition create(
264         Rule rule,
265         String rulePrecondIsActive,
266         String rulePrecondIsBidir,
267         String rulePrecondLb,
268         Timestamp updateDt,
269         Rule usedRule)
270     {
271         return (RulePrecondition)this.create(RulePreconditionDao.TRANSFORM_NONE, rule, rulePrecondIsActive, rulePrecondIsBidir, rulePrecondLb, updateDt, usedRule);
272     }
273 
274     /**
275      * {@inheritDoc}
276      */
277     @Override
278     public Object create(
279         final int transform,
280         Rule rule,
281         String rulePrecondIsActive,
282         String rulePrecondIsBidir,
283         String rulePrecondLb,
284         Timestamp updateDt,
285         Rule usedRule)
286     {
287         RulePrecondition entity = new RulePreconditionImpl();
288         entity.setRule(rule);
289         entity.setRulePrecondIsActive(rulePrecondIsActive);
290         entity.setRulePrecondIsBidir(rulePrecondIsBidir);
291         entity.setRulePrecondLb(rulePrecondLb);
292         entity.setUpdateDt(updateDt);
293         entity.setUsedRule(usedRule);
294         return this.create(transform, entity);
295     }
296 
297     /**
298      * {@inheritDoc}
299      */
300     @Override
301     public void update(RulePrecondition rulePrecondition)
302     {
303         if (rulePrecondition == null)
304         {
305             throw new IllegalArgumentException(
306                 "RulePrecondition.update - 'rulePrecondition' can not be null");
307         }
308         this.getSessionFactory().getCurrentSession().update(rulePrecondition);
309     }
310 
311     /**
312      * {@inheritDoc}
313      */
314     @Override
315     public void update(final Collection<RulePrecondition> entities)
316     {
317         if (entities == null)
318         {
319             throw new IllegalArgumentException(
320                 "RulePrecondition.update - 'entities' can not be null");
321         }
322                     for (RulePrecondition entity : entities)
323                     {
324                         update(entity);
325                     }
326     }
327 
328     /**
329      * {@inheritDoc}
330      */
331     @Override
332     public void remove(RulePrecondition rulePrecondition)
333     {
334         if (rulePrecondition == null)
335         {
336             throw new IllegalArgumentException(
337                 "RulePrecondition.remove - 'rulePrecondition' can not be null");
338         }
339         this.getSessionFactory().getCurrentSession().delete(rulePrecondition);
340     }
341 
342     /**
343      * {@inheritDoc}
344      */
345     @Override
346     public void remove(Integer rulePrecondId)
347     {
348         if (rulePrecondId == null)
349         {
350             throw new IllegalArgumentException(
351                 "RulePrecondition.remove - 'rulePrecondId' can not be null");
352         }
353         RulePrecondition entity = this.get(rulePrecondId);
354         if (entity != null)
355         {
356             this.remove(entity);
357         }
358     }
359 
360     /**
361      * {@inheritDoc}
362      */
363     @Override
364     public void remove(Collection<RulePrecondition> entities)
365     {
366         if (entities == null)
367         {
368             throw new IllegalArgumentException(
369                 "RulePrecondition.remove - 'entities' can not be null");
370         }
371         deleteAll(entities);
372     }
373     /**
374      * Allows transformation of entities into value objects
375      * (or something else for that matter), when the <code>transform</code>
376      * flag is set to one of the constants defined in <code>RulePreconditionDao</code>, please note
377      * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
378      * will be returned.
379      *
380      * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
381      *
382      * @param transform one of the constants declared in {@link RulePreconditionDao}
383      * @param entity an entity that was found
384      * @return the transformed entity (i.e. new value object, etc)
385      * @see RulePreconditionDao#transformEntity(int, RulePrecondition)
386      */
387     public Object transformEntity(final int transform, final RulePrecondition entity)
388     {
389         Object target = null;
390         if (entity != null)
391         {
392             switch (transform)
393             {
394                 case RulePreconditionDao.TRANSFORM_NONE : // fall-through
395                 default:
396                     target = entity;
397             }
398         }
399         return target;
400     }
401 
402     /**
403      * {@inheritDoc}
404      */
405     @Override
406     public void transformEntities(final int transform, final Collection<?> entities)
407     {
408         switch (transform)
409         {
410             case RulePreconditionDao.TRANSFORM_NONE : // fall-through
411                 default:
412                 // do nothing;
413         }
414     }
415 
416     /**
417      * @see RulePreconditionDao#toEntities(Collection)
418      */
419     public void toEntities(final Collection<?> results)
420     {
421         if (results != null)
422         {
423             CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
424         }
425     }
426 
427     /**
428      * This anonymous transformer is designed to transform report query results
429      * (which result in an array of entities) to {@link RulePrecondition}
430      * using the Jakarta Commons-Collections Transformation API.
431      */
432     private Transformer ENTITYTRANSFORMER =
433         new Transformer()
434         {
435             public Object transform(Object input)
436             {
437                 Object result = null;
438                 if (input instanceof Object[])
439                 {
440                     result = toEntity((Object[])input);
441                 }
442                 else if (input instanceof RulePrecondition)
443                 {
444                     result = input;
445                 }
446                 return result;
447             }
448         };
449 
450     /**
451      * @param row
452      * @return RulePrecondition
453      */
454     protected RulePrecondition toEntity(Object[] row)
455     {
456         RulePrecondition target = null;
457         if (row != null)
458         {
459             final int numberOfObjects = row.length;
460             for (int ctr = 0; ctr < numberOfObjects; ctr++)
461             {
462                 final Object object = row[ctr];
463                 if (object instanceof RulePrecondition)
464                 {
465                     target = (RulePrecondition)object;
466                     break;
467                 }
468             }
469         }
470         return target;
471     }
472 
473     /**
474      * Gets the current <code>principal</code> if one has been set,
475      * otherwise returns <code>null</code>.
476      *
477      * @return the current principal
478      */
479     protected Principal getPrincipal()
480     {
481         return PrincipalStore.get();
482     }
483 
484     /**
485      * {@inheritDoc}
486      */
487     @Override
488     @SuppressWarnings({ "unchecked" })
489     public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
490     {
491         try
492         {
493             search.setPageNumber(pageNumber);
494             search.setPageSize(pageSize);
495             final PropertySearch propertySearch = new PropertySearch(
496                 this.getSession(), RulePreconditionImpl.class, search);
497             final List results = propertySearch.executeAsList();
498             this.transformEntities(transform, results);
499             return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
500         }
501         catch (HibernateException ex)
502         {
503             throw ex; /*super.convertHibernateAccessException(ex);*/
504         }
505     }
506 
507     /**
508      * {@inheritDoc}
509      */
510     @Override
511     public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
512     {
513         return this.search(RulePreconditionDao.TRANSFORM_NONE, pageNumber, pageSize, search);
514     }
515 
516     /**
517      * {@inheritDoc}
518      */
519     @Override
520     public Set<?> search(final int transform, final Search search)
521     {
522         try
523         {
524             final PropertySearch propertySearch = new PropertySearch(
525                 this.getSession(), RulePreconditionImpl.class, search);
526             final Set<?> results = propertySearch.executeAsSet();
527             this.transformEntities(transform, results);
528             return results;
529         }
530         catch (HibernateException ex)
531         {
532             throw ex; /*super.convertHibernateAccessException(ex);*/
533         }
534     }
535 
536     /**
537      * {@inheritDoc}
538      */
539     @Override
540     @SuppressWarnings("unchecked")
541     public Set<RulePrecondition> search(final Search search)
542     {
543         return (Set<RulePrecondition>) this.search(RulePreconditionDao.TRANSFORM_NONE, search);
544     }
545 
546     /**
547      * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
548      * @param queryObject
549      * @param transform
550      * @param pageNumber
551      * @param pageSize
552      * @return PaginationResult
553      */
554     @SuppressWarnings({ "unchecked" })
555     protected PaginationResult getPaginationResult(
556         final Query queryObject,
557         final int transform, int pageNumber, int pageSize)
558     {
559         try
560         {
561             final ScrollableResults scrollableResults = queryObject.scroll();
562             scrollableResults.last();
563             int totalCount = scrollableResults.getRowNumber();
564             totalCount = totalCount >= 0 ? totalCount + 1 : 0;
565             if (pageNumber > 0 && pageSize > 0)
566             {
567                 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
568                 queryObject.setMaxResults(pageSize);
569             }
570             // Unchecked transformation because Set object is reused, cannot be strongly typed.
571             Set results = new LinkedHashSet(queryObject.list());
572             transformEntities(transform, results);
573             return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
574         }
575         catch (HibernateException ex)
576         {
577             throw ex; /*super.convertHibernateAccessException(ex);*/
578         }
579     }
580 
581     // spring-hibernate-dao-base merge-point
582 }