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