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