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