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