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