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