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