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