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