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