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