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