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