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