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