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