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