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