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