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