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