UserDaoBase.java

// license-header java merge-point
//
// Attention: Generated code! Do not modify by hand!
// Generated by DaoBase.vsl in andromda-ejb3-cartridge on 09/15/2014 18:39:44.
//
package org.andromda.timetracker.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.ejb.EJB;
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.andromda.timetracker.vo.UserDetailsVO;
import org.andromda.timetracker.vo.UserVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.hibernate.Session;

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

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

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

    // ------ DAO Injections ------

    /**
     * Inject DAO UserRoleDao
     */
    @EJB
    protected UserRoleDao userRoleDao;

    /**
     * Gets the reference to <code>userRoleDao</code>.
     * @return UserRoleDao
     */
    protected UserRoleDao getUserRoleDao()
    {
        return this.userRoleDao;
    }

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

    /**
     * @see UserDao#load( Long)
     */
    @Override
    public User load( final Long userId)
        throws UserDaoException
    {
        return (User)this.load(TRANSFORM_NONE, userId);
    }

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

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

    /**
     * Create User with no VO transformation
     * @see UserDao#create(User)
     */
    @Override
    public User create(User user)
        throws UserDaoException
    {
        return (User)this.create(TRANSFORM_NONE, user);
    }

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

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

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

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

    /**
     * Create Entity User using instance attributes with no VO transformation
     * @see UserDao#create(String, String, String, String, String, boolean, Date, String)
     */
    @Override
    public User create(
        String username,
        String firstName,
        String lastName,
        String password,
        String email,
        boolean isActive,
        Date creationDate,
        String comment)
        throws UserDaoException
    {
        return (User)this.create(TRANSFORM_NONE, username, firstName, lastName, password, email, isActive, creationDate, comment);
    }

    /**
     * Create Entity User using instance attributes with VO transformation
     * @see UserDao#create(int, String, String, String, String, String, boolean, Date, String)
     * composite=false identifiers=1
     */
    @Override
    public Object create(
        final int transform,
        String username,
        String firstName,
        String lastName,
        String password,
        String email,
        boolean isActive,
        Date creationDate,
        String comment)
        throws UserDaoException
    {
        User entity = new User();
        entity.setUsername(username);
        entity.setFirstName(firstName);
        entity.setLastName(lastName);
        entity.setPassword(password);
        entity.setEmail(email);
        entity.setIsActive(isActive);
        entity.setCreationDate(creationDate);
        entity.setComment(comment);
        return this.create(transform, entity);
    }

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

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

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

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

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

    /**
     * @see UserDao#findByUsername(String)
     */
    @Override
    public User findByUsername(String username)
        throws UserDaoException
    {
        return (User)this.findByUsername(TRANSFORM_NONE, username);
    }

    /**
     * @see UserDao#findByUsername(String, String)
     */
    @Override
    public User findByUsername(final String queryString, final String username)
        throws UserDaoException
    {
        return (User)this.findByUsername(TRANSFORM_NONE, queryString, username);
    }

    /**
     * @see UserDao#findByUsername(int, String)
     */
    @Override
    public Object findByUsername(final int transform, final String username)
        throws UserDaoException
    {
        try
        {
            TypedQuery<User> queryObject = this.emanager.createNamedQuery("User.findByUsername", User.class);
            queryObject.setParameter("username", username);
            Object result = queryObject.getSingleResult();
            result = transformEntity(transform, (User)result);
            return result;
        }
        catch (Exception ex)
        {
            throw new UserDaoException(ex);
        }
    }

    /**
     * @see UserDao#findByUsername(int, String, String)
     */
    @Override
    public Object findByUsername(final int transform, final String queryString, final String username)
        throws UserDaoException
    {
        try
        {
            TypedQuery<User> queryObject = this.emanager.createQuery(queryString, User.class);
            queryObject.setParameter("username", username);
            Object result = queryObject.getSingleResult();
            result = transformEntity(transform, (User)result);
            return result;
        }
        catch (Exception ex)
        {
            throw new UserDaoException(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>UserDao</code>, please note
     * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
     * will be returned.
     * <p/>
     * This method will return instances of these types:
     * <ul>
     *   <li>{@link User} - {@link #TRANSFORM_NONE}</li>
     *   <li>{@link UserVO} - {@link #TRANSFORM_USERVO}</li>
     *   <li>{@link UserDetailsVO} - {@link #TRANSFORM_USERDETAILSVO}</li>
     * </ul>
     *
     * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
     *
     * @param transform one of the constants declared in {@link UserDao}
     * @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 User entity)
    {
        Object target = null;
        if (entity != null)
        {
            switch (transform)
            {
                case UserDao.TRANSFORM_USERVO :
                    target = toUserVO(entity);
                    break;
                case UserDao.TRANSFORM_USERDETAILSVO :
                    target = toUserDetailsVO(entity);
                    break;
                case TRANSFORM_NONE : // fall-through
                default:
                    target = entity;
            }
        }
        return target;
    }

    /**
     * Transforms a collection of entities using the
     * {@link #transformEntity(int, User)}
     * 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>UserDao</code>
     * @param entities the collection of entities to transform
     * @see #transformEntity(int, User)
     */
    protected void transformEntities(final int transform, final Collection<User> entities)
    {
        switch (transform)
        {
            case UserDao.TRANSFORM_USERVO :
                toUserVOCollection(entities);
                break;
            case UserDao.TRANSFORM_USERDETAILSVO :
                toUserDetailsVOCollection(entities);
                break;
            case TRANSFORM_NONE : // fall-through
                default:
                // do nothing;
        }
    }

    /**
     * @see UserDao#toUserVOCollection(Collection)
     */
    @Override
    public final void toUserVOCollection(Collection entities)
    {
        if (entities != null)
        {
            CollectionUtils.transform(entities, this.USERVO_TRANSFORMER);
        }
    }

    /**
     * Default implementation for transforming the results of a report query into a value object. This
     * implementation exists for convenience reasons only. It needs only be overridden in the
     * {@link UserDaoImpl} class if you intend to use reporting queries.
     * @param row Object[] Array of User to transform
     * @return target UserVO
     * @see UserDao#toUserVO(User)
     */
    protected UserVO toUserVO(Object[] row)
    {
        UserVO 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 User)
                {
                    target = this.toUserVO((User)object);
                    break;
                }
            }
        }
        return target;
    }

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

    /**
     * @see UserDao#userVOToEntityCollection(Collection)
     */
    @Override
    public final void userVOToEntityCollection(Collection instances)
    {
        if (instances != null)
        {
            for (@SuppressWarnings("rawtypes")
                final Iterator iterator = instances.iterator(); iterator.hasNext();)
            {
                // - remove objects that are null or not of the correct instance
                if (!(iterator.next() instanceof UserVO))
                {
                    iterator.remove();
                }
            }
            CollectionUtils.transform(instances, this.UserVOToEntityTransformer);
        }
    }

    private final Transformer UserVOToEntityTransformer =
        new Transformer()
        {
            @Override
            public Object transform(Object input)
            {
                return userVOToEntity((UserVO)input);
            }
        };

    /**
     * @see UserDao#toUserVO(User, UserVO)
     */
    @Override
    public void toUserVO(
        User source,
        UserVO target)
    {
        target.setUsername(source.getUsername());
        target.setFirstName(source.getFirstName());
        target.setLastName(source.getLastName());
    }

    /**
     * @see UserDao#toUserVO(User)
     */
    @Override
    public UserVO toUserVO(final User entity)
    {
        final UserVO target = new UserVO();
        this.toUserVO(entity, target);
        return target;
    }

    /**
     * @see UserDao#userVOToEntity(UserVO, User, boolean)
     */
    @Override
    public void userVOToEntity(
        UserVO source,
        User target,
        boolean copyIfNull)
    {
        if (copyIfNull || source.getUsername() != null)
        {
            target.setUsername(source.getUsername());
        }
        if (copyIfNull || source.getFirstName() != null)
        {
            target.setFirstName(source.getFirstName());
        }
        if (copyIfNull || source.getLastName() != null)
        {
            target.setLastName(source.getLastName());
        }
    }

    /**
     * @see UserDao#toUserDetailsVOCollection(Collection)
     */
    @Override
    public final void toUserDetailsVOCollection(Collection entities)
    {
        if (entities != null)
        {
            CollectionUtils.transform(entities, this.USERDETAILSVO_TRANSFORMER);
        }
    }

    /**
     * Default implementation for transforming the results of a report query into a value object. This
     * implementation exists for convenience reasons only. It needs only be overridden in the
     * {@link UserDaoImpl} class if you intend to use reporting queries.
     * @param row Object[] Array of User to transform
     * @return target UserDetailsVO
     * @see UserDao#toUserDetailsVO(User)
     */
    protected UserDetailsVO toUserDetailsVO(Object[] row)
    {
        UserDetailsVO 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 User)
                {
                    target = this.toUserDetailsVO((User)object);
                    break;
                }
            }
        }
        return target;
    }

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

    /**
     * @see UserDao#userDetailsVOToEntityCollection(Collection)
     */
    @Override
    public final void userDetailsVOToEntityCollection(Collection instances)
    {
        if (instances != null)
        {
            for (@SuppressWarnings("rawtypes")
                final Iterator iterator = instances.iterator(); iterator.hasNext();)
            {
                // - remove objects that are null or not of the correct instance
                if (!(iterator.next() instanceof UserDetailsVO))
                {
                    iterator.remove();
                }
            }
            CollectionUtils.transform(instances, this.UserDetailsVOToEntityTransformer);
        }
    }

    private final Transformer UserDetailsVOToEntityTransformer =
        new Transformer()
        {
            @Override
            public Object transform(Object input)
            {
                return userDetailsVOToEntity((UserDetailsVO)input);
            }
        };

    /**
     * @see UserDao#toUserDetailsVO(User, UserDetailsVO)
     */
    @Override
    public void toUserDetailsVO(
        User source,
        UserDetailsVO target)
    {
        target.setPassword(source.getPassword());
        target.setEmail(source.getEmail());
        target.setIsActive(source.isIsActive());
        target.setCreationDate(source.getCreationDate());
        target.setComment(source.getComment());
        // No conversion for target.roles (can't convert source.getRoles():org.andromda.timetracker.domain.UserRole to org.andromda.timetracker.vo.UserRoleVO[])
        target.setUsername(source.getUsername());
        target.setFirstName(source.getFirstName());
        target.setLastName(source.getLastName());
    }

    /**
     * @see UserDao#toUserDetailsVO(User)
     */
    @Override
    public UserDetailsVO toUserDetailsVO(final User entity)
    {
        final UserDetailsVO target = new UserDetailsVO();
        this.toUserDetailsVO(entity, target);
        return target;
    }

    /**
     * @see UserDao#userDetailsVOToEntity(UserDetailsVO, User, boolean)
     */
    @Override
    public void userDetailsVOToEntity(
        UserDetailsVO source,
        User target,
        boolean copyIfNull)
    {
        if (copyIfNull || source.getUsername() != null)
        {
            target.setUsername(source.getUsername());
        }
        if (copyIfNull || source.getFirstName() != null)
        {
            target.setFirstName(source.getFirstName());
        }
        if (copyIfNull || source.getLastName() != null)
        {
            target.setLastName(source.getLastName());
        }
        if (copyIfNull || source.getPassword() != null)
        {
            target.setPassword(source.getPassword());
        }
        if (copyIfNull || source.getEmail() != null)
        {
            target.setEmail(source.getEmail());
        }
        if (copyIfNull || source.isIsActive() != false)
        {
            target.setIsActive(source.isIsActive());
        }
        if (copyIfNull || source.getCreationDate() != null)
        {
            target.setCreationDate(source.getCreationDate());
        }
        if (copyIfNull || source.getComment() != null)
        {
            target.setComment(source.getComment());
        }
    }


    // 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;
    }
}