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