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