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