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