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