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