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