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.Status;
035import fr.ifremer.adagio.core.dao.referential.location.Location;
036import fr.ifremer.adagio.core.dao.referential.pmfm.Parameter;
037import fr.ifremer.adagio.core.dao.referential.pmfm.Unit;
038import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxon;
039import fr.ifremer.adagio.core.dao.technical.hibernate.HibernateDaoSupport;
040import java.security.Principal;
041import java.sql.Timestamp;
042import java.util.Collection;
043import java.util.LinkedHashSet;
044import java.util.List;
045import java.util.Set;
046import javax.annotation.Resource;
047import org.andromda.spring.PaginationResult;
048import org.apache.commons.collections.CollectionUtils;
049import org.apache.commons.collections.Transformer;
050import org.hibernate.Criteria;
051import org.hibernate.HibernateException;
052import org.hibernate.Query;
053import org.hibernate.ScrollableResults;
054
055/**
056 * <p>
057 * Base Spring DAO Class: is able to create, update, remove, load, and find
058 * objects of type <code>WeightLengthConversion</code>.
059 * </p>
060 *
061 * @see WeightLengthConversion
062 */
063public abstract class WeightLengthConversionDaoBase
064    extends HibernateDaoSupport    
065    implements WeightLengthConversionDao
066{
067    /**
068     * {@inheritDoc}
069     */
070    @Override
071    public Object get(final int transform, final Integer id)
072    {
073        if (id == null)
074        {
075            throw new IllegalArgumentException(
076                "WeightLengthConversion.get - 'id' can not be null");
077        }
078        final WeightLengthConversion entity = get(WeightLengthConversionImpl.class, id);
079        return transformEntity(transform, entity);
080    }
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public WeightLengthConversion get(Integer id)
086    {
087        return (WeightLengthConversion)this.get(TRANSFORM_NONE, id);
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public Object load(final int transform, final Integer id)
095    {
096        if (id == null)
097        {
098            throw new IllegalArgumentException(
099                "WeightLengthConversion.load - 'id' can not be null");
100        }
101        final WeightLengthConversion entity = get(WeightLengthConversionImpl.class, id);
102        return transformEntity(transform, entity);
103    }
104
105    /**
106     * {@inheritDoc}
107     */
108    @Override
109    public WeightLengthConversion load(Integer id)
110    {
111        return (WeightLengthConversion)this.load(TRANSFORM_NONE, id);
112    }
113
114    /**
115     * {@inheritDoc}
116     */
117    @Override
118    @SuppressWarnings({"unchecked"})
119    public Collection<WeightLengthConversion> loadAll()
120    {
121        return (Collection<WeightLengthConversion>) this.loadAll(WeightLengthConversionDao.TRANSFORM_NONE);
122    }
123
124    /**
125     * {@inheritDoc}
126     */
127    @Override
128    public Collection<?> loadAll(final int transform)
129    {
130        return this.loadAll(transform, -1, -1);
131    }
132
133    /**
134     * {@inheritDoc}
135     */
136    @Override
137    public Collection<?> loadAll(final int pageNumber, final int pageSize)
138    {
139        return this.loadAll(WeightLengthConversionDao.TRANSFORM_NONE, pageNumber, pageSize);
140    }
141
142    /**
143     * {@inheritDoc}
144     */
145    @Override
146    public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
147    {
148        try
149        {
150            final Criteria criteria = this.getSession().createCriteria(WeightLengthConversionImpl.class);
151            if (pageNumber > 0 && pageSize > 0)
152            {
153                criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
154                criteria.setMaxResults(pageSize);
155            }
156            final Collection<?> results = criteria.list();
157            this.transformEntities(transform, results);
158            return results;
159        }
160        catch (HibernateException ex)
161        {
162            throw ex;
163        }
164    }
165
166    /**
167     * firstResult = (pageNumber - 1) * pageSize
168     * @param pageNumber
169     * @param pageSize
170     * @return firstResult
171     */
172    protected int calculateFirstResult(int pageNumber, int pageSize)
173    {
174        int firstResult = 0;
175        if (pageNumber > 0)
176        {
177            firstResult = (pageNumber - 1) * pageSize;
178        }
179        return firstResult;
180    }
181
182    /**
183     * {@inheritDoc}
184     */
185    @Override
186    public WeightLengthConversion create(WeightLengthConversion weightLengthConversion)
187    {
188        return (WeightLengthConversion)this.create(WeightLengthConversionDao.TRANSFORM_NONE, weightLengthConversion);
189    }
190
191    /**
192     * {@inheritDoc}
193     */
194    @Override
195    public Object create(final int transform, final WeightLengthConversion weightLengthConversion)
196    {
197        if (weightLengthConversion == null)
198        {
199            throw new IllegalArgumentException(
200                "WeightLengthConversion.create - 'weightLengthConversion' can not be null");
201        }
202        this.getSessionFactory().getCurrentSession().save(weightLengthConversion);
203        return this.transformEntity(transform, weightLengthConversion);
204    }
205
206    /**
207     * {@inheritDoc}
208     */
209    @Override
210    @SuppressWarnings({"unchecked"})
211    public Collection<WeightLengthConversion> create(final Collection<WeightLengthConversion> entities)
212    {
213        return (Collection<WeightLengthConversion>) create(WeightLengthConversionDao.TRANSFORM_NONE, entities);
214    }
215
216    /**
217     * {@inheritDoc}
218     */
219    @Override
220    public Collection<?> create(final int transform, final Collection<WeightLengthConversion> entities)
221    {
222        if (entities == null)
223        {
224            throw new IllegalArgumentException(
225                "WeightLengthConversion.create - 'entities' can not be null");
226        }
227                    for (WeightLengthConversion entity : entities)
228                    {
229                        create(transform, entity);
230                    }
231        return entities;
232    }
233
234    /**
235     * {@inheritDoc}
236     */
237    @Override
238    public WeightLengthConversion create(
239        Double conversionCoefficientA,
240        Double conversionCoefficientB,
241        Integer startMonth,
242        Integer endMonth,
243        Integer year,
244        String description,
245        String comments,
246        Timestamp updateDate)
247    {
248        return (WeightLengthConversion)this.create(WeightLengthConversionDao.TRANSFORM_NONE, conversionCoefficientA, conversionCoefficientB, startMonth, endMonth, year, description, comments, updateDate);
249    }
250
251    /**
252     * {@inheritDoc}
253     */
254    @Override
255    public Object create(
256        final int transform,
257        Double conversionCoefficientA,
258        Double conversionCoefficientB,
259        Integer startMonth,
260        Integer endMonth,
261        Integer year,
262        String description,
263        String comments,
264        Timestamp updateDate)
265    {
266        WeightLengthConversion entity = new WeightLengthConversionImpl();
267        entity.setConversionCoefficientA(conversionCoefficientA);
268        entity.setConversionCoefficientB(conversionCoefficientB);
269        entity.setStartMonth(startMonth);
270        entity.setEndMonth(endMonth);
271        entity.setYear(year);
272        entity.setDescription(description);
273        entity.setComments(comments);
274        entity.setUpdateDate(updateDate);
275        return this.create(transform, entity);
276    }
277
278    /**
279     * {@inheritDoc}
280     */
281    @Override
282    public WeightLengthConversion create(
283        Double conversionCoefficientA,
284        Double conversionCoefficientB,
285        Integer endMonth,
286        Parameter lengthParameter,
287        Unit lengthUnit,
288        Location location,
289        ReferenceTaxon referenceTaxon,
290        Integer startMonth,
291        Status status,
292        Timestamp updateDate)
293    {
294        return (WeightLengthConversion)this.create(WeightLengthConversionDao.TRANSFORM_NONE, conversionCoefficientA, conversionCoefficientB, endMonth, lengthParameter, lengthUnit, location, referenceTaxon, startMonth, status, updateDate);
295    }
296
297    /**
298     * {@inheritDoc}
299     */
300    @Override
301    public Object create(
302        final int transform,
303        Double conversionCoefficientA,
304        Double conversionCoefficientB,
305        Integer endMonth,
306        Parameter lengthParameter,
307        Unit lengthUnit,
308        Location location,
309        ReferenceTaxon referenceTaxon,
310        Integer startMonth,
311        Status status,
312        Timestamp updateDate)
313    {
314        WeightLengthConversion entity = new WeightLengthConversionImpl();
315        entity.setConversionCoefficientA(conversionCoefficientA);
316        entity.setConversionCoefficientB(conversionCoefficientB);
317        entity.setEndMonth(endMonth);
318        entity.setLengthParameter(lengthParameter);
319        entity.setLengthUnit(lengthUnit);
320        entity.setLocation(location);
321        entity.setReferenceTaxon(referenceTaxon);
322        entity.setStartMonth(startMonth);
323        entity.setStatus(status);
324        entity.setUpdateDate(updateDate);
325        return this.create(transform, entity);
326    }
327
328    /**
329     * {@inheritDoc}
330     */
331    @Override
332    public void update(WeightLengthConversion weightLengthConversion)
333    {
334        if (weightLengthConversion == null)
335        {
336            throw new IllegalArgumentException(
337                "WeightLengthConversion.update - 'weightLengthConversion' can not be null");
338        }
339        this.getSessionFactory().getCurrentSession().update(weightLengthConversion);
340    }
341
342    /**
343     * {@inheritDoc}
344     */
345    @Override
346    public void update(final Collection<WeightLengthConversion> entities)
347    {
348        if (entities == null)
349        {
350            throw new IllegalArgumentException(
351                "WeightLengthConversion.update - 'entities' can not be null");
352        }
353                    for (WeightLengthConversion entity : entities)
354                    {
355                        update(entity);
356                    }
357    }
358
359    /**
360     * {@inheritDoc}
361     */
362    @Override
363    public void remove(WeightLengthConversion weightLengthConversion)
364    {
365        if (weightLengthConversion == null)
366        {
367            throw new IllegalArgumentException(
368                "WeightLengthConversion.remove - 'weightLengthConversion' can not be null");
369        }
370        this.getSessionFactory().getCurrentSession().delete(weightLengthConversion);
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                "WeightLengthConversion.remove - 'id' can not be null");
383        }
384        WeightLengthConversion 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<WeightLengthConversion> entities)
396    {
397        if (entities == null)
398        {
399            throw new IllegalArgumentException(
400                "WeightLengthConversion.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>WeightLengthConversionDao</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 WeightLengthConversionDao}
414     * @param entity an entity that was found
415     * @return the transformed entity (i.e. new value object, etc)
416     * @see WeightLengthConversionDao#transformEntity(int, WeightLengthConversion)
417     */
418    public Object transformEntity(final int transform, final WeightLengthConversion entity)
419    {
420        Object target = null;
421        if (entity != null)
422        {
423            switch (transform)
424            {
425                case WeightLengthConversionDao.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 WeightLengthConversionDao.TRANSFORM_NONE : // fall-through
442                default:
443                // do nothing;
444        }
445    }
446
447    /**
448     * @see WeightLengthConversionDao#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 WeightLengthConversion}
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 WeightLengthConversion)
474                {
475                    result = input;
476                }
477                return result;
478            }
479        };
480
481    /**
482     * @param row
483     * @return WeightLengthConversion
484     */
485    protected WeightLengthConversion toEntity(Object[] row)
486    {
487        WeightLengthConversion 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 WeightLengthConversion)
495                {
496                    target = (WeightLengthConversion)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(), WeightLengthConversionImpl.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(WeightLengthConversionDao.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(), WeightLengthConversionImpl.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<WeightLengthConversion> search(final Search search)
573    {
574        return (Set<WeightLengthConversion>) this.search(WeightLengthConversionDao.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}