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