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