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