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.referential.pmfm;
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.referential.Status;
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>Parameter</code>.
53   * </p>
54   *
55   * @see Parameter
56   */
57  public abstract class ParameterDaoBase
58      extends HibernateDaoSupport    
59      implements ParameterDao
60  {
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public Object get(final int transform, final String parCd)
66      {
67          if (parCd == null)
68          {
69              throw new IllegalArgumentException(
70                  "Parameter.get - 'parCd' can not be null");
71          }
72          final Parameter entity = get(ParameterImpl.class, parCd);
73          return transformEntity(transform, entity);
74      }
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public Parameter get(String parCd)
80      {
81          return (Parameter)this.get(TRANSFORM_NONE, parCd);
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public Object load(final int transform, final String parCd)
89      {
90          if (parCd == null)
91          {
92              throw new IllegalArgumentException(
93                  "Parameter.load - 'parCd' can not be null");
94          }
95          final Parameter entity = get(ParameterImpl.class, parCd);
96          return transformEntity(transform, entity);
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public Parameter load(String parCd)
104     {
105         return (Parameter)this.load(TRANSFORM_NONE, parCd);
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     @SuppressWarnings({"unchecked"})
113     public Collection<Parameter> loadAll()
114     {
115         return (Collection<Parameter>) this.loadAll(ParameterDao.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(ParameterDao.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(ParameterImpl.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 Parameter create(Parameter parameter)
181     {
182         return (Parameter)this.create(ParameterDao.TRANSFORM_NONE, parameter);
183     }
184 
185     /**
186      * {@inheritDoc}
187      */
188     @Override
189     public Object create(final int transform, final Parameter parameter)
190     {
191         if (parameter == null)
192         {
193             throw new IllegalArgumentException(
194                 "Parameter.create - 'parameter' can not be null");
195         }
196         this.getSessionFactory().getCurrentSession().save(parameter);
197         return this.transformEntity(transform, parameter);
198     }
199 
200     /**
201      * {@inheritDoc}
202      */
203     @Override
204     @SuppressWarnings({"unchecked"})
205     public Collection<Parameter> create(final Collection<Parameter> entities)
206     {
207         return (Collection<Parameter>) create(ParameterDao.TRANSFORM_NONE, entities);
208     }
209 
210     /**
211      * {@inheritDoc}
212      */
213     @Override
214     public Collection<?> create(final int transform, final Collection<Parameter> entities)
215     {
216         if (entities == null)
217         {
218             throw new IllegalArgumentException(
219                 "Parameter.create - 'entities' can not be null");
220         }
221                     for (Parameter entity : entities)
222                     {
223                         create(transform, entity);
224                     }
225         return entities;
226     }
227 
228     /**
229      * {@inheritDoc}
230      */
231     @Override
232     public Parameter create(
233         String parNm,
234         String parDc,
235         String parIsQualitative,
236         String parIsCalculated,
237         String parIsTaxonomic,
238         Date parCreationDt,
239         Timestamp updateDt,
240         String parCm)
241     {
242         return (Parameter)this.create(ParameterDao.TRANSFORM_NONE, parNm, parDc, parIsQualitative, parIsCalculated, parIsTaxonomic, parCreationDt, updateDt, parCm);
243     }
244 
245     /**
246      * {@inheritDoc}
247      */
248     @Override
249     public Object create(
250         final int transform,
251         String parNm,
252         String parDc,
253         String parIsQualitative,
254         String parIsCalculated,
255         String parIsTaxonomic,
256         Date parCreationDt,
257         Timestamp updateDt,
258         String parCm)
259     {
260         Parameter entity = new ParameterImpl();
261         entity.setParNm(parNm);
262         entity.setParDc(parDc);
263         entity.setParIsQualitative(parIsQualitative);
264         entity.setParIsCalculated(parIsCalculated);
265         entity.setParIsTaxonomic(parIsTaxonomic);
266         entity.setParCreationDt(parCreationDt);
267         entity.setUpdateDt(updateDt);
268         entity.setParCm(parCm);
269         return this.create(transform, entity);
270     }
271 
272     /**
273      * {@inheritDoc}
274      */
275     @Override
276     public Parameter create(
277         ParameterGroup parGroupId,
278         Status status)
279     {
280         return (Parameter)this.create(ParameterDao.TRANSFORM_NONE, parGroupId, status);
281     }
282 
283     /**
284      * {@inheritDoc}
285      */
286     @Override
287     public Object create(
288         final int transform,
289         ParameterGroup parGroupId,
290         Status status)
291     {
292         Parameter entity = new ParameterImpl();
293         entity.setParGroupId(parGroupId);
294         entity.setStatus(status);
295         return this.create(transform, entity);
296     }
297 
298     /**
299      * {@inheritDoc}
300      */
301     @Override
302     public void update(Parameter parameter)
303     {
304         if (parameter == null)
305         {
306             throw new IllegalArgumentException(
307                 "Parameter.update - 'parameter' can not be null");
308         }
309         this.getSessionFactory().getCurrentSession().update(parameter);
310     }
311 
312     /**
313      * {@inheritDoc}
314      */
315     @Override
316     public void update(final Collection<Parameter> entities)
317     {
318         if (entities == null)
319         {
320             throw new IllegalArgumentException(
321                 "Parameter.update - 'entities' can not be null");
322         }
323                     for (Parameter entity : entities)
324                     {
325                         update(entity);
326                     }
327     }
328 
329     /**
330      * {@inheritDoc}
331      */
332     @Override
333     public void remove(Parameter parameter)
334     {
335         if (parameter == null)
336         {
337             throw new IllegalArgumentException(
338                 "Parameter.remove - 'parameter' can not be null");
339         }
340         this.getSessionFactory().getCurrentSession().delete(parameter);
341     }
342 
343     /**
344      * {@inheritDoc}
345      */
346     @Override
347     public void remove(String parCd)
348     {
349         if (parCd == null)
350         {
351             throw new IllegalArgumentException(
352                 "Parameter.remove - 'parCd' can not be null");
353         }
354         Parameter entity = this.get(parCd);
355         if (entity != null)
356         {
357             this.remove(entity);
358         }
359     }
360 
361     /**
362      * {@inheritDoc}
363      */
364     @Override
365     public void remove(Collection<Parameter> entities)
366     {
367         if (entities == null)
368         {
369             throw new IllegalArgumentException(
370                 "Parameter.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>ParameterDao</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 ParameterDao}
384      * @param entity an entity that was found
385      * @return the transformed entity (i.e. new value object, etc)
386      * @see ParameterDao#transformEntity(int, Parameter)
387      */
388     public Object transformEntity(final int transform, final Parameter entity)
389     {
390         Object target = null;
391         if (entity != null)
392         {
393             switch (transform)
394             {
395                 case ParameterDao.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 ParameterDao.TRANSFORM_NONE : // fall-through
412                 default:
413                 // do nothing;
414         }
415     }
416 
417     /**
418      * @see ParameterDao#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 Parameter}
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 Parameter)
444                 {
445                     result = input;
446                 }
447                 return result;
448             }
449         };
450 
451     /**
452      * @param row
453      * @return Parameter
454      */
455     protected Parameter toEntity(Object[] row)
456     {
457         Parameter 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 Parameter)
465                 {
466                     target = (Parameter)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(), ParameterImpl.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(ParameterDao.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(), ParameterImpl.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<Parameter> search(final Search search)
543     {
544         return (Set<Parameter>) this.search(ParameterDao.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 }