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