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