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