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