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