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.technical.hibernate.HibernateDaoSupport;
33  import java.security.Principal;
34  import java.util.Collection;
35  import java.util.Date;
36  import java.util.LinkedHashSet;
37  import java.util.List;
38  import java.util.Set;
39  import javax.annotation.Resource;
40  import org.andromda.spring.PaginationResult;
41  import org.apache.commons.collections.CollectionUtils;
42  import org.apache.commons.collections.Transformer;
43  import org.hibernate.Criteria;
44  import org.hibernate.HibernateException;
45  import org.hibernate.Query;
46  import org.hibernate.ScrollableResults;
47  
48  /**
49   * <p>
50   * Base Spring DAO Class: is able to create, update, remove, load, and find
51   * objects of type <code>BugReport</code>.
52   * </p>
53   *
54   * @see BugReport
55   */
56  public abstract class BugReportDaoBase
57      extends HibernateDaoSupport    
58      implements BugReportDao
59  {
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public Object get(final int transform, final Integer bugId)
65      {
66          if (bugId == null)
67          {
68              throw new IllegalArgumentException(
69                  "BugReport.get - 'bugId' can not be null");
70          }
71          final BugReport entity = get(BugReportImpl.class, bugId);
72          return transformEntity(transform, entity);
73      }
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public BugReport get(Integer bugId)
79      {
80          return (BugReport)this.get(TRANSFORM_NONE, bugId);
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public Object load(final int transform, final Integer bugId)
88      {
89          if (bugId == null)
90          {
91              throw new IllegalArgumentException(
92                  "BugReport.load - 'bugId' can not be null");
93          }
94          final BugReport entity = get(BugReportImpl.class, bugId);
95          return transformEntity(transform, entity);
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public BugReport load(Integer bugId)
103     {
104         return (BugReport)this.load(TRANSFORM_NONE, bugId);
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     @SuppressWarnings({"unchecked"})
112     public Collection<BugReport> loadAll()
113     {
114         return (Collection<BugReport>) this.loadAll(BugReportDao.TRANSFORM_NONE);
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public Collection<?> loadAll(final int transform)
122     {
123         return this.loadAll(transform, -1, -1);
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public Collection<?> loadAll(final int pageNumber, final int pageSize)
131     {
132         return this.loadAll(BugReportDao.TRANSFORM_NONE, pageNumber, pageSize);
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
140     {
141         try
142         {
143             final Criteria criteria = this.getSession().createCriteria(BugReportImpl.class);
144             if (pageNumber > 0 && pageSize > 0)
145             {
146                 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
147                 criteria.setMaxResults(pageSize);
148             }
149             final Collection<?> results = criteria.list();
150             this.transformEntities(transform, results);
151             return results;
152         }
153         catch (HibernateException ex)
154         {
155             throw ex;
156         }
157     }
158 
159     /**
160      * firstResult = (pageNumber - 1) * pageSize
161      * @param pageNumber
162      * @param pageSize
163      * @return firstResult
164      */
165     protected int calculateFirstResult(int pageNumber, int pageSize)
166     {
167         int firstResult = 0;
168         if (pageNumber > 0)
169         {
170             firstResult = (pageNumber - 1) * pageSize;
171         }
172         return firstResult;
173     }
174 
175     /**
176      * {@inheritDoc}
177      */
178     @Override
179     public BugReport create(BugReport bugReport)
180     {
181         return (BugReport)this.create(BugReportDao.TRANSFORM_NONE, bugReport);
182     }
183 
184     /**
185      * {@inheritDoc}
186      */
187     @Override
188     public Object create(final int transform, final BugReport bugReport)
189     {
190         if (bugReport == null)
191         {
192             throw new IllegalArgumentException(
193                 "BugReport.create - 'bugReport' can not be null");
194         }
195         this.getSessionFactory().getCurrentSession().save(bugReport);
196         return this.transformEntity(transform, bugReport);
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     @Override
203     @SuppressWarnings({"unchecked"})
204     public Collection<BugReport> create(final Collection<BugReport> entities)
205     {
206         return (Collection<BugReport>) create(BugReportDao.TRANSFORM_NONE, entities);
207     }
208 
209     /**
210      * {@inheritDoc}
211      */
212     @Override
213     public Collection<?> create(final int transform, final Collection<BugReport> entities)
214     {
215         if (entities == null)
216         {
217             throw new IllegalArgumentException(
218                 "BugReport.create - 'entities' can not be null");
219         }
220                     for (BugReport entity : entities)
221                     {
222                         create(transform, entity);
223                     }
224         return entities;
225     }
226 
227     /**
228      * {@inheritDoc}
229      */
230     @Override
231     public BugReport create(
232         String bugSummary,
233         String bugDescription,
234         String bugReporterName,
235         String bugStatus,
236         String bugCategory,
237         String bugPriority,
238         String bugSeverity,
239         Date bugSubmittedDate,
240         Date bugUpdatedDate,
241         Date bugWishedDate,
242         Date bugClosedDate,
243         String bugProductVersion,
244         String bugFixedVersion)
245     {
246         return (BugReport)this.create(BugReportDao.TRANSFORM_NONE, bugSummary, bugDescription, bugReporterName, bugStatus, bugCategory, bugPriority, bugSeverity, bugSubmittedDate, bugUpdatedDate, bugWishedDate, bugClosedDate, bugProductVersion, bugFixedVersion);
247     }
248 
249     /**
250      * {@inheritDoc}
251      */
252     @Override
253     public Object create(
254         final int transform,
255         String bugSummary,
256         String bugDescription,
257         String bugReporterName,
258         String bugStatus,
259         String bugCategory,
260         String bugPriority,
261         String bugSeverity,
262         Date bugSubmittedDate,
263         Date bugUpdatedDate,
264         Date bugWishedDate,
265         Date bugClosedDate,
266         String bugProductVersion,
267         String bugFixedVersion)
268     {
269         BugReport entity = new BugReportImpl();
270         entity.setBugSummary(bugSummary);
271         entity.setBugDescription(bugDescription);
272         entity.setBugReporterName(bugReporterName);
273         entity.setBugStatus(bugStatus);
274         entity.setBugCategory(bugCategory);
275         entity.setBugPriority(bugPriority);
276         entity.setBugSeverity(bugSeverity);
277         entity.setBugSubmittedDate(bugSubmittedDate);
278         entity.setBugUpdatedDate(bugUpdatedDate);
279         entity.setBugWishedDate(bugWishedDate);
280         entity.setBugClosedDate(bugClosedDate);
281         entity.setBugProductVersion(bugProductVersion);
282         entity.setBugFixedVersion(bugFixedVersion);
283         return this.create(transform, entity);
284     }
285 
286     /**
287      * {@inheritDoc}
288      */
289     @Override
290     public void update(BugReport bugReport)
291     {
292         if (bugReport == null)
293         {
294             throw new IllegalArgumentException(
295                 "BugReport.update - 'bugReport' can not be null");
296         }
297         this.getSessionFactory().getCurrentSession().update(bugReport);
298     }
299 
300     /**
301      * {@inheritDoc}
302      */
303     @Override
304     public void update(final Collection<BugReport> entities)
305     {
306         if (entities == null)
307         {
308             throw new IllegalArgumentException(
309                 "BugReport.update - 'entities' can not be null");
310         }
311                     for (BugReport entity : entities)
312                     {
313                         update(entity);
314                     }
315     }
316 
317     /**
318      * {@inheritDoc}
319      */
320     @Override
321     public void remove(BugReport bugReport)
322     {
323         if (bugReport == null)
324         {
325             throw new IllegalArgumentException(
326                 "BugReport.remove - 'bugReport' can not be null");
327         }
328         this.getSessionFactory().getCurrentSession().delete(bugReport);
329     }
330 
331     /**
332      * {@inheritDoc}
333      */
334     @Override
335     public void remove(Integer bugId)
336     {
337         if (bugId == null)
338         {
339             throw new IllegalArgumentException(
340                 "BugReport.remove - 'bugId' can not be null");
341         }
342         BugReport entity = this.get(bugId);
343         if (entity != null)
344         {
345             this.remove(entity);
346         }
347     }
348 
349     /**
350      * {@inheritDoc}
351      */
352     @Override
353     public void remove(Collection<BugReport> entities)
354     {
355         if (entities == null)
356         {
357             throw new IllegalArgumentException(
358                 "BugReport.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>BugReportDao</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 BugReportDao}
372      * @param entity an entity that was found
373      * @return the transformed entity (i.e. new value object, etc)
374      * @see BugReportDao#transformEntity(int, BugReport)
375      */
376     public Object transformEntity(final int transform, final BugReport entity)
377     {
378         Object target = null;
379         if (entity != null)
380         {
381             switch (transform)
382             {
383                 case BugReportDao.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 BugReportDao.TRANSFORM_NONE : // fall-through
400                 default:
401                 // do nothing;
402         }
403     }
404 
405     /**
406      * @see BugReportDao#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 BugReport}
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 BugReport)
432                 {
433                     result = input;
434                 }
435                 return result;
436             }
437         };
438 
439     /**
440      * @param row
441      * @return BugReport
442      */
443     protected BugReport toEntity(Object[] row)
444     {
445         BugReport 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 BugReport)
453                 {
454                     target = (BugReport)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(), BugReportImpl.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(BugReportDao.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(), BugReportImpl.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<BugReport> search(final Search search)
531     {
532         return (Set<BugReport>) this.search(BugReportDao.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 }