ReservationDaoBase.java

// license-header java merge-point
//
// Attention: Generated code! Do not modify by hand! hibernate4=true hibernateVersion=4.3.6.Final
// Generated by hibernate/SpringHibernateDaoBase.vsl in andromda-spring-cartridge on 08/18/2014 15:29:43-0400. Do not modify by hand!.
//
package org.andromda.samples.carrental.contracts;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.andromda.samples.carrental.PrincipalStore;
import org.andromda.samples.carrental.PropertySearch;
import org.andromda.samples.carrental.Search;
import org.andromda.samples.carrental.customers.Customer;
import org.andromda.spring.PaginationResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

/**
 * <p>
 * Base Spring DAO Class: is able to create, update, remove, load, and find
 * objects of type <code>Reservation</code>.
 * </p>
 *
 * @see Reservation
 */
@Repository
public abstract class ReservationDaoBase
    implements ReservationDao
{
    /**
     * For backwards compatibility with HibernateDao method
     * @deprecated Use LogFactory.getLog on each subclass, for the correct class name
     * Apache commons logging logger used by all subclasses
     */
    @Deprecated
    protected Log logger = LogFactory.getLog(ReservationDaoBase.class);

    @Resource
    private SessionFactory sessionFactory;
    /**
     * @param sessionFactoryIn
     */
    public void setSessionFactory(SessionFactory sessionFactoryIn) {
        this.sessionFactory = sessionFactoryIn;
    }
    /**
     * @return SessionFactory
     */
    protected SessionFactory getSessionFactory() {
        return this.sessionFactory;
    }
    /**
     * @return currentSession
     */
    protected Session getSession() {
        return this.sessionFactory.getCurrentSession();
    }
    /**
     * For backwards compatibility with HibernateDao method
     * @deprecated Use this.sessionFactory.getCurrentSession() instead
     * @param ignore boolean - always create new session if needed
     * @return currentSession
     */
    @Deprecated
    protected Session getSession(boolean ignore) {
        return this.getSession();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object get(final int transform, final Long id)
    {
        if (id == null)
        {
            throw new IllegalArgumentException(
                "Reservation.get - 'id' can not be null");
        }
        final Object entity = this.sessionFactory.getCurrentSession().get(ReservationImpl.class, id);
        return transformEntity(transform, (Reservation)entity);
    }
    /**
     * {@inheritDoc}
     */
    @Override
    public Reservation get(Long id)
    {
        return (Reservation)this.get(TRANSFORM_NONE, id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object load(final int transform, final Long id)
    {
        if (id == null)
        {
            throw new IllegalArgumentException(
                "Reservation.load - 'id' can not be null");
        }
        final Object entity = this.sessionFactory.getCurrentSession().get(ReservationImpl.class, id);
        return transformEntity(transform, (Reservation)entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Reservation load(Long id)
    {
        return (Reservation)this.load(TRANSFORM_NONE, id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> loadAll()
    {
        return (Collection<Reservation>) this.loadAll(ReservationDao.TRANSFORM_NONE);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> loadAll(final int transform)
    {
        return this.loadAll(transform, -1, -1);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> loadAll(final int pageNumber, final int pageSize)
    {
        return this.loadAll(ReservationDao.TRANSFORM_NONE, pageNumber, pageSize);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
    {
        final Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(ReservationImpl.class);
        if (pageNumber > 0 && pageSize > 0)
        {
            criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            criteria.setMaxResults(pageSize);
        }
        final Collection<?> results = criteria.list();
        this.transformEntities(transform, results);
        return results;
    }

    /**
     * firstResult = (pageNumber - 1) * pageSize
     * @param pageNumber
     * @param pageSize
     * @return firstResult
     */
    protected int calculateFirstResult(int pageNumber, int pageSize)
    {
        int firstResult = 0;
        if (pageNumber > 0)
        {
            firstResult = (pageNumber - 1) * pageSize;
        }
        return firstResult;
    }

    /**
     * prePersist event - This method is called before creating the entity
     */
    protected void _prePersist(final Reservation reservation)
    {
    }
     
    /**
     * postPersist event - This method is called after creating the entity
     */
    protected void _postPersist(final Reservation reservation)
    {
    }
     
    /**
     * preUpdate event - This method is called before updating the entity
     */
    protected void _preUpdate(final Reservation reservation)
    {
    }
     
    /**
     * postUpdate event - This method is called after updating the entity
     */
    protected void _postUpdate(final Reservation reservation)
    {
    }
     
    /**
     * preRemove event - This method is called before deleting the entity
     */
    protected void _preRemove(final Reservation reservation)
    {
    }
     
    /**
     * postRemove event - This method is called after deleting the entity
     */
    protected void _postRemove(final Reservation reservation)
    {
    }
     
    /**
     * {@inheritDoc}
     */
    @Override
    public Reservation create(Reservation reservation)
    {
        return (Reservation)this.create(ReservationDao.TRANSFORM_NONE, reservation);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object create(final int transform, final Reservation reservation)
    {
        if (reservation == null)
        {
            throw new IllegalArgumentException(
                "Reservation.create - 'reservation' can not be null");
        }

        //prePersist event
        _prePersist(reservation);

        this.sessionFactory.getCurrentSession().save(reservation);

        //postPersist event
        _postPersist(reservation);

        return this.transformEntity(transform, reservation);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> create(final Collection<Reservation> entities)
    {
        return (Collection<Reservation>) create(ReservationDao.TRANSFORM_NONE, entities);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> create(final int transform, final Collection<Reservation> entities)
    {
        if (entities == null)
        {
            throw new IllegalArgumentException(
                "Reservation.create - 'entities' can not be null");
        }
        Collection<Object> transformed = new ArrayList<Object>();
        for (Reservation entity : entities)
        {
            transformed.add(create(transform, entity));
        }
        return transformed;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Reservation create(
        Date reservationDate,
        String comfortClass)
    {
        return (Reservation)this.create(ReservationDao.TRANSFORM_NONE, reservationDate, comfortClass);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object create(
        final int transform,
        Date reservationDate,
        String comfortClass)
    {
        Reservation entity = new ReservationImpl();
        entity.setReservationDate(reservationDate);
        entity.setComfortClass(comfortClass);
        return this.create(transform, entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Reservation create(
        String comfortClass,
        Customer customer,
        Date reservationDate)
    {
        return (Reservation)this.create(ReservationDao.TRANSFORM_NONE, comfortClass, customer, reservationDate);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object create(
        final int transform,
        String comfortClass,
        Customer customer,
        Date reservationDate)
    {
        Reservation entity = new ReservationImpl();
        entity.setComfortClass(comfortClass);
        entity.setCustomer(customer);
        entity.setReservationDate(reservationDate);
        return this.create(transform, entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void update(Reservation reservation)
    {
        if (reservation == null)
        {
            throw new IllegalArgumentException(
                "Reservation.update - 'reservation' can not be null");
        }
        
        //preUpdate event
        _preUpdate(reservation);

        this.sessionFactory.getCurrentSession().update(reservation);

        //postUpdate event
        _postUpdate(reservation);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void update(final Collection<Reservation> entities)
    {
        if (entities == null)
        {
            throw new IllegalArgumentException(
                "Reservation.update - 'entities' can not be null");
        }
        for (Reservation entity : entities)
        {
            update(entity);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void remove(Reservation reservation)
    {
        if (reservation == null)
        {
            throw new IllegalArgumentException(
                "Reservation.remove - 'reservation' can not be null");
        }
        //preRemove event
        _preRemove(reservation);
        
        this.sessionFactory.getCurrentSession().delete(reservation);

        //postRemove event
        _postRemove(reservation);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void remove(Long id)
    {
        if (id == null)
        {
            throw new IllegalArgumentException(
                "Reservation.remove - 'id' can not be null");
        }
        Reservation entity = this.get(id);
        if (entity != null)
        {
            this.remove(entity);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void remove(Collection<Reservation> entities)
    {
        if (entities == null)
        {
            throw new IllegalArgumentException(
                "Reservation.remove - 'entities' can not be null");
        }
        for (Reservation entity : entities)
        {
            //preRemove event
            _preRemove(entity);

            this.sessionFactory.getCurrentSession().delete(entity);

            //postRemove event
            _postRemove(entity);
        }
    }
    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> findAll()
    {
        return (Collection<Reservation>)this.findAll(ReservationDao.TRANSFORM_NONE);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> findAll(final int transform)
    {
        return this.findAll(transform, -1, -1);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> findAll(final String queryString)
    {
        return (Collection<Reservation>)this.findAll(ReservationDao.TRANSFORM_NONE, queryString);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> findAll(final int pageNumber, final int pageSize)
    {
        return (Collection<Reservation>) this.findAll(ReservationDao.TRANSFORM_NONE, pageNumber, pageSize);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> findAll(final String queryString, final int pageNumber, final int pageSize)
    {
        return (Collection<Reservation>) this.findAll(ReservationDao.TRANSFORM_NONE, queryString, pageNumber, pageSize);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> findAll(final int transform, final String queryString)
    {
        return this.findAll(transform, queryString, -1, -1);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> findAll(final int transform, final int pageNumber, final int pageSize)
    {
        return this.findAll(transform, "from ReservationImpl as reservation", pageNumber, pageSize);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public Collection<?> findAll(final int transform, final String queryString, int pageNumber, int pageSize)
    {
        Query queryObject = this.sessionFactory.getCurrentSession().createQuery(queryString);
            if (pageNumber > 0 && pageSize > 0)
            {
                queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
                queryObject.setMaxResults(pageSize);
            }
            List results = queryObject.list();
            transformEntities(transform, results);
            return results;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> findByCustomer(String customerId)
    {
        return (Collection<Reservation>)this.findByCustomer(ReservationDao.TRANSFORM_NONE, customerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> findByCustomer(final int transform, final String customerId)
    {
        return this.findByCustomer(transform, -1, -1, customerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> findByCustomer(final String queryString, final String customerId)
    {
        return (Collection<Reservation>)this.findByCustomer(ReservationDao.TRANSFORM_NONE, queryString, customerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> findByCustomer(final int pageNumber, final int pageSize, final String customerId)
    {
        return (Collection<Reservation>) this.findByCustomer(ReservationDao.TRANSFORM_NONE, pageNumber, pageSize, customerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> findByCustomer(final String queryString, final int pageNumber, final int pageSize, final String customerId)
    {
        return (Collection<Reservation>) this.findByCustomer(ReservationDao.TRANSFORM_NONE, queryString, pageNumber, pageSize, customerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> findByCustomer(final int transform, final String queryString, final String customerId)
    {
        return this.findByCustomer(transform, queryString, -1, -1, customerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> findByCustomer(final int transform, final int pageNumber, final int pageSize, final String customerId)
    {
        return this.findByCustomer(transform, "from ReservationImpl as reservation where reservation.customerId = :customerId", pageNumber, pageSize, customerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public Collection<?> findByCustomer(final int transform, final String queryString, int pageNumber, int pageSize, final String customerId)
    {
        Query queryObject = this.sessionFactory.getCurrentSession().createQuery(queryString);
            queryObject.setParameter("customerId", customerId);
            if (pageNumber > 0 && pageSize > 0)
            {
                queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
                queryObject.setMaxResults(pageSize);
            }
            List results = queryObject.list();
            transformEntities(transform, results);
            return results;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> findByComfortClassAndDate(String comfortClass, Date reservationDate)
    {
        return (Collection<Reservation>)this.findByComfortClassAndDate(ReservationDao.TRANSFORM_NONE, comfortClass, reservationDate);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> findByComfortClassAndDate(final int transform, final String comfortClass, final Date reservationDate)
    {
        return this.findByComfortClassAndDate(transform, -1, -1, comfortClass, reservationDate);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> findByComfortClassAndDate(final String queryString, final String comfortClass, final Date reservationDate)
    {
        return (Collection<Reservation>)this.findByComfortClassAndDate(ReservationDao.TRANSFORM_NONE, queryString, comfortClass, reservationDate);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> findByComfortClassAndDate(final int pageNumber, final int pageSize, final String comfortClass, final Date reservationDate)
    {
        return (Collection<Reservation>) this.findByComfortClassAndDate(ReservationDao.TRANSFORM_NONE, pageNumber, pageSize, comfortClass, reservationDate);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Reservation> findByComfortClassAndDate(final String queryString, final int pageNumber, final int pageSize, final String comfortClass, final Date reservationDate)
    {
        return (Collection<Reservation>) this.findByComfortClassAndDate(ReservationDao.TRANSFORM_NONE, queryString, pageNumber, pageSize, comfortClass, reservationDate);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> findByComfortClassAndDate(final int transform, final String queryString, final String comfortClass, final Date reservationDate)
    {
        return this.findByComfortClassAndDate(transform, queryString, -1, -1, comfortClass, reservationDate);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> findByComfortClassAndDate(final int transform, final int pageNumber, final int pageSize, final String comfortClass, final Date reservationDate)
    {
        return this.findByComfortClassAndDate(transform, "from ReservationImpl as reservation where reservation.comfortClass = :comfortClass and reservation.reservationDate = :reservationDate", pageNumber, pageSize, comfortClass, reservationDate);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public Collection<?> findByComfortClassAndDate(final int transform, final String queryString, int pageNumber, int pageSize, final String comfortClass, final Date reservationDate)
    {
        Query queryObject = this.sessionFactory.getCurrentSession().createQuery(queryString);
            queryObject.setParameter("comfortClass", comfortClass);
            queryObject.setParameter("reservationDate", reservationDate);
            if (pageNumber > 0 && pageSize > 0)
            {
                queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
                queryObject.setMaxResults(pageSize);
            }
            List results = queryObject.list();
            transformEntities(transform, results);
            return results;
    }

    /**
     * Allows transformation of entities into value objects
     * (or something else for that matter), when the <code>transform</code>
     * flag is set to one of the constants defined in <code>ReservationDao</code>, please note
     * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
     * will be returned.
     *
     * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
     *
     * @param transform one of the constants declared in {@link ReservationDao}
     * @param entity an entity that was found
     * @return the transformed entity (i.e. new value object, etc)
     * @see ReservationDao#transformEntity(int, Reservation)
     */
    @Override
    public Object transformEntity(final int transform, final Reservation entity)
    {
        Object target = null;
        if (entity != null)
        {
            switch (transform)
            {
                case ReservationDao.TRANSFORM_NONE : // fall-through
                default:
                    target = entity;
            }
        }
        return target;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void transformEntities(final int transform, final Collection<?> entities)
    {
        switch (transform)
        {
            case ReservationDao.TRANSFORM_NONE : // fall-through
                default:
                // do nothing;
        }
    }

    /**
     * @see ReservationDao#toEntities(Collection)
     */
    @Override
    public void toEntities(final Collection<?> results)
    {
        if (results != null)
        {
            CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
        }
    }

    /**
     * This anonymous transformer is designed to transform report query results
     * (which result in an array of entities) to {@link Reservation}
     * using the Jakarta Commons-Collections Transformation API.
     */
    private Transformer ENTITYTRANSFORMER =
        new Transformer()
        {
            @Override
            public Object transform(Object input)
            {
                Object result = null;
                if (input instanceof Object[])
                {
                    final Object[] rows = (Object[])input; 
                    result = toEntity(rows);
                }
                else if (input instanceof Reservation)
                {
                    result = input;
                }
                return result;
            }
        };

    /**
     * @param row
     * @return Reservation
     */
    protected Reservation toEntity(Object[] row)
    {
        Reservation target = null;
        if (row != null)
        {
            final int numberOfObjects = row.length;
            for (int ctr = 0; ctr < numberOfObjects; ctr++)
            {
                final Object object = row[ctr];
                if (object instanceof Reservation)
                {
                    target = (Reservation)object;
                    break;
                }
            }
        }
        return target;
    }

    /**
     * Gets the current <code>principal</code> if one has been set,
     * otherwise returns <code>null</code>.
     *
     * @return the current principal
     */
    protected Principal getPrincipal()
    {
        return PrincipalStore.get();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({ "unchecked" })
    public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
    {
        search.setPageNumber(pageNumber);
        search.setPageSize(pageSize);
        final PropertySearch propertySearch = new PropertySearch(
            this.sessionFactory.getCurrentSession(), ReservationImpl.class, search);
        final List results = propertySearch.executeAsList();
        this.transformEntities(transform, results);
        return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
    {
        return this.search(ReservationDao.TRANSFORM_NONE, pageNumber, pageSize, search);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<?> search(final int transform, final Search search)
    {
        final PropertySearch propertySearch = new PropertySearch(
            this.sessionFactory.getCurrentSession(), ReservationImpl.class, search);
        final Collection<?> results = propertySearch.executeAsList();
        this.transformEntities(transform, results);
        return results;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public Collection<Reservation> search(final Search search)
    {
        return (Collection<Reservation>) this.search(ReservationDao.TRANSFORM_NONE, search);
    }

    /**
     * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
     * @param queryObject
     * @param transform
     * @param pageNumber
     * @param pageSize
     * @return PaginationResult
     */
    @SuppressWarnings({ "unchecked" })
    protected PaginationResult getPaginationResult(
        final Query queryObject,
        final int transform, int pageNumber, int pageSize)
    {
        final ScrollableResults scrollableResults = queryObject.scroll();
        scrollableResults.last();
        int totalCount = scrollableResults.getRowNumber();
        totalCount = totalCount >= 0 ? totalCount + 1 : 0;
        if (pageNumber > 0 && pageSize > 0)
        {
            queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            queryObject.setMaxResults(pageSize);
        }
        // Unchecked transformation because Set object is reused, cannot be strongly typed.
        @SuppressWarnings("rawtypes")
        Collection results = new ArrayList(queryObject.list());
        transformEntities(transform, results);
        return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
    }


    /**
     * Create or Update the <code>reservation</code> instance in the persistent store.
     * @param reservation
     */
    @Override
    public Reservation createOrUpdate(Reservation reservation)
    {
        if(reservation.getId() == null)
        {
            return (Reservation)this.create(TRANSFORM_NONE,reservation);
        }
        else
        {
            this.update(reservation);
            return reservation;
        }
    }
    
    // spring-hibernate-dao-base merge-point
}