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