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