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