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