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