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