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