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