BicycleDaoBase.java

// license-header java merge-point
//
// Attention: Generated code! Do not modify by hand!
// Generated by DaoBase.vsl in andromda-ejb3-cartridge on 08/08/2014 12:21:06.
//
package org.andromda.demo.ejb3.bicycle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Local;
import javax.ejb.SessionContext;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.hibernate.Session;

/**
 * <p>
 * Base EJB3 DAO Class: is able to create, update, remove, load, and find
 * objects of type <code>Bicycle</code>.
 * </p>
 *
 * @see BicycleDao
 */
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Local({BicycleDao.class})
public abstract class BicycleDaoBase
    implements BicycleDao
{
    /** Session Context Injection */
    @Resource
    protected SessionContext context;

    /**
     * Inject persistence context demo-ejb3
     */
    @PersistenceContext(unitName = "demo-ejb3")
    protected EntityManager emanager;

    /**
     * Inject Hibernate Session
     */
    @PersistenceContext(unitName = "demo-ejb3")
    protected Session hibernateSession;

    /**
     * @see BicycleDao#load
     */
    @Override
    public Object load(final int transform, final Long id)
        throws BicycleDaoException
    {
        if (id == null)
        {
            throw new IllegalArgumentException(
                "Bicycle.load - 'id' can not be null");
        }
        try
        {
            final Bicycle entity = this.emanager.find(Bicycle.class, id);
            return transformEntity(transform, entity);
        }
        catch (Exception ex)
        {
            throw new BicycleDaoException(ex);
        }
    }

    /**
     * @see BicycleDao#load( Long)
     */
    @Override
    public Bicycle load( final Long id)
        throws BicycleDaoException
    {
        return (Bicycle)this.load(TRANSFORM_NONE, id);
    }

    /**
     * @see BicycleDao#loadAll()
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Bicycle> loadAll()
        throws BicycleDaoException
    {
        return this.loadAll(TRANSFORM_NONE);
    }

    /**
     * @see BicycleDao#loadAll(int)
     */
    @Override
    public Collection loadAll(final int transform)
        throws BicycleDaoException
    {
        try
        {
            TypedQuery<Bicycle> query = this.emanager.createNamedQuery("Bicycle.findAll", Bicycle.class);
            List<Bicycle> results = query.getResultList();
            this.transformEntities(transform, results);
            return results;
        }
        catch (Exception ex)
        {
            throw new BicycleDaoException(ex);
        }
    }

    /**
     * Create Bicycle with no VO transformation
     * @see BicycleDao#create(Bicycle)
     */
    @Override
    public Bicycle create(Bicycle bicycle)
        throws BicycleDaoException
    {
        return (Bicycle)this.create(TRANSFORM_NONE, bicycle);
    }

    /**
     * Create Bicycle with VO transformation
     * @see BicycleDao#create(int, Bicycle)
     */
    @Override
    public Object create(final int transform, final Bicycle bicycle)
        throws BicycleDaoException
    {
        if (bicycle == null)
        {
            throw new IllegalArgumentException(
                "Bicycle.create - 'bicycle' can not be null");
        }

        try
        {
            this.emanager.persist(bicycle);
            this.emanager.flush();
            return this.transformEntity(transform, bicycle);
        }
        catch (Exception ex)
        {
            throw new BicycleDaoException(ex);
        }
    }

    /**
     * Create a Collection of Bicycle with no VO transformation
     * @see BicycleDao#create(Collection)
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public Collection<Bicycle> create(final Collection<Bicycle> entities)
        throws BicycleDaoException
    {
        return create(TRANSFORM_NONE, entities);
    }

    /**
     * Create a Collection of Bicycle with VO transformation
     * @see BicycleDao#create(int, Collection)
     */
    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public Collection create(final int transform, final Collection<Bicycle> entities)
        throws BicycleDaoException
    {
        if (entities == null)
        {
            throw new IllegalArgumentException(
                "Bicycle.create - 'entities' can not be null");
        }
        Collection results = new ArrayList();
        try
        {
            for (final Bicycle entity : entities)
            {
                results.add(create(transform, entity));
            }
        }
        catch (Exception ex)
        {
            throw new BicycleDaoException(ex);
        }
        return results;
    }

    /**
     * Create Entity Bicycle using instance attributes with no VO transformation
     * @see BicycleDao#create(String, int, String)
     */
    @Override
    public Bicycle create(
        String type,
        int gears,
        String tyreTypes)
        throws BicycleDaoException
    {
        return (Bicycle)this.create(TRANSFORM_NONE, type, gears, tyreTypes);
    }

    /**
     * Create Entity Bicycle using instance attributes with VO transformation
     * @see BicycleDao#create(int, String, int, String)
     * composite=false identifiers=1
     */
    @Override
    public Object create(
        final int transform,
        String type,
        int gears,
        String tyreTypes)
        throws BicycleDaoException
    {
        Bicycle entity = new Bicycle();
        entity.setType(type);
        entity.setGears(gears);
        entity.setTyreTypes(tyreTypes);
        return this.create(transform, entity);
    }

    /**
     * @see BicycleDao#update(Bicycle)
     */
    @Override
    public void update(Bicycle bicycle)
        throws BicycleDaoException
    {
        if (bicycle == null)
        {
            throw new IllegalArgumentException(
                "Bicycle.update - 'bicycle' can not be null");
        }
        try
        {
            this.emanager.merge(bicycle);
            this.emanager.flush();
        }
        catch (Exception ex)
        {
            throw new BicycleDaoException(ex);
        }
    }

    /**
     * @see BicycleDao#update(Collection)
     */
    @Override
    public void update(final Collection<Bicycle> entities)
        throws BicycleDaoException
    {
        if (entities == null)
        {
            throw new IllegalArgumentException(
                "Bicycle.update - 'entities' can not be null");
        }
        try
        {
            for (final Bicycle entity : entities)
            {
                update(entity);
            }
        }
        catch (Exception ex)
        {
            throw new BicycleDaoException(ex);
        }
    }

    /**
     * @see BicycleDao#remove(Bicycle)
     */
    @Override
    public void remove(Bicycle bicycle)
        throws BicycleDaoException
    {
        if (bicycle == null)
        {
            throw new IllegalArgumentException(
                "Bicycle.remove - 'bicycle' can not be null");
        }
        try
        {
            this.emanager.remove(bicycle);
            this.emanager.flush();
        }
        catch (Exception ex)
        {
            throw new BicycleDaoException(ex);
        }
    }

    /**
     * @see BicycleDao#remove(Long)
     */
    @Override
    public void remove(Long id)
        throws BicycleDaoException
    {
        if (id == null)
        {
            throw new IllegalArgumentException(
                "Bicycle.remove - 'id' can not be null");
        }
        try
        {
            final Bicycle entity = this.load(id);
            if (entity != null)
            {
                this.remove(entity);
            }
        }
        catch (Exception ex)
        {
            throw new BicycleDaoException(ex);
        }
    }

    /**
     * @see BicycleDao#remove(Collection)
     */
    @Override
    public void remove(Collection<Bicycle> entities)
        throws BicycleDaoException
    {
        if (entities == null)
        {
            throw new IllegalArgumentException(
                "Bicycle.remove - 'entities' can not be null");
        }
        try
        {
            for (final Bicycle entity : entities)
            {
                remove(entity);
            }
        }
        catch (Exception ex)
        {
            throw new BicycleDaoException(ex);
        }
    }

    /**
     * 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>BicycleDao</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 BicycleDao}
     * @param entity an entity that was found
     * @return the transformed entity (i.e. new value object, etc)
     * @see #transformEntities(int,Collection)
     */
    protected Object transformEntity(final int transform, final Bicycle entity)
    {
        Object target = null;
        if (entity != null)
        {
            switch (transform)
            {
                case TRANSFORM_NONE : // fall-through
                default:
                    target = entity;
            }
        }
        return target;
    }

    /**
     * Transforms a collection of entities using the
     * {@link #transformEntity(int, Bicycle)}
     * method. This method does not instantiate a new collection.
     * <p/>
     * Transforms into the same collection as the argument, but this time containing the transformed entities
     * This method is to be used internally only.
     *
     * @param transform one of the constants declared in <code>BicycleDao</code>
     * @param entities the collection of entities to transform
     * @see #transformEntity(int, Bicycle)
     */
    protected void transformEntities(final int transform, final Collection<Bicycle> entities)
    {
        switch (transform)
        {
            case TRANSFORM_NONE : // fall-through
                default:
                // do nothing;
        }
    }


    // For unit testing outside of container - persistence context not injected
    /**
     * @return the context
     */
    public SessionContext getContext()
    {
        return this.context;
    }

    /**
     * @param contextIn the context to set
     */
    public void setContext(SessionContext contextIn)
    {
        this.context = contextIn;
    }

    /**
     * @return the emanager
     */
    public EntityManager getEmanager()
    {
        return this.emanager;
    }

    /**
     * @param emanagerIn the emanager to set
     */
    public void setEmanager(EntityManager emanagerIn)
    {
        this.emanager = emanagerIn;
    }

    /**
     * @return the hibernateSession
     */
    public Session getHibernateSession()
    {
        return this.hibernateSession;
    }

    /**
     * @param hibernateSessionIn the hibernateSession to set
     */
    public void setHibernateSession(Session hibernateSessionIn)
    {
        this.hibernateSession = hibernateSessionIn;
    }
}