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