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