BookDaoBase.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.book;

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>Book</code>.
 * </p>
 *
 * @see BookDao
 */
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Local({BookDao.class})
public abstract class BookDaoBase
    implements BookDao
{
    /** 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 BookDao#load
     */
    @Override
    public Object load(final int transform, final Long bookId)
        throws BookDaoException
    {
        if (bookId == null)
        {
            throw new IllegalArgumentException(
                "Book.load - 'bookId' can not be null");
        }
        try
        {
            final Book entity = this.emanager.find(Book.class, bookId);
            return transformEntity(transform, entity);
        }
        catch (Exception ex)
        {
            throw new BookDaoException(ex);
        }
    }

    /**
     * @see BookDao#load( Long)
     */
    @Override
    public Book load( final Long bookId)
        throws BookDaoException
    {
        return (Book)this.load(TRANSFORM_NONE, bookId);
    }

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

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

    /**
     * Create Book with no VO transformation
     * @see BookDao#create(Book)
     */
    @Override
    public Book create(Book book)
        throws BookDaoException
    {
        return (Book)this.create(TRANSFORM_NONE, book);
    }

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

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

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

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

    /**
     * Create Entity Book using instance attributes with no VO transformation
     * @see BookDao#create(String, long)
     */
    @Override
    public Book create(
        String name,
        long pages)
        throws BookDaoException
    {
        return (Book)this.create(TRANSFORM_NONE, name, pages);
    }

    /**
     * Create Entity Book using instance attributes with VO transformation
     * @see BookDao#create(int, String, long)
     * composite=false identifiers=1
     */
    @Override
    public Object create(
        final int transform,
        String name,
        long pages)
        throws BookDaoException
    {
        Book entity = new Book();
        entity.setName(name);
        entity.setPages(pages);
        return this.create(transform, entity);
    }

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

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

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

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

    /**
     * @see BookDao#remove(Collection)
     */
    @Override
    public void remove(Collection<Book> entities)
        throws BookDaoException
    {
        if (entities == null)
        {
            throw new IllegalArgumentException(
                "Book.remove - 'entities' can not be null");
        }
        try
        {
            for (final Book entity : entities)
            {
                remove(entity);
            }
        }
        catch (Exception ex)
        {
            throw new BookDaoException(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>BookDao</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 BookDao}
     * @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 Book 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, Book)}
     * 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>BookDao</code>
     * @param entities the collection of entities to transform
     * @see #transformEntity(int, Book)
     */
    protected void transformEntities(final int transform, final Collection<Book> 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;
    }
}