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