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