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