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