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