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