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