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