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