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