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