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.sandre;
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.technical.hibernate.HibernateDaoSupport;
33  import java.security.Principal;
34  import java.util.Collection;
35  import java.util.LinkedHashSet;
36  import java.util.List;
37  import java.util.Set;
38  import javax.annotation.Resource;
39  import org.andromda.spring.PaginationResult;
40  import org.apache.commons.collections.CollectionUtils;
41  import org.apache.commons.collections.Transformer;
42  import org.hibernate.Criteria;
43  import org.hibernate.HibernateException;
44  import org.hibernate.Query;
45  import org.hibernate.ScrollableResults;
46  
47  /**
48   * <p>
49   * Base Spring DAO Class: is able to create, update, remove, load, and find
50   * objects of type <code>SandreBaliseEq</code>.
51   * </p>
52   *
53   * @see SandreBaliseEq
54   */
55  public abstract class SandreBaliseEqDaoBase
56      extends HibernateDaoSupport    
57      implements SandreBaliseEqDao
58  {
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public Object get(final int transform, final Integer sandreBalEqId)
64      {
65          if (sandreBalEqId == null)
66          {
67              throw new IllegalArgumentException(
68                  "SandreBaliseEq.get - 'sandreBalEqId' can not be null");
69          }
70          final SandreBaliseEq entity = get(SandreBaliseEqImpl.class, sandreBalEqId);
71          return transformEntity(transform, entity);
72      }
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public SandreBaliseEq get(Integer sandreBalEqId)
78      {
79          return (SandreBaliseEq)this.get(TRANSFORM_NONE, sandreBalEqId);
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public Object load(final int transform, final Integer sandreBalEqId)
87      {
88          if (sandreBalEqId == null)
89          {
90              throw new IllegalArgumentException(
91                  "SandreBaliseEq.load - 'sandreBalEqId' can not be null");
92          }
93          final SandreBaliseEq entity = get(SandreBaliseEqImpl.class, sandreBalEqId);
94          return transformEntity(transform, entity);
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public SandreBaliseEq load(Integer sandreBalEqId)
102     {
103         return (SandreBaliseEq)this.load(TRANSFORM_NONE, sandreBalEqId);
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     @SuppressWarnings({"unchecked"})
111     public Collection<SandreBaliseEq> loadAll()
112     {
113         return (Collection<SandreBaliseEq>) this.loadAll(SandreBaliseEqDao.TRANSFORM_NONE);
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public Collection<?> loadAll(final int transform)
121     {
122         return this.loadAll(transform, -1, -1);
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public Collection<?> loadAll(final int pageNumber, final int pageSize)
130     {
131         return this.loadAll(SandreBaliseEqDao.TRANSFORM_NONE, pageNumber, pageSize);
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
139     {
140         try
141         {
142             final Criteria criteria = this.getSession().createCriteria(SandreBaliseEqImpl.class);
143             if (pageNumber > 0 && pageSize > 0)
144             {
145                 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
146                 criteria.setMaxResults(pageSize);
147             }
148             final Collection<?> results = criteria.list();
149             this.transformEntities(transform, results);
150             return results;
151         }
152         catch (HibernateException ex)
153         {
154             throw ex;
155         }
156     }
157 
158     /**
159      * firstResult = (pageNumber - 1) * pageSize
160      * @param pageNumber
161      * @param pageSize
162      * @return firstResult
163      */
164     protected int calculateFirstResult(int pageNumber, int pageSize)
165     {
166         int firstResult = 0;
167         if (pageNumber > 0)
168         {
169             firstResult = (pageNumber - 1) * pageSize;
170         }
171         return firstResult;
172     }
173 
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     public SandreBaliseEq create(SandreBaliseEq sandreBaliseEq)
179     {
180         return (SandreBaliseEq)this.create(SandreBaliseEqDao.TRANSFORM_NONE, sandreBaliseEq);
181     }
182 
183     /**
184      * {@inheritDoc}
185      */
186     @Override
187     public Object create(final int transform, final SandreBaliseEq sandreBaliseEq)
188     {
189         if (sandreBaliseEq == null)
190         {
191             throw new IllegalArgumentException(
192                 "SandreBaliseEq.create - 'sandreBaliseEq' can not be null");
193         }
194         this.getSessionFactory().getCurrentSession().save(sandreBaliseEq);
195         return this.transformEntity(transform, sandreBaliseEq);
196     }
197 
198     /**
199      * {@inheritDoc}
200      */
201     @Override
202     @SuppressWarnings({"unchecked"})
203     public Collection<SandreBaliseEq> create(final Collection<SandreBaliseEq> entities)
204     {
205         return (Collection<SandreBaliseEq>) create(SandreBaliseEqDao.TRANSFORM_NONE, entities);
206     }
207 
208     /**
209      * {@inheritDoc}
210      */
211     @Override
212     public Collection<?> create(final int transform, final Collection<SandreBaliseEq> entities)
213     {
214         if (entities == null)
215         {
216             throw new IllegalArgumentException(
217                 "SandreBaliseEq.create - 'entities' can not be null");
218         }
219                     for (SandreBaliseEq entity : entities)
220                     {
221                         create(transform, entity);
222                     }
223         return entities;
224     }
225 
226     /**
227      * {@inheritDoc}
228      */
229     @Override
230     public SandreBaliseEq create(
231         String sandreBalEqTableImportNm,
232         String sandreBalEqAttrImportNm,
233         String sandreBalEqTableMandatory,
234         String sandreBalEqTableCd,
235         String sandreBalEqTableExportNm,
236         String sandreBalEqAttrExportNm)
237     {
238         return (SandreBaliseEq)this.create(SandreBaliseEqDao.TRANSFORM_NONE, sandreBalEqTableImportNm, sandreBalEqAttrImportNm, sandreBalEqTableMandatory, sandreBalEqTableCd, sandreBalEqTableExportNm, sandreBalEqAttrExportNm);
239     }
240 
241     /**
242      * {@inheritDoc}
243      */
244     @Override
245     public Object create(
246         final int transform,
247         String sandreBalEqTableImportNm,
248         String sandreBalEqAttrImportNm,
249         String sandreBalEqTableMandatory,
250         String sandreBalEqTableCd,
251         String sandreBalEqTableExportNm,
252         String sandreBalEqAttrExportNm)
253     {
254         SandreBaliseEq entity = new SandreBaliseEqImpl();
255         entity.setSandreBalEqTableImportNm(sandreBalEqTableImportNm);
256         entity.setSandreBalEqAttrImportNm(sandreBalEqAttrImportNm);
257         entity.setSandreBalEqTableMandatory(sandreBalEqTableMandatory);
258         entity.setSandreBalEqTableCd(sandreBalEqTableCd);
259         entity.setSandreBalEqTableExportNm(sandreBalEqTableExportNm);
260         entity.setSandreBalEqAttrExportNm(sandreBalEqAttrExportNm);
261         return this.create(transform, entity);
262     }
263 
264     /**
265      * {@inheritDoc}
266      */
267     @Override
268     public SandreBaliseEq create(
269         String sandreBalEqTableCd,
270         String sandreBalEqTableMandatory,
271         SandreXmlBalise sandreXmlUpperBalId)
272     {
273         return (SandreBaliseEq)this.create(SandreBaliseEqDao.TRANSFORM_NONE, sandreBalEqTableCd, sandreBalEqTableMandatory, sandreXmlUpperBalId);
274     }
275 
276     /**
277      * {@inheritDoc}
278      */
279     @Override
280     public Object create(
281         final int transform,
282         String sandreBalEqTableCd,
283         String sandreBalEqTableMandatory,
284         SandreXmlBalise sandreXmlUpperBalId)
285     {
286         SandreBaliseEq entity = new SandreBaliseEqImpl();
287         entity.setSandreBalEqTableCd(sandreBalEqTableCd);
288         entity.setSandreBalEqTableMandatory(sandreBalEqTableMandatory);
289         entity.setSandreXmlUpperBalId(sandreXmlUpperBalId);
290         return this.create(transform, entity);
291     }
292 
293     /**
294      * {@inheritDoc}
295      */
296     @Override
297     public void update(SandreBaliseEq sandreBaliseEq)
298     {
299         if (sandreBaliseEq == null)
300         {
301             throw new IllegalArgumentException(
302                 "SandreBaliseEq.update - 'sandreBaliseEq' can not be null");
303         }
304         this.getSessionFactory().getCurrentSession().update(sandreBaliseEq);
305     }
306 
307     /**
308      * {@inheritDoc}
309      */
310     @Override
311     public void update(final Collection<SandreBaliseEq> entities)
312     {
313         if (entities == null)
314         {
315             throw new IllegalArgumentException(
316                 "SandreBaliseEq.update - 'entities' can not be null");
317         }
318                     for (SandreBaliseEq entity : entities)
319                     {
320                         update(entity);
321                     }
322     }
323 
324     /**
325      * {@inheritDoc}
326      */
327     @Override
328     public void remove(SandreBaliseEq sandreBaliseEq)
329     {
330         if (sandreBaliseEq == null)
331         {
332             throw new IllegalArgumentException(
333                 "SandreBaliseEq.remove - 'sandreBaliseEq' can not be null");
334         }
335         this.getSessionFactory().getCurrentSession().delete(sandreBaliseEq);
336     }
337 
338     /**
339      * {@inheritDoc}
340      */
341     @Override
342     public void remove(Integer sandreBalEqId)
343     {
344         if (sandreBalEqId == null)
345         {
346             throw new IllegalArgumentException(
347                 "SandreBaliseEq.remove - 'sandreBalEqId' can not be null");
348         }
349         SandreBaliseEq entity = this.get(sandreBalEqId);
350         if (entity != null)
351         {
352             this.remove(entity);
353         }
354     }
355 
356     /**
357      * {@inheritDoc}
358      */
359     @Override
360     public void remove(Collection<SandreBaliseEq> entities)
361     {
362         if (entities == null)
363         {
364             throw new IllegalArgumentException(
365                 "SandreBaliseEq.remove - 'entities' can not be null");
366         }
367         deleteAll(entities);
368     }
369     /**
370      * Allows transformation of entities into value objects
371      * (or something else for that matter), when the <code>transform</code>
372      * flag is set to one of the constants defined in <code>SandreBaliseEqDao</code>, please note
373      * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
374      * will be returned.
375      *
376      * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
377      *
378      * @param transform one of the constants declared in {@link SandreBaliseEqDao}
379      * @param entity an entity that was found
380      * @return the transformed entity (i.e. new value object, etc)
381      * @see SandreBaliseEqDao#transformEntity(int, SandreBaliseEq)
382      */
383     public Object transformEntity(final int transform, final SandreBaliseEq entity)
384     {
385         Object target = null;
386         if (entity != null)
387         {
388             switch (transform)
389             {
390                 case SandreBaliseEqDao.TRANSFORM_NONE : // fall-through
391                 default:
392                     target = entity;
393             }
394         }
395         return target;
396     }
397 
398     /**
399      * {@inheritDoc}
400      */
401     @Override
402     public void transformEntities(final int transform, final Collection<?> entities)
403     {
404         switch (transform)
405         {
406             case SandreBaliseEqDao.TRANSFORM_NONE : // fall-through
407                 default:
408                 // do nothing;
409         }
410     }
411 
412     /**
413      * @see SandreBaliseEqDao#toEntities(Collection)
414      */
415     public void toEntities(final Collection<?> results)
416     {
417         if (results != null)
418         {
419             CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
420         }
421     }
422 
423     /**
424      * This anonymous transformer is designed to transform report query results
425      * (which result in an array of entities) to {@link SandreBaliseEq}
426      * using the Jakarta Commons-Collections Transformation API.
427      */
428     private Transformer ENTITYTRANSFORMER =
429         new Transformer()
430         {
431             public Object transform(Object input)
432             {
433                 Object result = null;
434                 if (input instanceof Object[])
435                 {
436                     result = toEntity((Object[])input);
437                 }
438                 else if (input instanceof SandreBaliseEq)
439                 {
440                     result = input;
441                 }
442                 return result;
443             }
444         };
445 
446     /**
447      * @param row
448      * @return SandreBaliseEq
449      */
450     protected SandreBaliseEq toEntity(Object[] row)
451     {
452         SandreBaliseEq target = null;
453         if (row != null)
454         {
455             final int numberOfObjects = row.length;
456             for (int ctr = 0; ctr < numberOfObjects; ctr++)
457             {
458                 final Object object = row[ctr];
459                 if (object instanceof SandreBaliseEq)
460                 {
461                     target = (SandreBaliseEq)object;
462                     break;
463                 }
464             }
465         }
466         return target;
467     }
468 
469     /**
470      * Gets the current <code>principal</code> if one has been set,
471      * otherwise returns <code>null</code>.
472      *
473      * @return the current principal
474      */
475     protected Principal getPrincipal()
476     {
477         return PrincipalStore.get();
478     }
479 
480     /**
481      * {@inheritDoc}
482      */
483     @Override
484     @SuppressWarnings({ "unchecked" })
485     public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
486     {
487         try
488         {
489             search.setPageNumber(pageNumber);
490             search.setPageSize(pageSize);
491             final PropertySearch propertySearch = new PropertySearch(
492                 this.getSession(), SandreBaliseEqImpl.class, search);
493             final List results = propertySearch.executeAsList();
494             this.transformEntities(transform, results);
495             return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
496         }
497         catch (HibernateException ex)
498         {
499             throw ex; /*super.convertHibernateAccessException(ex);*/
500         }
501     }
502 
503     /**
504      * {@inheritDoc}
505      */
506     @Override
507     public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
508     {
509         return this.search(SandreBaliseEqDao.TRANSFORM_NONE, pageNumber, pageSize, search);
510     }
511 
512     /**
513      * {@inheritDoc}
514      */
515     @Override
516     public Set<?> search(final int transform, final Search search)
517     {
518         try
519         {
520             final PropertySearch propertySearch = new PropertySearch(
521                 this.getSession(), SandreBaliseEqImpl.class, search);
522             final Set<?> results = propertySearch.executeAsSet();
523             this.transformEntities(transform, results);
524             return results;
525         }
526         catch (HibernateException ex)
527         {
528             throw ex; /*super.convertHibernateAccessException(ex);*/
529         }
530     }
531 
532     /**
533      * {@inheritDoc}
534      */
535     @Override
536     @SuppressWarnings("unchecked")
537     public Set<SandreBaliseEq> search(final Search search)
538     {
539         return (Set<SandreBaliseEq>) this.search(SandreBaliseEqDao.TRANSFORM_NONE, search);
540     }
541 
542     /**
543      * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
544      * @param queryObject
545      * @param transform
546      * @param pageNumber
547      * @param pageSize
548      * @return PaginationResult
549      */
550     @SuppressWarnings({ "unchecked" })
551     protected PaginationResult getPaginationResult(
552         final Query queryObject,
553         final int transform, int pageNumber, int pageSize)
554     {
555         try
556         {
557             final ScrollableResults scrollableResults = queryObject.scroll();
558             scrollableResults.last();
559             int totalCount = scrollableResults.getRowNumber();
560             totalCount = totalCount >= 0 ? totalCount + 1 : 0;
561             if (pageNumber > 0 && pageSize > 0)
562             {
563                 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
564                 queryObject.setMaxResults(pageSize);
565             }
566             // Unchecked transformation because Set object is reused, cannot be strongly typed.
567             Set results = new LinkedHashSet(queryObject.list());
568             transformEntities(transform, results);
569             return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
570         }
571         catch (HibernateException ex)
572         {
573             throw ex; /*super.convertHibernateAccessException(ex);*/
574         }
575     }
576 
577     // spring-hibernate-dao-base merge-point
578 }