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