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