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