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