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