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