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