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