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