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