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