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