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.synchronization;
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.util.Collection;
34  import java.util.LinkedHashSet;
35  import java.util.List;
36  import java.util.Set;
37  import javax.annotation.Resource;
38  import org.andromda.spring.PaginationResult;
39  import org.apache.commons.collections.CollectionUtils;
40  import org.apache.commons.collections.Transformer;
41  import org.hibernate.Criteria;
42  import org.hibernate.HibernateException;
43  import org.hibernate.Query;
44  import org.hibernate.ScrollableResults;
45  
46  /**
47   * <p>
48   * Base Spring DAO Class: is able to create, update, remove, load, and find
49   * objects of type <code>TempQueryParameter</code>.
50   * </p>
51   *
52   * @see TempQueryParameter
53   */
54  public abstract class TempQueryParameterDaoBase
55      extends HibernateDaoSupport    
56      implements TempQueryParameterDao
57  {
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public Object get(final int transform, final Integer id)
63      {
64          if (id == null)
65          {
66              throw new IllegalArgumentException(
67                  "TempQueryParameter.get - 'id' can not be null");
68          }
69          final TempQueryParameter entity = get(TempQueryParameterImpl.class, id);
70          return transformEntity(transform, entity);
71      }
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public TempQueryParameter get(Integer id)
77      {
78          return (TempQueryParameter)this.get(TRANSFORM_NONE, id);
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public Object load(final int transform, final Integer id)
86      {
87          if (id == null)
88          {
89              throw new IllegalArgumentException(
90                  "TempQueryParameter.load - 'id' can not be null");
91          }
92          final TempQueryParameter entity = get(TempQueryParameterImpl.class, id);
93          return transformEntity(transform, entity);
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public TempQueryParameter load(Integer id)
101     {
102         return (TempQueryParameter)this.load(TRANSFORM_NONE, id);
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     @SuppressWarnings({"unchecked"})
110     public Collection<TempQueryParameter> loadAll()
111     {
112         return (Collection<TempQueryParameter>) this.loadAll(TempQueryParameterDao.TRANSFORM_NONE);
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public Collection<?> loadAll(final int transform)
120     {
121         return this.loadAll(transform, -1, -1);
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public Collection<?> loadAll(final int pageNumber, final int pageSize)
129     {
130         return this.loadAll(TempQueryParameterDao.TRANSFORM_NONE, pageNumber, pageSize);
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     @Override
137     public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
138     {
139         try
140         {
141             final Criteria criteria = this.getSession().createCriteria(TempQueryParameterImpl.class);
142             if (pageNumber > 0 && pageSize > 0)
143             {
144                 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
145                 criteria.setMaxResults(pageSize);
146             }
147             final Collection<?> results = criteria.list();
148             this.transformEntities(transform, results);
149             return results;
150         }
151         catch (HibernateException ex)
152         {
153             throw ex;
154         }
155     }
156 
157     /**
158      * firstResult = (pageNumber - 1) * pageSize
159      * @param pageNumber
160      * @param pageSize
161      * @return firstResult
162      */
163     protected int calculateFirstResult(int pageNumber, int pageSize)
164     {
165         int firstResult = 0;
166         if (pageNumber > 0)
167         {
168             firstResult = (pageNumber - 1) * pageSize;
169         }
170         return firstResult;
171     }
172 
173     /**
174      * {@inheritDoc}
175      */
176     @Override
177     public TempQueryParameter create(TempQueryParameter tempQueryParameter)
178     {
179         return (TempQueryParameter)this.create(TempQueryParameterDao.TRANSFORM_NONE, tempQueryParameter);
180     }
181 
182     /**
183      * {@inheritDoc}
184      */
185     @Override
186     public Object create(final int transform, final TempQueryParameter tempQueryParameter)
187     {
188         if (tempQueryParameter == null)
189         {
190             throw new IllegalArgumentException(
191                 "TempQueryParameter.create - 'tempQueryParameter' can not be null");
192         }
193         this.getSessionFactory().getCurrentSession().save(tempQueryParameter);
194         return this.transformEntity(transform, tempQueryParameter);
195     }
196 
197     /**
198      * {@inheritDoc}
199      */
200     @Override
201     @SuppressWarnings({"unchecked"})
202     public Collection<TempQueryParameter> create(final Collection<TempQueryParameter> entities)
203     {
204         return (Collection<TempQueryParameter>) create(TempQueryParameterDao.TRANSFORM_NONE, entities);
205     }
206 
207     /**
208      * {@inheritDoc}
209      */
210     @Override
211     public Collection<?> create(final int transform, final Collection<TempQueryParameter> entities)
212     {
213         if (entities == null)
214         {
215             throw new IllegalArgumentException(
216                 "TempQueryParameter.create - 'entities' can not be null");
217         }
218                     for (TempQueryParameter entity : entities)
219                     {
220                         create(transform, entity);
221                     }
222         return entities;
223     }
224 
225     /**
226      * {@inheritDoc}
227      */
228     @Override
229     public TempQueryParameter create(
230         String parameterName,
231         Double numericalValue,
232         String alphanumericalValue,
233         Integer quserId,
234         Integer groupingKey)
235     {
236         return (TempQueryParameter)this.create(TempQueryParameterDao.TRANSFORM_NONE, parameterName, numericalValue, alphanumericalValue, quserId, groupingKey);
237     }
238 
239     /**
240      * {@inheritDoc}
241      */
242     @Override
243     public Object create(
244         final int transform,
245         String parameterName,
246         Double numericalValue,
247         String alphanumericalValue,
248         Integer quserId,
249         Integer groupingKey)
250     {
251         TempQueryParameter entity = new TempQueryParameterImpl();
252         entity.setParameterName(parameterName);
253         entity.setNumericalValue(numericalValue);
254         entity.setAlphanumericalValue(alphanumericalValue);
255         entity.setQuserId(quserId);
256         entity.setGroupingKey(groupingKey);
257         return this.create(transform, entity);
258     }
259 
260     /**
261      * {@inheritDoc}
262      */
263     @Override
264     public void update(TempQueryParameter tempQueryParameter)
265     {
266         if (tempQueryParameter == null)
267         {
268             throw new IllegalArgumentException(
269                 "TempQueryParameter.update - 'tempQueryParameter' can not be null");
270         }
271         this.getSessionFactory().getCurrentSession().update(tempQueryParameter);
272     }
273 
274     /**
275      * {@inheritDoc}
276      */
277     @Override
278     public void update(final Collection<TempQueryParameter> entities)
279     {
280         if (entities == null)
281         {
282             throw new IllegalArgumentException(
283                 "TempQueryParameter.update - 'entities' can not be null");
284         }
285                     for (TempQueryParameter entity : entities)
286                     {
287                         update(entity);
288                     }
289     }
290 
291     /**
292      * {@inheritDoc}
293      */
294     @Override
295     public void remove(TempQueryParameter tempQueryParameter)
296     {
297         if (tempQueryParameter == null)
298         {
299             throw new IllegalArgumentException(
300                 "TempQueryParameter.remove - 'tempQueryParameter' can not be null");
301         }
302         this.getSessionFactory().getCurrentSession().delete(tempQueryParameter);
303     }
304 
305     /**
306      * {@inheritDoc}
307      */
308     @Override
309     public void remove(Integer id)
310     {
311         if (id == null)
312         {
313             throw new IllegalArgumentException(
314                 "TempQueryParameter.remove - 'id' can not be null");
315         }
316         TempQueryParameter entity = this.get(id);
317         if (entity != null)
318         {
319             this.remove(entity);
320         }
321     }
322 
323     /**
324      * {@inheritDoc}
325      */
326     @Override
327     public void remove(Collection<TempQueryParameter> entities)
328     {
329         if (entities == null)
330         {
331             throw new IllegalArgumentException(
332                 "TempQueryParameter.remove - 'entities' can not be null");
333         }
334         deleteAll(entities);
335     }
336     /**
337      * Allows transformation of entities into value objects
338      * (or something else for that matter), when the <code>transform</code>
339      * flag is set to one of the constants defined in <code>TempQueryParameterDao</code>, please note
340      * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
341      * will be returned.
342      *
343      * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
344      *
345      * @param transform one of the constants declared in {@link TempQueryParameterDao}
346      * @param entity an entity that was found
347      * @return the transformed entity (i.e. new value object, etc)
348      * @see TempQueryParameterDao#transformEntity(int, TempQueryParameter)
349      */
350     public Object transformEntity(final int transform, final TempQueryParameter entity)
351     {
352         Object target = null;
353         if (entity != null)
354         {
355             switch (transform)
356             {
357                 case TempQueryParameterDao.TRANSFORM_NONE : // fall-through
358                 default:
359                     target = entity;
360             }
361         }
362         return target;
363     }
364 
365     /**
366      * {@inheritDoc}
367      */
368     @Override
369     public void transformEntities(final int transform, final Collection<?> entities)
370     {
371         switch (transform)
372         {
373             case TempQueryParameterDao.TRANSFORM_NONE : // fall-through
374                 default:
375                 // do nothing;
376         }
377     }
378 
379     /**
380      * @see TempQueryParameterDao#toEntities(Collection)
381      */
382     public void toEntities(final Collection<?> results)
383     {
384         if (results != null)
385         {
386             CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
387         }
388     }
389 
390     /**
391      * This anonymous transformer is designed to transform report query results
392      * (which result in an array of entities) to {@link TempQueryParameter}
393      * using the Jakarta Commons-Collections Transformation API.
394      */
395     private Transformer ENTITYTRANSFORMER =
396         new Transformer()
397         {
398             public Object transform(Object input)
399             {
400                 Object result = null;
401                 if (input instanceof Object[])
402                 {
403                     result = toEntity((Object[])input);
404                 }
405                 else if (input instanceof TempQueryParameter)
406                 {
407                     result = input;
408                 }
409                 return result;
410             }
411         };
412 
413     /**
414      * @param row
415      * @return TempQueryParameter
416      */
417     protected TempQueryParameter toEntity(Object[] row)
418     {
419         TempQueryParameter target = null;
420         if (row != null)
421         {
422             final int numberOfObjects = row.length;
423             for (int ctr = 0; ctr < numberOfObjects; ctr++)
424             {
425                 final Object object = row[ctr];
426                 if (object instanceof TempQueryParameter)
427                 {
428                     target = (TempQueryParameter)object;
429                     break;
430                 }
431             }
432         }
433         return target;
434     }
435 
436     /**
437      * Gets the current <code>principal</code> if one has been set,
438      * otherwise returns <code>null</code>.
439      *
440      * @return the current principal
441      */
442     protected Principal getPrincipal()
443     {
444         return PrincipalStore.get();
445     }
446 
447     /**
448      * {@inheritDoc}
449      */
450     @Override
451     @SuppressWarnings({ "unchecked" })
452     public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
453     {
454         try
455         {
456             search.setPageNumber(pageNumber);
457             search.setPageSize(pageSize);
458             final PropertySearch propertySearch = new PropertySearch(
459                 this.getSession(), TempQueryParameterImpl.class, search);
460             final List results = propertySearch.executeAsList();
461             this.transformEntities(transform, results);
462             return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
463         }
464         catch (HibernateException ex)
465         {
466             throw ex; /*super.convertHibernateAccessException(ex);*/
467         }
468     }
469 
470     /**
471      * {@inheritDoc}
472      */
473     @Override
474     public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
475     {
476         return this.search(TempQueryParameterDao.TRANSFORM_NONE, pageNumber, pageSize, search);
477     }
478 
479     /**
480      * {@inheritDoc}
481      */
482     @Override
483     public Set<?> search(final int transform, final Search search)
484     {
485         try
486         {
487             final PropertySearch propertySearch = new PropertySearch(
488                 this.getSession(), TempQueryParameterImpl.class, search);
489             final Set<?> results = propertySearch.executeAsSet();
490             this.transformEntities(transform, results);
491             return results;
492         }
493         catch (HibernateException ex)
494         {
495             throw ex; /*super.convertHibernateAccessException(ex);*/
496         }
497     }
498 
499     /**
500      * {@inheritDoc}
501      */
502     @Override
503     @SuppressWarnings("unchecked")
504     public Set<TempQueryParameter> search(final Search search)
505     {
506         return (Set<TempQueryParameter>) this.search(TempQueryParameterDao.TRANSFORM_NONE, search);
507     }
508 
509     /**
510      * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
511      * @param queryObject
512      * @param transform
513      * @param pageNumber
514      * @param pageSize
515      * @return PaginationResult
516      */
517     @SuppressWarnings({ "unchecked" })
518     protected PaginationResult getPaginationResult(
519         final Query queryObject,
520         final int transform, int pageNumber, int pageSize)
521     {
522         try
523         {
524             final ScrollableResults scrollableResults = queryObject.scroll();
525             scrollableResults.last();
526             int totalCount = scrollableResults.getRowNumber();
527             totalCount = totalCount >= 0 ? totalCount + 1 : 0;
528             if (pageNumber > 0 && pageSize > 0)
529             {
530                 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
531                 queryObject.setMaxResults(pageSize);
532             }
533             // Unchecked transformation because Set object is reused, cannot be strongly typed.
534             Set results = new LinkedHashSet(queryObject.list());
535             transformEntities(transform, results);
536             return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
537         }
538         catch (HibernateException ex)
539         {
540             throw ex; /*super.convertHibernateAccessException(ex);*/
541         }
542     }
543 
544     // spring-hibernate-dao-base merge-point
545 }