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