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