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.administration.program;
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.administration.user.Department;
32  import fr.ifremer.quadrige3.core.dao.technical.hibernate.HibernateDaoSupport;
33  import fr.ifremer.quadrige3.core.vo.administration.program.ProgDepProgPrivVO;
34  import java.security.Principal;
35  import java.sql.Timestamp;
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Iterator;
39  import java.util.LinkedHashSet;
40  import java.util.List;
41  import java.util.Set;
42  import javax.annotation.Resource;
43  import org.andromda.spring.PaginationResult;
44  import org.apache.commons.collections.CollectionUtils;
45  import org.apache.commons.collections.Transformer;
46  import org.hibernate.Criteria;
47  import org.hibernate.HibernateException;
48  import org.hibernate.Query;
49  import org.hibernate.ScrollableResults;
50  
51  /**
52   * <p>
53   * Base Spring DAO Class: is able to create, update, remove, load, and find
54   * objects of type <code>ProgDepProgPriv</code>.
55   * </p>
56   *
57   * @see ProgDepProgPriv
58   */
59  public abstract class ProgDepProgPrivDaoBase
60      extends HibernateDaoSupport    
61      implements ProgDepProgPrivDao
62  {
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public Object get(final int transform, final ProgDepProgPrivPK progDepProgPrivPk)
68      {
69          if (progDepProgPrivPk == null)
70          {
71              throw new IllegalArgumentException(
72                  "ProgDepProgPriv.get - 'progDepProgPrivPk' can not be null");
73          }
74          final ProgDepProgPriv entity = get(ProgDepProgPrivImpl.class, progDepProgPrivPk);
75          return transformEntity(transform, entity);
76      }
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public ProgDepProgPriv get(ProgDepProgPrivPK progDepProgPrivPk)
82      {
83          return (ProgDepProgPriv)this.get(TRANSFORM_NONE, progDepProgPrivPk);
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public Object load(final int transform, final ProgDepProgPrivPK progDepProgPrivPk)
91      {
92          if (progDepProgPrivPk == null)
93          {
94              throw new IllegalArgumentException(
95                  "ProgDepProgPriv.load - 'progDepProgPrivPk' can not be null");
96          }
97          final ProgDepProgPriv entity = get(ProgDepProgPrivImpl.class, progDepProgPrivPk);
98          return transformEntity(transform, entity);
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public ProgDepProgPriv load(ProgDepProgPrivPK progDepProgPrivPk)
106     {
107         return (ProgDepProgPriv)this.load(TRANSFORM_NONE, progDepProgPrivPk);
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     @SuppressWarnings({"unchecked"})
115     public Collection<ProgDepProgPriv> loadAll()
116     {
117         return (Collection<ProgDepProgPriv>) this.loadAll(ProgDepProgPrivDao.TRANSFORM_NONE);
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public Collection<?> loadAll(final int transform)
125     {
126         return this.loadAll(transform, -1, -1);
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public Collection<?> loadAll(final int pageNumber, final int pageSize)
134     {
135         return this.loadAll(ProgDepProgPrivDao.TRANSFORM_NONE, pageNumber, pageSize);
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
143     {
144         try
145         {
146             final Criteria criteria = this.getSession().createCriteria(ProgDepProgPrivImpl.class);
147             if (pageNumber > 0 && pageSize > 0)
148             {
149                 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
150                 criteria.setMaxResults(pageSize);
151             }
152             final Collection<?> results = criteria.list();
153             this.transformEntities(transform, results);
154             return results;
155         }
156         catch (HibernateException ex)
157         {
158             throw ex;
159         }
160     }
161 
162     /**
163      * firstResult = (pageNumber - 1) * pageSize
164      * @param pageNumber
165      * @param pageSize
166      * @return firstResult
167      */
168     protected int calculateFirstResult(int pageNumber, int pageSize)
169     {
170         int firstResult = 0;
171         if (pageNumber > 0)
172         {
173             firstResult = (pageNumber - 1) * pageSize;
174         }
175         return firstResult;
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
181     @Override
182     public ProgDepProgPriv create(ProgDepProgPriv progDepProgPriv)
183     {
184         return (ProgDepProgPriv)this.create(ProgDepProgPrivDao.TRANSFORM_NONE, progDepProgPriv);
185     }
186 
187     /**
188      * {@inheritDoc}
189      */
190     @Override
191     public Object create(final int transform, final ProgDepProgPriv progDepProgPriv)
192     {
193         if (progDepProgPriv == null)
194         {
195             throw new IllegalArgumentException(
196                 "ProgDepProgPriv.create - 'progDepProgPriv' can not be null");
197         }
198         this.getSessionFactory().getCurrentSession().save(progDepProgPriv);
199         return this.transformEntity(transform, progDepProgPriv);
200     }
201 
202     /**
203      * {@inheritDoc}
204      */
205     @Override
206     @SuppressWarnings({"unchecked"})
207     public Collection<ProgDepProgPriv> create(final Collection<ProgDepProgPriv> entities)
208     {
209         return (Collection<ProgDepProgPriv>) create(ProgDepProgPrivDao.TRANSFORM_NONE, entities);
210     }
211 
212     /**
213      * {@inheritDoc}
214      */
215     @Override
216     public Collection<?> create(final int transform, final Collection<ProgDepProgPriv> entities)
217     {
218         if (entities == null)
219         {
220             throw new IllegalArgumentException(
221                 "ProgDepProgPriv.create - 'entities' can not be null");
222         }
223                     for (ProgDepProgPriv entity : entities)
224                     {
225                         create(transform, entity);
226                     }
227         return entities;
228     }
229 
230     /**
231      * {@inheritDoc}
232      */
233     @Override
234     public ProgDepProgPriv create(
235         Timestamp updateDt)
236     {
237         return (ProgDepProgPriv)this.create(ProgDepProgPrivDao.TRANSFORM_NONE, updateDt);
238     }
239 
240     /**
241      * {@inheritDoc}
242      */
243     @Override
244     public Object create(
245         final int transform,
246         Timestamp updateDt)
247     {
248         ProgDepProgPriv entity = new ProgDepProgPrivImpl();
249         entity.setUpdateDt(updateDt);
250         return this.create(transform, entity);
251     }
252 
253     /**
254      * {@inheritDoc}
255      */
256     @Override
257     public ProgDepProgPriv create(
258         ProgramPrivilege programPrivilege,
259         Department department,
260         Program program)
261     {
262         return (ProgDepProgPriv)this.create(ProgDepProgPrivDao.TRANSFORM_NONE, programPrivilege, department, program);
263     }
264 
265     /**
266      * {@inheritDoc}
267      */
268     @Override
269     public Object create(
270         final int transform,
271         ProgramPrivilege programPrivilege,
272         Department department,
273         Program program)
274     {
275         ProgDepProgPriv entity = new ProgDepProgPrivImpl();
276         entity.setProgramPrivilege(programPrivilege);
277         entity.setDepartment(department);
278         entity.setProgram(program);
279         return this.create(transform, entity);
280     }
281 
282     /**
283      * {@inheritDoc}
284      */
285     @Override
286     public void update(ProgDepProgPriv progDepProgPriv)
287     {
288         if (progDepProgPriv == null)
289         {
290             throw new IllegalArgumentException(
291                 "ProgDepProgPriv.update - 'progDepProgPriv' can not be null");
292         }
293         this.getSessionFactory().getCurrentSession().update(progDepProgPriv);
294     }
295 
296     /**
297      * {@inheritDoc}
298      */
299     @Override
300     public void update(final Collection<ProgDepProgPriv> entities)
301     {
302         if (entities == null)
303         {
304             throw new IllegalArgumentException(
305                 "ProgDepProgPriv.update - 'entities' can not be null");
306         }
307                     for (ProgDepProgPriv entity : entities)
308                     {
309                         update(entity);
310                     }
311     }
312 
313     /**
314      * {@inheritDoc}
315      */
316     @Override
317     public void remove(ProgDepProgPriv progDepProgPriv)
318     {
319         if (progDepProgPriv == null)
320         {
321             throw new IllegalArgumentException(
322                 "ProgDepProgPriv.remove - 'progDepProgPriv' can not be null");
323         }
324         this.getSessionFactory().getCurrentSession().delete(progDepProgPriv);
325     }
326 
327     /**
328      * {@inheritDoc}
329      */
330     @Override
331     public void remove(ProgDepProgPrivPK progDepProgPrivPk)
332     {
333         if (progDepProgPrivPk == null)
334         {
335             throw new IllegalArgumentException(
336                 "ProgDepProgPriv.remove - 'progDepProgPrivPk' can not be null");
337         }
338         ProgDepProgPriv entity = this.get(progDepProgPrivPk);
339         if (entity != null)
340         {
341             this.remove(entity);
342         }
343     }
344 
345     /**
346      * {@inheritDoc}
347      */
348     @Override
349     public void remove(Collection<ProgDepProgPriv> entities)
350     {
351         if (entities == null)
352         {
353             throw new IllegalArgumentException(
354                 "ProgDepProgPriv.remove - 'entities' can not be null");
355         }
356         deleteAll(entities);
357     }
358     /**
359      * Allows transformation of entities into value objects
360      * (or something else for that matter), when the <code>transform</code>
361      * flag is set to one of the constants defined in <code>ProgDepProgPrivDao</code>, please note
362      * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
363      * will be returned.
364      * <p>
365      * This method will return instances of these types:
366      * <ul>
367      *   <li>{@link ProgDepProgPriv} - {@link #TRANSFORM_NONE}</li>
368      *   <li>{@link ProgDepProgPrivVO} - {@link #TRANSFORM_PROGDEPPROGPRIVVO}</li>
369      * </ul>
370      *
371      * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
372      *
373      * @param transform one of the constants declared in {@link ProgDepProgPrivDao}
374      * @param entity an entity that was found
375      * @return the transformed entity (i.e. new value object, etc)
376      * @see ProgDepProgPrivDao#transformEntity(int, ProgDepProgPriv)
377      */
378     public Object transformEntity(final int transform, final ProgDepProgPriv entity)
379     {
380         Object target = null;
381         if (entity != null)
382         {
383             switch (transform)
384             {
385                 case TRANSFORM_PROGDEPPROGPRIVVO :
386                     target = toProgDepProgPrivVO(entity);
387                     break;
388                 case ProgDepProgPrivDao.TRANSFORM_NONE : // fall-through
389                 default:
390                     target = entity;
391             }
392         }
393         return target;
394     }
395 
396     /**
397      * {@inheritDoc}
398      */
399     @Override
400     public void transformEntities(final int transform, final Collection<?> entities)
401     {
402         switch (transform)
403         {
404             case TRANSFORM_PROGDEPPROGPRIVVO :
405                 toProgDepProgPrivVOCollection(entities);
406                 break;
407             case ProgDepProgPrivDao.TRANSFORM_NONE : // fall-through
408                 default:
409                 // do nothing;
410         }
411     }
412 
413     /**
414      * @see ProgDepProgPrivDao#toEntities(Collection)
415      */
416     public void toEntities(final Collection<?> results)
417     {
418         if (results != null)
419         {
420             CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
421         }
422     }
423 
424     /**
425      * This anonymous transformer is designed to transform report query results
426      * (which result in an array of entities) to {@link ProgDepProgPriv}
427      * using the Jakarta Commons-Collections Transformation API.
428      */
429     private Transformer ENTITYTRANSFORMER =
430         new Transformer()
431         {
432             public Object transform(Object input)
433             {
434                 Object result = null;
435                 if (input instanceof Object[])
436                 {
437                     result = toEntity((Object[])input);
438                 }
439                 else if (input instanceof ProgDepProgPriv)
440                 {
441                     result = input;
442                 }
443                 return result;
444             }
445         };
446 
447     /**
448      * @param row
449      * @return ProgDepProgPriv
450      */
451     protected ProgDepProgPriv toEntity(Object[] row)
452     {
453         ProgDepProgPriv target = null;
454         if (row != null)
455         {
456             final int numberOfObjects = row.length;
457             for (int ctr = 0; ctr < numberOfObjects; ctr++)
458             {
459                 final Object object = row[ctr];
460                 if (object instanceof ProgDepProgPriv)
461                 {
462                     target = (ProgDepProgPriv)object;
463                     break;
464                 }
465             }
466         }
467         return target;
468     }
469 
470     /**
471      * {@inheritDoc}
472      */
473     @Override
474     @SuppressWarnings({"unchecked"})
475     public final Collection<ProgDepProgPrivVO> toProgDepProgPrivVOCollection(Collection<?> entities)
476     {
477         Collection<ProgDepProgPrivVO> result = new ArrayList<ProgDepProgPrivVO>();
478         if (entities != null)
479         {
480             CollectionUtils.transform(entities, this.PROGDEPPROGPRIVVO_TRANSFORMER);
481             result.addAll((Collection<? extends ProgDepProgPrivVO>) entities);
482         }
483         return result;
484     }
485 
486     /**
487      * {@inheritDoc}
488      */
489     @Override
490     @SuppressWarnings({ "unchecked" })
491     public final ProgDepProgPrivVO[] toProgDepProgPrivVOArray(Collection<?> entities)
492     {
493         ProgDepProgPrivVO[] result = null;
494         if (entities != null)
495         {
496             // Unchecked transformation: reuses entities Collection
497             final Collection collection = new ArrayList(entities);
498             this.toProgDepProgPrivVOCollection(collection);
499             result = (ProgDepProgPrivVO[]) collection.toArray(new ProgDepProgPrivVO[collection.size()]);
500         }
501         return result;
502     }
503 
504     /**
505      * Default implementation for transforming the results of a report query into a value object. This
506      * implementation exists for convenience reasons only. It needs only be overridden in the
507      * {@link ProgDepProgPrivDaoImpl} class if you intend to use reporting queries.
508      * @param row
509      * @return toProgDepProgPrivVO(this.toEntity(row))
510      * @see ProgDepProgPrivDao#toProgDepProgPrivVO(ProgDepProgPriv)
511      */
512     protected ProgDepProgPrivVO toProgDepProgPrivVO(Object[] row)
513     {
514         return this.toProgDepProgPrivVO(this.toEntity(row));
515     }
516 
517     /**
518      * This anonymous transformer is designed to transform entities or report query results
519      * (which result in an array of objects) to {@link ProgDepProgPrivVO}
520      * using the Jakarta Commons-Collections Transformation API.
521      */
522     private Transformer PROGDEPPROGPRIVVO_TRANSFORMER =
523         new Transformer()
524         {
525             public Object transform(Object input)
526             {
527                 Object result = null;
528                 if (input instanceof ProgDepProgPriv)
529                 {
530                     result = toProgDepProgPrivVO((ProgDepProgPriv)input);
531                 }
532                 else if (input instanceof Object[])
533                 {
534                     result = toProgDepProgPrivVO((Object[])input);
535                 }
536                 return result;
537             }
538         };
539 
540     /**
541      * {@inheritDoc}
542      */
543     @Override
544     public final void progDepProgPrivVOToEntityCollection(Collection<?> instances)
545     {
546         if (instances != null)
547         {
548             for (final Iterator<?> iterator = instances.iterator(); iterator.hasNext();)
549             {
550                 // - remove an objects that are null or not of the correct instance
551                 if (!(iterator.next() instanceof ProgDepProgPrivVO))
552                 {
553                     iterator.remove();
554                 }
555             }
556             CollectionUtils.transform(instances, this.ProgDepProgPrivVOToEntityTransformer);
557         }
558     }
559 
560     private final Transformer ProgDepProgPrivVOToEntityTransformer =
561         new Transformer()
562         {
563             public Object transform(Object input)
564             {
565                 return progDepProgPrivVOToEntity((ProgDepProgPrivVO)input);
566             }
567         };
568 
569 
570     /**
571      * {@inheritDoc}
572      */
573     @Override
574     public void toProgDepProgPrivVO(
575         ProgDepProgPriv source,
576         ProgDepProgPrivVO target)
577     {
578         target.setUpdateDt(source.getUpdateDt());
579     }
580 
581     /**
582      * {@inheritDoc}
583      */
584     @Override
585     public ProgDepProgPrivVO toProgDepProgPrivVO(final ProgDepProgPriv entity)
586     {
587         ProgDepProgPrivVO target = null;
588         if (entity != null)
589         {
590             target =  new ProgDepProgPrivVO();
591             this.toProgDepProgPrivVO(entity, target);
592         }
593         return target;
594     }
595 
596     /**
597      * {@inheritDoc}
598      */
599     @Override
600     public void progDepProgPrivVOToEntity(
601         ProgDepProgPrivVO source,
602         ProgDepProgPriv target,
603         boolean copyIfNull)
604     {
605         if (copyIfNull || source.getUpdateDt() != null)
606         {
607             target.setUpdateDt(source.getUpdateDt());
608         }
609     }
610 
611     /**
612      * Gets the current <code>principal</code> if one has been set,
613      * otherwise returns <code>null</code>.
614      *
615      * @return the current principal
616      */
617     protected Principal getPrincipal()
618     {
619         return PrincipalStore.get();
620     }
621 
622     /**
623      * {@inheritDoc}
624      */
625     @Override
626     @SuppressWarnings({ "unchecked" })
627     public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
628     {
629         try
630         {
631             search.setPageNumber(pageNumber);
632             search.setPageSize(pageSize);
633             final PropertySearch propertySearch = new PropertySearch(
634                 this.getSession(), ProgDepProgPrivImpl.class, search);
635             final List results = propertySearch.executeAsList();
636             this.transformEntities(transform, results);
637             return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
638         }
639         catch (HibernateException ex)
640         {
641             throw ex; /*super.convertHibernateAccessException(ex);*/
642         }
643     }
644 
645     /**
646      * {@inheritDoc}
647      */
648     @Override
649     public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
650     {
651         return this.search(ProgDepProgPrivDao.TRANSFORM_NONE, pageNumber, pageSize, search);
652     }
653 
654     /**
655      * {@inheritDoc}
656      */
657     @Override
658     public Set<?> search(final int transform, final Search search)
659     {
660         try
661         {
662             final PropertySearch propertySearch = new PropertySearch(
663                 this.getSession(), ProgDepProgPrivImpl.class, search);
664             final Set<?> results = propertySearch.executeAsSet();
665             this.transformEntities(transform, results);
666             return results;
667         }
668         catch (HibernateException ex)
669         {
670             throw ex; /*super.convertHibernateAccessException(ex);*/
671         }
672     }
673 
674     /**
675      * {@inheritDoc}
676      */
677     @Override
678     @SuppressWarnings("unchecked")
679     public Set<ProgDepProgPriv> search(final Search search)
680     {
681         return (Set<ProgDepProgPriv>) this.search(ProgDepProgPrivDao.TRANSFORM_NONE, search);
682     }
683 
684     /**
685      * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
686      * @param queryObject
687      * @param transform
688      * @param pageNumber
689      * @param pageSize
690      * @return PaginationResult
691      */
692     @SuppressWarnings({ "unchecked" })
693     protected PaginationResult getPaginationResult(
694         final Query queryObject,
695         final int transform, int pageNumber, int pageSize)
696     {
697         try
698         {
699             final ScrollableResults scrollableResults = queryObject.scroll();
700             scrollableResults.last();
701             int totalCount = scrollableResults.getRowNumber();
702             totalCount = totalCount >= 0 ? totalCount + 1 : 0;
703             if (pageNumber > 0 && pageSize > 0)
704             {
705                 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
706                 queryObject.setMaxResults(pageSize);
707             }
708             // Unchecked transformation because Set object is reused, cannot be strongly typed.
709             Set results = new LinkedHashSet(queryObject.list());
710             transformEntities(transform, results);
711             return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
712         }
713         catch (HibernateException ex)
714         {
715             throw ex; /*super.convertHibernateAccessException(ex);*/
716         }
717     }
718 
719     // spring-hibernate-dao-base merge-point
720 }