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