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.referential.QualityFlag;
035import fr.ifremer.adagio.core.dao.technical.hibernate.HibernateDaoSupport;
036import java.security.Principal;
037import java.sql.Timestamp;
038import java.util.Collection;
039import java.util.Date;
040import java.util.LinkedHashSet;
041import java.util.List;
042import java.util.Set;
043import javax.annotation.Resource;
044import org.andromda.spring.PaginationResult;
045import org.apache.commons.collections.CollectionUtils;
046import org.apache.commons.collections.Transformer;
047import org.hibernate.Criteria;
048import org.hibernate.HibernateException;
049import org.hibernate.Query;
050import org.hibernate.ScrollableResults;
051
052/**
053 * <p>
054 * Base Spring DAO Class: is able to create, update, remove, load, and find
055 * objects of type <code>CatchBatch</code>.
056 * </p>
057 *
058 * @see CatchBatch
059 */
060public abstract class CatchBatchDaoBase
061    extends HibernateDaoSupport    
062    implements CatchBatchDao
063{
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public Object get(final int transform, final Integer id)
069    {
070        if (id == null)
071        {
072            throw new IllegalArgumentException(
073                "CatchBatch.get - 'id' can not be null");
074        }
075        final CatchBatch entity = get(CatchBatchImpl.class, id);
076        return transformEntity(transform, entity);
077    }
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public CatchBatch get(Integer id)
083    {
084        return (CatchBatch)this.get(TRANSFORM_NONE, id);
085    }
086
087    /**
088     * {@inheritDoc}
089     */
090    @Override
091    public Object load(final int transform, final Integer id)
092    {
093        if (id == null)
094        {
095            throw new IllegalArgumentException(
096                "CatchBatch.load - 'id' can not be null");
097        }
098        final CatchBatch entity = get(CatchBatchImpl.class, id);
099        return transformEntity(transform, entity);
100    }
101
102    /**
103     * {@inheritDoc}
104     */
105    @Override
106    public CatchBatch load(Integer id)
107    {
108        return (CatchBatch)this.load(TRANSFORM_NONE, id);
109    }
110
111    /**
112     * {@inheritDoc}
113     */
114    @Override
115    @SuppressWarnings({"unchecked"})
116    public Collection<CatchBatch> loadAll()
117    {
118        return (Collection<CatchBatch>) this.loadAll(CatchBatchDao.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(CatchBatchDao.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(CatchBatchImpl.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 CatchBatch create(CatchBatch catchBatch)
184    {
185        return (CatchBatch)this.create(CatchBatchDao.TRANSFORM_NONE, catchBatch);
186    }
187
188    /**
189     * {@inheritDoc}
190     */
191    @Override
192    public Object create(final int transform, final CatchBatch catchBatch)
193    {
194        if (catchBatch == null)
195        {
196            throw new IllegalArgumentException(
197                "CatchBatch.create - 'catchBatch' can not be null");
198        }
199        this.getSessionFactory().getCurrentSession().save(catchBatch);
200        return this.transformEntity(transform, catchBatch);
201    }
202
203    /**
204     * {@inheritDoc}
205     */
206    @Override
207    @SuppressWarnings({"unchecked"})
208    public Collection<CatchBatch> create(final Collection<CatchBatch> entities)
209    {
210        return (Collection<CatchBatch>) create(CatchBatchDao.TRANSFORM_NONE, entities);
211    }
212
213    /**
214     * {@inheritDoc}
215     */
216    @Override
217    public Collection<?> create(final int transform, final Collection<CatchBatch> entities)
218    {
219        if (entities == null)
220        {
221            throw new IllegalArgumentException(
222                "CatchBatch.create - 'entities' can not be null");
223        }
224                    for (CatchBatch entity : entities)
225                    {
226                        create(transform, entity);
227                    }
228        return entities;
229    }
230
231    /**
232     * {@inheritDoc}
233     */
234    @Override
235    public CatchBatch create(
236        
237        Date controlDate,
238        Date validationDate,
239        Date qualificationDate,
240        String qualificationComments,
241        Timestamp updateDate,
242        Short rankOrder,
243        String label,
244        Float subgroupCount,
245        Integer individualCount,
246        Boolean childBatchsReplication,
247        Boolean exhaustiveInventory,
248        String comments)
249    {
250        return (CatchBatch)this.create(CatchBatchDao.TRANSFORM_NONE, controlDate, validationDate, qualificationDate, qualificationComments, updateDate, rankOrder, label, subgroupCount, individualCount, childBatchsReplication, exhaustiveInventory, comments);
251    }
252
253    /**
254     * {@inheritDoc}
255     */
256    @Override
257    public Object create(
258        final int transform,
259        
260        Date controlDate,
261        Date validationDate,
262        Date qualificationDate,
263        String qualificationComments,
264        Timestamp updateDate,
265        Short rankOrder,
266        String label,
267        Float subgroupCount,
268        Integer individualCount,
269        Boolean childBatchsReplication,
270        Boolean exhaustiveInventory,
271        String comments)
272    {
273        CatchBatch entity = new CatchBatchImpl();
274        
275        entity.setControlDate(controlDate);
276        entity.setValidationDate(validationDate);
277        entity.setQualificationDate(qualificationDate);
278        entity.setQualificationComments(qualificationComments);
279        entity.setUpdateDate(updateDate);
280        entity.setRankOrder(rankOrder);
281        entity.setLabel(label);
282        entity.setSubgroupCount(subgroupCount);
283        entity.setIndividualCount(individualCount);
284        entity.setChildBatchsReplication(childBatchsReplication);
285        entity.setExhaustiveInventory(exhaustiveInventory);
286        entity.setComments(comments);
287        return this.create(transform, entity);
288    }
289
290    /**
291     * {@inheritDoc}
292     */
293    @Override
294    public CatchBatch create(
295        Boolean childBatchsReplication,
296        Boolean exhaustiveInventory,
297        QualityFlag qualityFlag,
298        Short rankOrder,
299        
300        Timestamp updateDate)
301    {
302        return (CatchBatch)this.create(CatchBatchDao.TRANSFORM_NONE, childBatchsReplication, exhaustiveInventory, qualityFlag, rankOrder, updateDate);
303    }
304
305    /**
306     * {@inheritDoc}
307     */
308    @Override
309    public Object create(
310        final int transform,
311        Boolean childBatchsReplication,
312        Boolean exhaustiveInventory,
313        QualityFlag qualityFlag,
314        Short rankOrder,
315        
316        Timestamp updateDate)
317    {
318        CatchBatch entity = new CatchBatchImpl();
319        entity.setChildBatchsReplication(childBatchsReplication);
320        entity.setExhaustiveInventory(exhaustiveInventory);
321        entity.setQualityFlag(qualityFlag);
322        entity.setRankOrder(rankOrder);
323        
324        entity.setUpdateDate(updateDate);
325        return this.create(transform, entity);
326    }
327
328    /**
329     * {@inheritDoc}
330     */
331    @Override
332    public void update(CatchBatch catchBatch)
333    {
334        if (catchBatch == null)
335        {
336            throw new IllegalArgumentException(
337                "CatchBatch.update - 'catchBatch' can not be null");
338        }
339        this.getSessionFactory().getCurrentSession().update(catchBatch);
340    }
341
342    /**
343     * {@inheritDoc}
344     */
345    @Override
346    public void update(final Collection<CatchBatch> entities)
347    {
348        if (entities == null)
349        {
350            throw new IllegalArgumentException(
351                "CatchBatch.update - 'entities' can not be null");
352        }
353                    for (CatchBatch entity : entities)
354                    {
355                        update(entity);
356                    }
357    }
358
359    /**
360     * {@inheritDoc}
361     */
362    @Override
363    public void remove(CatchBatch catchBatch)
364    {
365        if (catchBatch == null)
366        {
367            throw new IllegalArgumentException(
368                "CatchBatch.remove - 'catchBatch' can not be null");
369        }
370        this.getSessionFactory().getCurrentSession().delete(catchBatch);
371    }
372
373    /**
374     * {@inheritDoc}
375     */
376    @Override
377    public void remove(Integer id)
378    {
379        if (id == null)
380        {
381            throw new IllegalArgumentException(
382                "CatchBatch.remove - 'id' can not be null");
383        }
384        CatchBatch entity = this.get(id);
385        if (entity != null)
386        {
387            this.remove(entity);
388        }
389    }
390
391    /**
392     * {@inheritDoc}
393     */
394    @Override
395    public void remove(Collection<CatchBatch> entities)
396    {
397        if (entities == null)
398        {
399            throw new IllegalArgumentException(
400                "CatchBatch.remove - 'entities' can not be null");
401        }
402        deleteAll(entities);
403    }
404    /**
405     * Allows transformation of entities into value objects
406     * (or something else for that matter), when the <code>transform</code>
407     * flag is set to one of the constants defined in <code>CatchBatchDao</code>, please note
408     * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
409     * will be returned.
410     *
411     * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
412     *
413     * @param transform one of the constants declared in {@link CatchBatchDao}
414     * @param entity an entity that was found
415     * @return the transformed entity (i.e. new value object, etc)
416     * @see CatchBatchDao#transformEntity(int, CatchBatch)
417     */
418    public Object transformEntity(final int transform, final CatchBatch entity)
419    {
420        Object target = null;
421        if (entity != null)
422        {
423            switch (transform)
424            {
425                case CatchBatchDao.TRANSFORM_NONE : // fall-through
426                default:
427                    target = entity;
428            }
429        }
430        return target;
431    }
432
433    /**
434     * {@inheritDoc}
435     */
436    @Override
437    public void transformEntities(final int transform, final Collection<?> entities)
438    {
439        switch (transform)
440        {
441            case CatchBatchDao.TRANSFORM_NONE : // fall-through
442                default:
443                // do nothing;
444        }
445    }
446
447    /**
448     * @see CatchBatchDao#toEntities(Collection)
449     */
450    public void toEntities(final Collection<?> results)
451    {
452        if (results != null)
453        {
454            CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
455        }
456    }
457
458    /**
459     * This anonymous transformer is designed to transform report query results
460     * (which result in an array of entities) to {@link CatchBatch}
461     * using the Jakarta Commons-Collections Transformation API.
462     */
463    private Transformer ENTITYTRANSFORMER =
464        new Transformer()
465        {
466            public Object transform(Object input)
467            {
468                Object result = null;
469                if (input instanceof Object[])
470                {
471                    result = toEntity((Object[])input);
472                }
473                else if (input instanceof CatchBatch)
474                {
475                    result = input;
476                }
477                return result;
478            }
479        };
480
481    /**
482     * @param row
483     * @return CatchBatch
484     */
485    protected CatchBatch toEntity(Object[] row)
486    {
487        CatchBatch target = null;
488        if (row != null)
489        {
490            final int numberOfObjects = row.length;
491            for (int ctr = 0; ctr < numberOfObjects; ctr++)
492            {
493                final Object object = row[ctr];
494                if (object instanceof CatchBatch)
495                {
496                    target = (CatchBatch)object;
497                    break;
498                }
499            }
500        }
501        return target;
502    }
503
504    /**
505     * Gets the current <code>principal</code> if one has been set,
506     * otherwise returns <code>null</code>.
507     *
508     * @return the current principal
509     */
510    protected Principal getPrincipal()
511    {
512        return PrincipalStore.get();
513    }
514
515    /**
516     * {@inheritDoc}
517     */
518    @Override
519    @SuppressWarnings({ "unchecked" })
520    public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
521    {
522        try
523        {
524            search.setPageNumber(pageNumber);
525            search.setPageSize(pageSize);
526            final PropertySearch propertySearch = new PropertySearch(
527                this.getSession(), CatchBatchImpl.class, search);
528            final List results = propertySearch.executeAsList();
529            this.transformEntities(transform, results);
530            return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
531        }
532        catch (HibernateException ex)
533        {
534            throw ex; /*super.convertHibernateAccessException(ex);*/
535        }
536    }
537
538    /**
539     * {@inheritDoc}
540     */
541    @Override
542    public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
543    {
544        return this.search(CatchBatchDao.TRANSFORM_NONE, pageNumber, pageSize, search);
545    }
546
547    /**
548     * {@inheritDoc}
549     */
550    @Override
551    public Set<?> search(final int transform, final Search search)
552    {
553        try
554        {
555            final PropertySearch propertySearch = new PropertySearch(
556                this.getSession(), CatchBatchImpl.class, search);
557            final Set<?> results = propertySearch.executeAsSet();
558            this.transformEntities(transform, results);
559            return results;
560        }
561        catch (HibernateException ex)
562        {
563            throw ex; /*super.convertHibernateAccessException(ex);*/
564        }
565    }
566
567    /**
568     * {@inheritDoc}
569     */
570    @Override
571    @SuppressWarnings("unchecked")
572    public Set<CatchBatch> search(final Search search)
573    {
574        return (Set<CatchBatch>) this.search(CatchBatchDao.TRANSFORM_NONE, search);
575    }
576
577    /**
578     * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
579     * @param queryObject
580     * @param transform
581     * @param pageNumber
582     * @param pageSize
583     * @return PaginationResult
584     */
585    @SuppressWarnings({ "unchecked" })
586    protected PaginationResult getPaginationResult(
587        final Query queryObject,
588        final int transform, int pageNumber, int pageSize)
589    {
590        try
591        {
592            final ScrollableResults scrollableResults = queryObject.scroll();
593            scrollableResults.last();
594            int totalCount = scrollableResults.getRowNumber();
595            totalCount = totalCount >= 0 ? totalCount + 1 : 0;
596            if (pageNumber > 0 && pageSize > 0)
597            {
598                queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
599                queryObject.setMaxResults(pageSize);
600            }
601            // Unchecked transformation because Set object is reused, cannot be strongly typed.
602            Set results = new LinkedHashSet(queryObject.list());
603            transformEntities(transform, results);
604            return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
605        }
606        catch (HibernateException ex)
607        {
608            throw ex; /*super.convertHibernateAccessException(ex);*/
609        }
610    }
611
612    // spring-hibernate-dao-base merge-point
613}