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