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.sample.Sample;
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>SampleMeasurement</code>.
057 * </p>
058 *
059 * @see SampleMeasurement
060 */
061public abstract class SampleMeasurementDaoBase
062    extends HibernateDaoSupport    
063    implements SampleMeasurementDao
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                "SampleMeasurement.get - 'id' can not be null");
075        }
076        final SampleMeasurement entity = get(SampleMeasurementImpl.class, id);
077        return transformEntity(transform, entity);
078    }
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public SampleMeasurement get(Integer id)
084    {
085        return (SampleMeasurement)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                "SampleMeasurement.load - 'id' can not be null");
098        }
099        final SampleMeasurement entity = get(SampleMeasurementImpl.class, id);
100        return transformEntity(transform, entity);
101    }
102
103    /**
104     * {@inheritDoc}
105     */
106    @Override
107    public SampleMeasurement load(Integer id)
108    {
109        return (SampleMeasurement)this.load(TRANSFORM_NONE, id);
110    }
111
112    /**
113     * {@inheritDoc}
114     */
115    @Override
116    @SuppressWarnings({"unchecked"})
117    public Collection<SampleMeasurement> loadAll()
118    {
119        return (Collection<SampleMeasurement>) this.loadAll(SampleMeasurementDao.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(SampleMeasurementDao.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(SampleMeasurementImpl.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 SampleMeasurement create(SampleMeasurement sampleMeasurement)
185    {
186        return (SampleMeasurement)this.create(SampleMeasurementDao.TRANSFORM_NONE, sampleMeasurement);
187    }
188
189    /**
190     * {@inheritDoc}
191     */
192    @Override
193    public Object create(final int transform, final SampleMeasurement sampleMeasurement)
194    {
195        if (sampleMeasurement == null)
196        {
197            throw new IllegalArgumentException(
198                "SampleMeasurement.create - 'sampleMeasurement' can not be null");
199        }
200        this.getSessionFactory().getCurrentSession().save(sampleMeasurement);
201        return this.transformEntity(transform, sampleMeasurement);
202    }
203
204    /**
205     * {@inheritDoc}
206     */
207    @Override
208    @SuppressWarnings({"unchecked"})
209    public Collection<SampleMeasurement> create(final Collection<SampleMeasurement> entities)
210    {
211        return (Collection<SampleMeasurement>) create(SampleMeasurementDao.TRANSFORM_NONE, entities);
212    }
213
214    /**
215     * {@inheritDoc}
216     */
217    @Override
218    public Collection<?> create(final int transform, final Collection<SampleMeasurement> entities)
219    {
220        if (entities == null)
221        {
222            throw new IllegalArgumentException(
223                "SampleMeasurement.create - 'entities' can not be null");
224        }
225                    for (SampleMeasurement entity : entities)
226                    {
227                        create(transform, entity);
228                    }
229        return entities;
230    }
231
232    /**
233     * {@inheritDoc}
234     */
235    @Override
236    public SampleMeasurement create(
237        Short individualNumber,
238        Boolean isReferenceMeasurement,
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 (SampleMeasurement)this.create(SampleMeasurementDao.TRANSFORM_NONE, individualNumber, isReferenceMeasurement, 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 individualNumber,
258        Boolean isReferenceMeasurement,
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        SampleMeasurement entity = new SampleMeasurementImpl();
269        entity.setIndividualNumber(individualNumber);
270        entity.setIsReferenceMeasurement(isReferenceMeasurement);
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 SampleMeasurement create(
287        Pmfm pmfm,
288        QualityFlag qualityFlag,
289        Boolean isReferenceMeasurement,
290        Sample sample)
291    {
292        return (SampleMeasurement)this.create(SampleMeasurementDao.TRANSFORM_NONE, pmfm, qualityFlag, isReferenceMeasurement, sample);
293    }
294
295    /**
296     * {@inheritDoc}
297     */
298    @Override
299    public Object create(
300        final int transform,
301        Pmfm pmfm,
302        QualityFlag qualityFlag,
303        Boolean isReferenceMeasurement,
304        Sample sample)
305    {
306        SampleMeasurement entity = new SampleMeasurementImpl();
307        entity.setPmfm(pmfm);
308        entity.setQualityFlag(qualityFlag);
309        entity.setIsReferenceMeasurement(isReferenceMeasurement);
310        entity.setSample(sample);
311        return this.create(transform, entity);
312    }
313
314    /**
315     * {@inheritDoc}
316     */
317    @Override
318    public void update(SampleMeasurement sampleMeasurement)
319    {
320        if (sampleMeasurement == null)
321        {
322            throw new IllegalArgumentException(
323                "SampleMeasurement.update - 'sampleMeasurement' can not be null");
324        }
325        this.getSessionFactory().getCurrentSession().update(sampleMeasurement);
326    }
327
328    /**
329     * {@inheritDoc}
330     */
331    @Override
332    public void update(final Collection<SampleMeasurement> entities)
333    {
334        if (entities == null)
335        {
336            throw new IllegalArgumentException(
337                "SampleMeasurement.update - 'entities' can not be null");
338        }
339                    for (SampleMeasurement entity : entities)
340                    {
341                        update(entity);
342                    }
343    }
344
345    /**
346     * {@inheritDoc}
347     */
348    @Override
349    public void remove(SampleMeasurement sampleMeasurement)
350    {
351        if (sampleMeasurement == null)
352        {
353            throw new IllegalArgumentException(
354                "SampleMeasurement.remove - 'sampleMeasurement' can not be null");
355        }
356        this.getSessionFactory().getCurrentSession().delete(sampleMeasurement);
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                "SampleMeasurement.remove - 'id' can not be null");
369        }
370        SampleMeasurement 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<SampleMeasurement> entities)
382    {
383        if (entities == null)
384        {
385            throw new IllegalArgumentException(
386                "SampleMeasurement.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>SampleMeasurementDao</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 SampleMeasurementDao}
400     * @param entity an entity that was found
401     * @return the transformed entity (i.e. new value object, etc)
402     * @see SampleMeasurementDao#transformEntity(int, SampleMeasurement)
403     */
404    public Object transformEntity(final int transform, final SampleMeasurement entity)
405    {
406        Object target = null;
407        if (entity != null)
408        {
409            switch (transform)
410            {
411                case SampleMeasurementDao.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 SampleMeasurementDao.TRANSFORM_NONE : // fall-through
428                default:
429                // do nothing;
430        }
431    }
432
433    /**
434     * @see SampleMeasurementDao#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 SampleMeasurement}
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 SampleMeasurement)
460                {
461                    result = input;
462                }
463                return result;
464            }
465        };
466
467    /**
468     * @param row
469     * @return SampleMeasurement
470     */
471    protected SampleMeasurement toEntity(Object[] row)
472    {
473        SampleMeasurement 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 SampleMeasurement)
481                {
482                    target = (SampleMeasurement)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(), SampleMeasurementImpl.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(SampleMeasurementDao.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(), SampleMeasurementImpl.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<SampleMeasurement> search(final Search search)
559    {
560        return (Set<SampleMeasurement>) this.search(SampleMeasurementDao.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}