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