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