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