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