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.administration.programStrategy;
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.pmfm.Pmfm;
035import fr.ifremer.adagio.core.dao.technical.hibernate.HibernateDaoSupport;
036import java.security.Principal;
037import java.util.Collection;
038import java.util.LinkedHashSet;
039import java.util.List;
040import java.util.Set;
041import javax.annotation.Resource;
042import org.andromda.spring.PaginationResult;
043import org.apache.commons.collections.CollectionUtils;
044import org.apache.commons.collections.Transformer;
045import org.hibernate.Criteria;
046import org.hibernate.HibernateException;
047import org.hibernate.Query;
048import org.hibernate.ScrollableResults;
049
050/**
051 * <p>
052 * Base Spring DAO Class: is able to create, update, remove, load, and find
053 * objects of type <code>PmfmStrategy</code>.
054 * </p>
055 *
056 * @see PmfmStrategy
057 */
058public abstract class PmfmStrategyDaoBase
059    extends HibernateDaoSupport    
060    implements PmfmStrategyDao
061{
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public Object get(final int transform, final Integer id)
067    {
068        if (id == null)
069        {
070            throw new IllegalArgumentException(
071                "PmfmStrategy.get - 'id' can not be null");
072        }
073        final PmfmStrategy entity = get(PmfmStrategyImpl.class, id);
074        return transformEntity(transform, entity);
075    }
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public PmfmStrategy get(Integer id)
081    {
082        return (PmfmStrategy)this.get(TRANSFORM_NONE, id);
083    }
084
085    /**
086     * {@inheritDoc}
087     */
088    @Override
089    public Object load(final int transform, final Integer id)
090    {
091        if (id == null)
092        {
093            throw new IllegalArgumentException(
094                "PmfmStrategy.load - 'id' can not be null");
095        }
096        final PmfmStrategy entity = get(PmfmStrategyImpl.class, id);
097        return transformEntity(transform, entity);
098    }
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public PmfmStrategy load(Integer id)
105    {
106        return (PmfmStrategy)this.load(TRANSFORM_NONE, id);
107    }
108
109    /**
110     * {@inheritDoc}
111     */
112    @Override
113    @SuppressWarnings({"unchecked"})
114    public Collection<PmfmStrategy> loadAll()
115    {
116        return (Collection<PmfmStrategy>) this.loadAll(PmfmStrategyDao.TRANSFORM_NONE);
117    }
118
119    /**
120     * {@inheritDoc}
121     */
122    @Override
123    public Collection<?> loadAll(final int transform)
124    {
125        return this.loadAll(transform, -1, -1);
126    }
127
128    /**
129     * {@inheritDoc}
130     */
131    @Override
132    public Collection<?> loadAll(final int pageNumber, final int pageSize)
133    {
134        return this.loadAll(PmfmStrategyDao.TRANSFORM_NONE, pageNumber, pageSize);
135    }
136
137    /**
138     * {@inheritDoc}
139     */
140    @Override
141    public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
142    {
143        try
144        {
145            final Criteria criteria = this.getSession().createCriteria(PmfmStrategyImpl.class);
146            if (pageNumber > 0 && pageSize > 0)
147            {
148                criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
149                criteria.setMaxResults(pageSize);
150            }
151            final Collection<?> results = criteria.list();
152            this.transformEntities(transform, results);
153            return results;
154        }
155        catch (HibernateException ex)
156        {
157            throw ex;
158        }
159    }
160
161    /**
162     * firstResult = (pageNumber - 1) * pageSize
163     * @param pageNumber
164     * @param pageSize
165     * @return firstResult
166     */
167    protected int calculateFirstResult(int pageNumber, int pageSize)
168    {
169        int firstResult = 0;
170        if (pageNumber > 0)
171        {
172            firstResult = (pageNumber - 1) * pageSize;
173        }
174        return firstResult;
175    }
176
177    /**
178     * {@inheritDoc}
179     */
180    @Override
181    public PmfmStrategy create(PmfmStrategy pmfmStrategy)
182    {
183        return (PmfmStrategy)this.create(PmfmStrategyDao.TRANSFORM_NONE, pmfmStrategy);
184    }
185
186    /**
187     * {@inheritDoc}
188     */
189    @Override
190    public Object create(final int transform, final PmfmStrategy pmfmStrategy)
191    {
192        if (pmfmStrategy == null)
193        {
194            throw new IllegalArgumentException(
195                "PmfmStrategy.create - 'pmfmStrategy' can not be null");
196        }
197        this.getSessionFactory().getCurrentSession().save(pmfmStrategy);
198        return this.transformEntity(transform, pmfmStrategy);
199    }
200
201    /**
202     * {@inheritDoc}
203     */
204    @Override
205    @SuppressWarnings({"unchecked"})
206    public Collection<PmfmStrategy> create(final Collection<PmfmStrategy> entities)
207    {
208        return (Collection<PmfmStrategy>) create(PmfmStrategyDao.TRANSFORM_NONE, entities);
209    }
210
211    /**
212     * {@inheritDoc}
213     */
214    @Override
215    public Collection<?> create(final int transform, final Collection<PmfmStrategy> entities)
216    {
217        if (entities == null)
218        {
219            throw new IllegalArgumentException(
220                "PmfmStrategy.create - 'entities' can not be null");
221        }
222                    for (PmfmStrategy entity : entities)
223                    {
224                        create(transform, entity);
225                    }
226        return entities;
227    }
228
229    /**
230     * {@inheritDoc}
231     */
232    @Override
233    public PmfmStrategy create(
234        Integer acquisitionNumber,
235        Integer rankOrder,
236        Boolean isMandatory,
237        Float minValue,
238        Float maxValue)
239    {
240        return (PmfmStrategy)this.create(PmfmStrategyDao.TRANSFORM_NONE, acquisitionNumber, rankOrder, isMandatory, minValue, maxValue);
241    }
242
243    /**
244     * {@inheritDoc}
245     */
246    @Override
247    public Object create(
248        final int transform,
249        Integer acquisitionNumber,
250        Integer rankOrder,
251        Boolean isMandatory,
252        Float minValue,
253        Float maxValue)
254    {
255        PmfmStrategy entity = new PmfmStrategyImpl();
256        entity.setAcquisitionNumber(acquisitionNumber);
257        entity.setRankOrder(rankOrder);
258        entity.setIsMandatory(isMandatory);
259        entity.setMinValue(minValue);
260        entity.setMaxValue(maxValue);
261        return this.create(transform, entity);
262    }
263
264    /**
265     * {@inheritDoc}
266     */
267    @Override
268    public PmfmStrategy create(
269        AcquisitionLevel acquisitionLevel,
270        Integer acquisitionNumber,
271        Boolean isMandatory,
272        Pmfm pmfm,
273        Integer rankOrder,
274        Strategy strategy)
275    {
276        return (PmfmStrategy)this.create(PmfmStrategyDao.TRANSFORM_NONE, acquisitionLevel, acquisitionNumber, isMandatory, pmfm, rankOrder, strategy);
277    }
278
279    /**
280     * {@inheritDoc}
281     */
282    @Override
283    public Object create(
284        final int transform,
285        AcquisitionLevel acquisitionLevel,
286        Integer acquisitionNumber,
287        Boolean isMandatory,
288        Pmfm pmfm,
289        Integer rankOrder,
290        Strategy strategy)
291    {
292        PmfmStrategy entity = new PmfmStrategyImpl();
293        entity.setAcquisitionLevel(acquisitionLevel);
294        entity.setAcquisitionNumber(acquisitionNumber);
295        entity.setIsMandatory(isMandatory);
296        entity.setPmfm(pmfm);
297        entity.setRankOrder(rankOrder);
298        entity.setStrategy(strategy);
299        return this.create(transform, entity);
300    }
301
302    /**
303     * {@inheritDoc}
304     */
305    @Override
306    public void update(PmfmStrategy pmfmStrategy)
307    {
308        if (pmfmStrategy == null)
309        {
310            throw new IllegalArgumentException(
311                "PmfmStrategy.update - 'pmfmStrategy' can not be null");
312        }
313        this.getSessionFactory().getCurrentSession().update(pmfmStrategy);
314    }
315
316    /**
317     * {@inheritDoc}
318     */
319    @Override
320    public void update(final Collection<PmfmStrategy> entities)
321    {
322        if (entities == null)
323        {
324            throw new IllegalArgumentException(
325                "PmfmStrategy.update - 'entities' can not be null");
326        }
327                    for (PmfmStrategy entity : entities)
328                    {
329                        update(entity);
330                    }
331    }
332
333    /**
334     * {@inheritDoc}
335     */
336    @Override
337    public void remove(PmfmStrategy pmfmStrategy)
338    {
339        if (pmfmStrategy == null)
340        {
341            throw new IllegalArgumentException(
342                "PmfmStrategy.remove - 'pmfmStrategy' can not be null");
343        }
344        this.getSessionFactory().getCurrentSession().delete(pmfmStrategy);
345    }
346
347    /**
348     * {@inheritDoc}
349     */
350    @Override
351    public void remove(Integer id)
352    {
353        if (id == null)
354        {
355            throw new IllegalArgumentException(
356                "PmfmStrategy.remove - 'id' can not be null");
357        }
358        PmfmStrategy entity = this.get(id);
359        if (entity != null)
360        {
361            this.remove(entity);
362        }
363    }
364
365    /**
366     * {@inheritDoc}
367     */
368    @Override
369    public void remove(Collection<PmfmStrategy> entities)
370    {
371        if (entities == null)
372        {
373            throw new IllegalArgumentException(
374                "PmfmStrategy.remove - 'entities' can not be null");
375        }
376        deleteAll(entities);
377    }
378    /**
379     * Allows transformation of entities into value objects
380     * (or something else for that matter), when the <code>transform</code>
381     * flag is set to one of the constants defined in <code>PmfmStrategyDao</code>, please note
382     * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
383     * will be returned.
384     *
385     * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
386     *
387     * @param transform one of the constants declared in {@link PmfmStrategyDao}
388     * @param entity an entity that was found
389     * @return the transformed entity (i.e. new value object, etc)
390     * @see PmfmStrategyDao#transformEntity(int, PmfmStrategy)
391     */
392    public Object transformEntity(final int transform, final PmfmStrategy entity)
393    {
394        Object target = null;
395        if (entity != null)
396        {
397            switch (transform)
398            {
399                case PmfmStrategyDao.TRANSFORM_NONE : // fall-through
400                default:
401                    target = entity;
402            }
403        }
404        return target;
405    }
406
407    /**
408     * {@inheritDoc}
409     */
410    @Override
411    public void transformEntities(final int transform, final Collection<?> entities)
412    {
413        switch (transform)
414        {
415            case PmfmStrategyDao.TRANSFORM_NONE : // fall-through
416                default:
417                // do nothing;
418        }
419    }
420
421    /**
422     * @see PmfmStrategyDao#toEntities(Collection)
423     */
424    public void toEntities(final Collection<?> results)
425    {
426        if (results != null)
427        {
428            CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
429        }
430    }
431
432    /**
433     * This anonymous transformer is designed to transform report query results
434     * (which result in an array of entities) to {@link PmfmStrategy}
435     * using the Jakarta Commons-Collections Transformation API.
436     */
437    private Transformer ENTITYTRANSFORMER =
438        new Transformer()
439        {
440            public Object transform(Object input)
441            {
442                Object result = null;
443                if (input instanceof Object[])
444                {
445                    result = toEntity((Object[])input);
446                }
447                else if (input instanceof PmfmStrategy)
448                {
449                    result = input;
450                }
451                return result;
452            }
453        };
454
455    /**
456     * @param row
457     * @return PmfmStrategy
458     */
459    protected PmfmStrategy toEntity(Object[] row)
460    {
461        PmfmStrategy target = null;
462        if (row != null)
463        {
464            final int numberOfObjects = row.length;
465            for (int ctr = 0; ctr < numberOfObjects; ctr++)
466            {
467                final Object object = row[ctr];
468                if (object instanceof PmfmStrategy)
469                {
470                    target = (PmfmStrategy)object;
471                    break;
472                }
473            }
474        }
475        return target;
476    }
477
478    /**
479     * Gets the current <code>principal</code> if one has been set,
480     * otherwise returns <code>null</code>.
481     *
482     * @return the current principal
483     */
484    protected Principal getPrincipal()
485    {
486        return PrincipalStore.get();
487    }
488
489    /**
490     * {@inheritDoc}
491     */
492    @Override
493    @SuppressWarnings({ "unchecked" })
494    public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
495    {
496        try
497        {
498            search.setPageNumber(pageNumber);
499            search.setPageSize(pageSize);
500            final PropertySearch propertySearch = new PropertySearch(
501                this.getSession(), PmfmStrategyImpl.class, search);
502            final List results = propertySearch.executeAsList();
503            this.transformEntities(transform, results);
504            return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
505        }
506        catch (HibernateException ex)
507        {
508            throw ex; /*super.convertHibernateAccessException(ex);*/
509        }
510    }
511
512    /**
513     * {@inheritDoc}
514     */
515    @Override
516    public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
517    {
518        return this.search(PmfmStrategyDao.TRANSFORM_NONE, pageNumber, pageSize, search);
519    }
520
521    /**
522     * {@inheritDoc}
523     */
524    @Override
525    public Set<?> search(final int transform, final Search search)
526    {
527        try
528        {
529            final PropertySearch propertySearch = new PropertySearch(
530                this.getSession(), PmfmStrategyImpl.class, search);
531            final Set<?> results = propertySearch.executeAsSet();
532            this.transformEntities(transform, results);
533            return results;
534        }
535        catch (HibernateException ex)
536        {
537            throw ex; /*super.convertHibernateAccessException(ex);*/
538        }
539    }
540
541    /**
542     * {@inheritDoc}
543     */
544    @Override
545    @SuppressWarnings("unchecked")
546    public Set<PmfmStrategy> search(final Search search)
547    {
548        return (Set<PmfmStrategy>) this.search(PmfmStrategyDao.TRANSFORM_NONE, search);
549    }
550
551    /**
552     * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
553     * @param queryObject
554     * @param transform
555     * @param pageNumber
556     * @param pageSize
557     * @return PaginationResult
558     */
559    @SuppressWarnings({ "unchecked" })
560    protected PaginationResult getPaginationResult(
561        final Query queryObject,
562        final int transform, int pageNumber, int pageSize)
563    {
564        try
565        {
566            final ScrollableResults scrollableResults = queryObject.scroll();
567            scrollableResults.last();
568            int totalCount = scrollableResults.getRowNumber();
569            totalCount = totalCount >= 0 ? totalCount + 1 : 0;
570            if (pageNumber > 0 && pageSize > 0)
571            {
572                queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
573                queryObject.setMaxResults(pageSize);
574            }
575            // Unchecked transformation because Set object is reused, cannot be strongly typed.
576            Set results = new LinkedHashSet(queryObject.list());
577            transformEntities(transform, results);
578            return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
579        }
580        catch (HibernateException ex)
581        {
582            throw ex; /*super.convertHibernateAccessException(ex);*/
583        }
584    }
585
586    // spring-hibernate-dao-base merge-point
587}