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