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