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