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