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