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