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