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