001// license-header java merge-point
002//
003// Attention: Generated code! Do not modify by hand!
004// Generated by: hibernate/SpringHibernateDaoBase.vsl in <project>/mda/src/main/cartridge/custom/...
005//
006package fr.ifremer.adagio.core.dao.data.batch;
007
008/*
009 * #%L
010 * SIH-Adagio :: Core
011 * $Id:$
012 * $HeadURL:$
013 * %%
014 * Copyright (C) 2012 - 2014 Ifremer
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Affero General Public License as published by
018 * the Free Software Foundation, either version 3 of the License, or
019 * (at your option) any later version.
020 * 
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Affero General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * #L%
029 */
030
031import fr.ifremer.adagio.core.dao.PrincipalStore;
032import fr.ifremer.adagio.core.dao.PropertySearch;
033import fr.ifremer.adagio.core.dao.Search;
034import fr.ifremer.adagio.core.dao.administration.user.Department;
035import fr.ifremer.adagio.core.dao.technical.hibernate.HibernateDaoSupport;
036import java.security.Principal;
037import java.sql.Timestamp;
038import java.util.Collection;
039import java.util.LinkedHashSet;
040import java.util.List;
041import java.util.Set;
042import javax.annotation.Resource;
043import org.andromda.spring.PaginationResult;
044import org.apache.commons.collections.CollectionUtils;
045import org.apache.commons.collections.Transformer;
046import org.hibernate.Criteria;
047import org.hibernate.HibernateException;
048import org.hibernate.Query;
049import org.hibernate.ScrollableResults;
050
051/**
052 * <p>
053 * Base Spring DAO Class: is able to create, update, remove, load, and find
054 * objects of type <code>BatchModel</code>.
055 * </p>
056 *
057 * @see BatchModel
058 */
059public abstract class BatchModelDaoBase
060    extends HibernateDaoSupport    
061    implements BatchModelDao
062{
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public Object get(final int transform, final Integer id)
068    {
069        if (id == null)
070        {
071            throw new IllegalArgumentException(
072                "BatchModel.get - 'id' can not be null");
073        }
074        final BatchModel entity = get(BatchModelImpl.class, id);
075        return transformEntity(transform, entity);
076    }
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public BatchModel get(Integer id)
082    {
083        return (BatchModel)this.get(TRANSFORM_NONE, id);
084    }
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public Object load(final int transform, final Integer id)
091    {
092        if (id == null)
093        {
094            throw new IllegalArgumentException(
095                "BatchModel.load - 'id' can not be null");
096        }
097        final BatchModel entity = get(BatchModelImpl.class, id);
098        return transformEntity(transform, entity);
099    }
100
101    /**
102     * {@inheritDoc}
103     */
104    @Override
105    public BatchModel load(Integer id)
106    {
107        return (BatchModel)this.load(TRANSFORM_NONE, id);
108    }
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    @SuppressWarnings({"unchecked"})
115    public Collection<BatchModel> loadAll()
116    {
117        return (Collection<BatchModel>) this.loadAll(BatchModelDao.TRANSFORM_NONE);
118    }
119
120    /**
121     * {@inheritDoc}
122     */
123    @Override
124    public Collection<?> loadAll(final int transform)
125    {
126        return this.loadAll(transform, -1, -1);
127    }
128
129    /**
130     * {@inheritDoc}
131     */
132    @Override
133    public Collection<?> loadAll(final int pageNumber, final int pageSize)
134    {
135        return this.loadAll(BatchModelDao.TRANSFORM_NONE, pageNumber, pageSize);
136    }
137
138    /**
139     * {@inheritDoc}
140     */
141    @Override
142    public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
143    {
144        try
145        {
146            final Criteria criteria = this.getSession().createCriteria(BatchModelImpl.class);
147            if (pageNumber > 0 && pageSize > 0)
148            {
149                criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
150                criteria.setMaxResults(pageSize);
151            }
152            final Collection<?> results = criteria.list();
153            this.transformEntities(transform, results);
154            return results;
155        }
156        catch (HibernateException ex)
157        {
158            throw ex;
159        }
160    }
161
162    /**
163     * firstResult = (pageNumber - 1) * pageSize
164     * @param pageNumber
165     * @param pageSize
166     * @return firstResult
167     */
168    protected int calculateFirstResult(int pageNumber, int pageSize)
169    {
170        int firstResult = 0;
171        if (pageNumber > 0)
172        {
173            firstResult = (pageNumber - 1) * pageSize;
174        }
175        return firstResult;
176    }
177
178    /**
179     * {@inheritDoc}
180     */
181    @Override
182    public BatchModel create(BatchModel batchModel)
183    {
184        return (BatchModel)this.create(BatchModelDao.TRANSFORM_NONE, batchModel);
185    }
186
187    /**
188     * {@inheritDoc}
189     */
190    @Override
191    public Object create(final int transform, final BatchModel batchModel)
192    {
193        if (batchModel == null)
194        {
195            throw new IllegalArgumentException(
196                "BatchModel.create - 'batchModel' can not be null");
197        }
198        this.getSessionFactory().getCurrentSession().save(batchModel);
199        return this.transformEntity(transform, batchModel);
200    }
201
202    /**
203     * {@inheritDoc}
204     */
205    @Override
206    @SuppressWarnings({"unchecked"})
207    public Collection<BatchModel> create(final Collection<BatchModel> entities)
208    {
209        return (Collection<BatchModel>) create(BatchModelDao.TRANSFORM_NONE, entities);
210    }
211
212    /**
213     * {@inheritDoc}
214     */
215    @Override
216    public Collection<?> create(final int transform, final Collection<BatchModel> entities)
217    {
218        if (entities == null)
219        {
220            throw new IllegalArgumentException(
221                "BatchModel.create - 'entities' can not be null");
222        }
223                    for (BatchModel entity : entities)
224                    {
225                        create(transform, entity);
226                    }
227        return entities;
228    }
229
230    /**
231     * {@inheritDoc}
232     */
233    @Override
234    public BatchModel create(
235        String name,
236        Boolean isGlobal,
237        Timestamp updateDate)
238    {
239        return (BatchModel)this.create(BatchModelDao.TRANSFORM_NONE, name, isGlobal, updateDate);
240    }
241
242    /**
243     * {@inheritDoc}
244     */
245    @Override
246    public Object create(
247        final int transform,
248        String name,
249        Boolean isGlobal,
250        Timestamp updateDate)
251    {
252        BatchModel entity = new BatchModelImpl();
253        entity.setName(name);
254        entity.setIsGlobal(isGlobal);
255        entity.setUpdateDate(updateDate);
256        return this.create(transform, entity);
257    }
258
259    /**
260     * {@inheritDoc}
261     */
262    @Override
263    public BatchModel create(
264        CatchBatch catchBatch,
265        Boolean isGlobal,
266        String name,
267        Department recorderDepartment,
268        Timestamp updateDate)
269    {
270        return (BatchModel)this.create(BatchModelDao.TRANSFORM_NONE, catchBatch, isGlobal, name, recorderDepartment, updateDate);
271    }
272
273    /**
274     * {@inheritDoc}
275     */
276    @Override
277    public Object create(
278        final int transform,
279        CatchBatch catchBatch,
280        Boolean isGlobal,
281        String name,
282        Department recorderDepartment,
283        Timestamp updateDate)
284    {
285        BatchModel entity = new BatchModelImpl();
286        entity.setCatchBatch(catchBatch);
287        entity.setIsGlobal(isGlobal);
288        entity.setName(name);
289        entity.setRecorderDepartment(recorderDepartment);
290        entity.setUpdateDate(updateDate);
291        return this.create(transform, entity);
292    }
293
294    /**
295     * {@inheritDoc}
296     */
297    @Override
298    public void update(BatchModel batchModel)
299    {
300        if (batchModel == null)
301        {
302            throw new IllegalArgumentException(
303                "BatchModel.update - 'batchModel' can not be null");
304        }
305        this.getSessionFactory().getCurrentSession().update(batchModel);
306    }
307
308    /**
309     * {@inheritDoc}
310     */
311    @Override
312    public void update(final Collection<BatchModel> entities)
313    {
314        if (entities == null)
315        {
316            throw new IllegalArgumentException(
317                "BatchModel.update - 'entities' can not be null");
318        }
319                    for (BatchModel entity : entities)
320                    {
321                        update(entity);
322                    }
323    }
324
325    /**
326     * {@inheritDoc}
327     */
328    @Override
329    public void remove(BatchModel batchModel)
330    {
331        if (batchModel == null)
332        {
333            throw new IllegalArgumentException(
334                "BatchModel.remove - 'batchModel' can not be null");
335        }
336        this.getSessionFactory().getCurrentSession().delete(batchModel);
337    }
338
339    /**
340     * {@inheritDoc}
341     */
342    @Override
343    public void remove(Integer id)
344    {
345        if (id == null)
346        {
347            throw new IllegalArgumentException(
348                "BatchModel.remove - 'id' can not be null");
349        }
350        BatchModel entity = this.get(id);
351        if (entity != null)
352        {
353            this.remove(entity);
354        }
355    }
356
357    /**
358     * {@inheritDoc}
359     */
360    @Override
361    public void remove(Collection<BatchModel> entities)
362    {
363        if (entities == null)
364        {
365            throw new IllegalArgumentException(
366                "BatchModel.remove - 'entities' can not be null");
367        }
368        deleteAll(entities);
369    }
370    /**
371     * Allows transformation of entities into value objects
372     * (or something else for that matter), when the <code>transform</code>
373     * flag is set to one of the constants defined in <code>BatchModelDao</code>, please note
374     * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
375     * will be returned.
376     *
377     * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
378     *
379     * @param transform one of the constants declared in {@link BatchModelDao}
380     * @param entity an entity that was found
381     * @return the transformed entity (i.e. new value object, etc)
382     * @see BatchModelDao#transformEntity(int, BatchModel)
383     */
384    public Object transformEntity(final int transform, final BatchModel entity)
385    {
386        Object target = null;
387        if (entity != null)
388        {
389            switch (transform)
390            {
391                case BatchModelDao.TRANSFORM_NONE : // fall-through
392                default:
393                    target = entity;
394            }
395        }
396        return target;
397    }
398
399    /**
400     * {@inheritDoc}
401     */
402    @Override
403    public void transformEntities(final int transform, final Collection<?> entities)
404    {
405        switch (transform)
406        {
407            case BatchModelDao.TRANSFORM_NONE : // fall-through
408                default:
409                // do nothing;
410        }
411    }
412
413    /**
414     * @see BatchModelDao#toEntities(Collection)
415     */
416    public void toEntities(final Collection<?> results)
417    {
418        if (results != null)
419        {
420            CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
421        }
422    }
423
424    /**
425     * This anonymous transformer is designed to transform report query results
426     * (which result in an array of entities) to {@link BatchModel}
427     * using the Jakarta Commons-Collections Transformation API.
428     */
429    private Transformer ENTITYTRANSFORMER =
430        new Transformer()
431        {
432            public Object transform(Object input)
433            {
434                Object result = null;
435                if (input instanceof Object[])
436                {
437                    result = toEntity((Object[])input);
438                }
439                else if (input instanceof BatchModel)
440                {
441                    result = input;
442                }
443                return result;
444            }
445        };
446
447    /**
448     * @param row
449     * @return BatchModel
450     */
451    protected BatchModel toEntity(Object[] row)
452    {
453        BatchModel target = null;
454        if (row != null)
455        {
456            final int numberOfObjects = row.length;
457            for (int ctr = 0; ctr < numberOfObjects; ctr++)
458            {
459                final Object object = row[ctr];
460                if (object instanceof BatchModel)
461                {
462                    target = (BatchModel)object;
463                    break;
464                }
465            }
466        }
467        return target;
468    }
469
470    /**
471     * Gets the current <code>principal</code> if one has been set,
472     * otherwise returns <code>null</code>.
473     *
474     * @return the current principal
475     */
476    protected Principal getPrincipal()
477    {
478        return PrincipalStore.get();
479    }
480
481    /**
482     * {@inheritDoc}
483     */
484    @Override
485    @SuppressWarnings({ "unchecked" })
486    public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
487    {
488        try
489        {
490            search.setPageNumber(pageNumber);
491            search.setPageSize(pageSize);
492            final PropertySearch propertySearch = new PropertySearch(
493                this.getSession(), BatchModelImpl.class, search);
494            final List results = propertySearch.executeAsList();
495            this.transformEntities(transform, results);
496            return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
497        }
498        catch (HibernateException ex)
499        {
500            throw ex; /*super.convertHibernateAccessException(ex);*/
501        }
502    }
503
504    /**
505     * {@inheritDoc}
506     */
507    @Override
508    public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
509    {
510        return this.search(BatchModelDao.TRANSFORM_NONE, pageNumber, pageSize, search);
511    }
512
513    /**
514     * {@inheritDoc}
515     */
516    @Override
517    public Set<?> search(final int transform, final Search search)
518    {
519        try
520        {
521            final PropertySearch propertySearch = new PropertySearch(
522                this.getSession(), BatchModelImpl.class, search);
523            final Set<?> results = propertySearch.executeAsSet();
524            this.transformEntities(transform, results);
525            return results;
526        }
527        catch (HibernateException ex)
528        {
529            throw ex; /*super.convertHibernateAccessException(ex);*/
530        }
531    }
532
533    /**
534     * {@inheritDoc}
535     */
536    @Override
537    @SuppressWarnings("unchecked")
538    public Set<BatchModel> search(final Search search)
539    {
540        return (Set<BatchModel>) this.search(BatchModelDao.TRANSFORM_NONE, search);
541    }
542
543    /**
544     * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
545     * @param queryObject
546     * @param transform
547     * @param pageNumber
548     * @param pageSize
549     * @return PaginationResult
550     */
551    @SuppressWarnings({ "unchecked" })
552    protected PaginationResult getPaginationResult(
553        final Query queryObject,
554        final int transform, int pageNumber, int pageSize)
555    {
556        try
557        {
558            final ScrollableResults scrollableResults = queryObject.scroll();
559            scrollableResults.last();
560            int totalCount = scrollableResults.getRowNumber();
561            totalCount = totalCount >= 0 ? totalCount + 1 : 0;
562            if (pageNumber > 0 && pageSize > 0)
563            {
564                queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
565                queryObject.setMaxResults(pageSize);
566            }
567            // Unchecked transformation because Set object is reused, cannot be strongly typed.
568            Set results = new LinkedHashSet(queryObject.list());
569            transformEntities(transform, results);
570            return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
571        }
572        catch (HibernateException ex)
573        {
574            throw ex; /*super.convertHibernateAccessException(ex);*/
575        }
576    }
577
578    // spring-hibernate-dao-base merge-point
579}