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