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