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