RegistrationManageableServiceBase.java

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

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.andromda.demo.ejb3.registration.Profile;
import org.andromda.demo.ejb3.registration.Registration;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;

/**
 * Autogenerated EJB manageable session bean class RegistrationManageableService.
 */
/**
 * Do not specify the javax.ejb.${manageable.type} annotation
 * Instead, define the session bean in the ejb-jar.xml descriptor
 * javax.ejb.${manageable.type}
 */
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Remote({RegistrationManageableService.class})
public final class RegistrationManageableServiceBase
    implements RegistrationManageableService
{
    // ------ Session Context Injection ------

    @Resource
    protected SessionContext context;

    // ------ Persistence Context Injection --------

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

    // ------------ Private Operations ----------

    private List<Profile> findProfileByIds(Long[] ids)
        throws RegistrationReadManageableException
    {
        try
        {
            Query query = emanager.createQuery("select entity from Profile as entity where entity.profileId in (:profileId)");
            query.setParameter("profileId", Arrays.asList(ids));
            return query.getResultList();
        }
        catch (Exception ex)
        {
            throw new RegistrationReadManageableException(ex);
        }
    }

    // ------------ CRUD Operations -------------

    /**
     * Create operations
     *
     * @return Registration
     * @throws RegistrationCreateManageableException
     */
    public Registration create(String mobileNumber, String name, String address, String description, Long registrationId, Long profile)
        throws RegistrationCreateManageableException
    {
        if (mobileNumber == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.create(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'mobileNumber' can not be null");
        }

        if (name == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.create(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'name' can not be null");
        }

        if (address == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.create(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'address' can not be null");
        }

        if (profile == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.create(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'profile' can not be null");
        }

        final Registration entity = new Registration();
        entity.setMobileNumber(mobileNumber);
        entity.setName(name);
        entity.setAddress(address);
        entity.setDescription(description);
        entity.setRegistrationId(registrationId);

        try
        {
            Profile profileEntity = null;
            if (profile != null)
            {
                profileEntity = (Profile)emanager.find(Profile.class, profile);
            }

            if (profileEntity != null)
            {
                entity.setProfile(profileEntity);
            }


            emanager.persist(entity);
            emanager.flush();
            return entity;
        }
        catch (Exception ex)
        {
            throw new RegistrationCreateManageableException(ex);
        }
    }

    /**
     * Entity read operation
     *
     * @return Registration
     * @throws RegistrationReadManageableException
     */
    public Registration readById(Long registrationId)
        throws RegistrationReadManageableException
    {
        try{
            return (Registration)emanager.find(Registration.class, registrationId);
        }
        catch (Exception ex)
        {
            throw new RegistrationReadManageableException(ex);
        }
    }

    /**
     * Read operation
     *
     * @return List
     * @throws RegistrationReadManageableException
     */
    public List<Registration> read(String mobileNumber, String name, String address, String description, Long registrationId, Long profile)
        throws RegistrationReadManageableException
    {
        String logicalOperator = "";
        StringBuilder buf = new StringBuilder("from Registration as entity");
        buf.append(" where ");
        if (mobileNumber != null)
        {
            buf.append(logicalOperator);
            buf.append("entity.mobileNumber like :mobileNumber");
            logicalOperator = " and ";
        }
        if (name != null)
        {
            buf.append(logicalOperator);
            buf.append("entity.name like :name");
            logicalOperator = " and ";
        }
        if (address != null)
        {
            buf.append(logicalOperator);
            buf.append("entity.address like :address");
            logicalOperator = " and ";
        }
        if (description != null)
        {
            buf.append(logicalOperator);
            buf.append("entity.description like :description");
            logicalOperator = " and ";
        }
        if (registrationId != null)
        {
            buf.append(logicalOperator);
            buf.append("entity.registrationId = :registrationId");
            logicalOperator = " and ";
        }
        if (profile != null)
        {
            buf.append(logicalOperator);
            buf.append("entity.profile = :profile");
            logicalOperator = " and ";
        }

        try
        {
            final Query query = emanager.createQuery(buf.toString());
            if (profile != null)
            {
                query.setParameter("profile", profile);
            }
            query.setMaxResults(250);
            return query.getResultList();
        }
        catch (Exception ex)
        {
            throw new RegistrationReadManageableException(ex);
        }
    }

    /**
     * Read all operation
     *
     * @return List
     * @throws RegistrationReadManageableException
     */
    public List<Registration> readAll()
        throws RegistrationReadManageableException
    {
        try
        {
            Query query = emanager.createQuery("select entity from Registration as entity");
            query.setMaxResults(250);
            List<Registration> entities = query.getResultList();
            CollectionUtils.transform(entities, new Transformer()
            {
                public Object transform(final Object object)
                {
                    Registration result = (Registration)object;
                    if (result.getProfile() != null)
                    {
                        result.setProfileLabel(result.getProfile().getCode());
                    }
                    return result;
                }
            });
            return entities;
        }
        catch (Exception ex)
        {
            throw new RegistrationReadManageableException(ex);
        }
    }

    /**
     * Read backing list operation
     *
     * @return Map
     * @throws RegistrationReadManageableException
     */
    public Map readBackingLists()
        throws RegistrationReadManageableException
    {
        final Map lists = new HashMap();

        try
        {
            lists.put("profile", emanager.createQuery("select item.profileId, item.code from Profile as item order by item.code").getResultList());
        }
        catch (Exception ex)
        {
            throw new RegistrationReadManageableException(ex);
        }
        return lists;
    }

    /**
     * Update Operation
     *
     * @return Registration
     * @throws RegistrationUpdateManageableException
     */
    public Registration update(String mobileNumber, String name, String address, String description, Long registrationId, Long profile)
        throws RegistrationUpdateManageableException
    {
        if (mobileNumber == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.update(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'mobileNumber' can not be null");
        }

        if (name == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.update(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'name' can not be null");
        }

        if (address == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.update(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'address' can not be null");
        }

        if (profile == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.update(String mobileNumber, String name, String address, String description, Long registrationId, Long profile) - 'profile' can not be null");
        }

        final Registration entity = (Registration)emanager.find(Registration.class, registrationId);
        entity.setMobileNumber(mobileNumber);
        entity.setName(name);
        entity.setAddress(address);
        entity.setDescription(description);

        try
        {
            Profile profileEntity = null;
            if (profile != null)
            {
                profileEntity = (Profile)emanager.find(Profile.class, profile);
            }

            if (profileEntity != null)
            {
                entity.setProfile(profileEntity);
            }

            emanager.merge(entity);
            emanager.flush();
            return entity;
        }
        catch (Exception ex)
        {
            throw new RegistrationUpdateManageableException(ex);
        }
    }

    /**
     * Delete operation
     *
     * @throws RegistrationDeleteManageableException
     */
    public void delete(Long[] ids)
        throws RegistrationDeleteManageableException
    {
        if (ids == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.RegistrationManageableService.delete(Long[] ids) - 'ids' can not be null");
        }

        try
        {
            final Query queryObject = emanager.createQuery("delete from Registration where registrationId in (:ids)");
            queryObject.setParameter("ids", Arrays.asList(ids));
            queryObject.executeUpdate();
        }
        catch (Exception ex)
        {
            throw new RegistrationDeleteManageableException(ex);
        }
    }

}