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