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