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