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