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