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