ProfileManageableServiceBase.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 ProfileManageableService.
 */
/**
 * 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({ProfileManageableService.class})
public final class ProfileManageableServiceBase
    implements ProfileManageableService
{
    // ------ 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<Registration> findRegistrationByIds(Long[] ids)
        throws ProfileReadManageableException
    {
        try
        {
            Query query = emanager.createQuery("select entity from Registration as entity where entity.registrationId in (:registrationId)");
            query.setParameter("registrationId", Arrays.asList(ids));
            return query.getResultList();
        }
        catch (Exception ex)
        {
            throw new ProfileReadManageableException(ex);
        }
    }

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

    /**
     * Create operations
     *
     * @return Profile
     * @throws ProfileCreateManageableException
     */
    public Profile create(String code, String name, String description, Long profileId, Long registration)
        throws ProfileCreateManageableException
    {
        if (code == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.ProfileManageableService.create(String code, String name, String description, Long profileId, Long registration) - 'code' can not be null");
        }

        if (registration == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.ProfileManageableService.create(String code, String name, String description, Long profileId, Long registration) - 'registration' can not be null");
        }

        final Profile entity = new Profile();
        entity.setCode(code);
        entity.setName(name);
        entity.setDescription(description);
        entity.setProfileId(profileId);

        try
        {
            Registration registrationEntity = null;
            if (registration != null)
            {
                registrationEntity = (Registration)emanager.find(Registration.class, registration);
            }

            if (registrationEntity != null)
            {
                entity.setRegistration(registrationEntity);
            }


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

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

    /**
     * Read operation
     *
     * @return List
     * @throws ProfileReadManageableException
     */
    public List<Profile> read(String code, String name, String description, Long profileId, Long registration)
        throws ProfileReadManageableException
    {
        String logicalOperator = "";
        StringBuilder buf = new StringBuilder("from Profile as entity");
        buf.append(" where ");
        if (code != null)
        {
            buf.append(logicalOperator);
            buf.append("entity.code like :code");
            logicalOperator = " and ";
        }
        if (name != null)
        {
            buf.append(logicalOperator);
            buf.append("entity.name like :name");
            logicalOperator = " and ";
        }
        if (description != null)
        {
            buf.append(logicalOperator);
            buf.append("entity.description like :description");
            logicalOperator = " and ";
        }
        if (profileId != null)
        {
            buf.append(logicalOperator);
            buf.append("entity.profileId = :profileId");
            logicalOperator = " and ";
        }
        if (registration != null)
        {
            buf.append(logicalOperator);
            buf.append("entity.registration = :registration");
            logicalOperator = " and ";
        }

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

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

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

        try
        {
            lists.put("registration", emanager.createQuery("select item.registrationId, item.mobileNumber from Registration as item order by item.mobileNumber").getResultList());
        }
        catch (Exception ex)
        {
            throw new ProfileReadManageableException(ex);
        }
        return lists;
    }

    /**
     * Update Operation
     *
     * @return Profile
     * @throws ProfileUpdateManageableException
     */
    public Profile update(String code, String name, String description, Long profileId, Long registration)
        throws ProfileUpdateManageableException
    {
        if (code == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.ProfileManageableService.update(String code, String name, String description, Long profileId, Long registration) - 'code' can not be null");
        }

        if (registration == null)
        {
            throw new IllegalArgumentException(
                "org.andromda.demo.ejb3.registration.crud.ProfileManageableService.update(String code, String name, String description, Long profileId, Long registration) - 'registration' can not be null");
        }

        final Profile entity = (Profile)emanager.find(Profile.class, profileId);
        entity.setCode(code);
        entity.setName(name);
        entity.setDescription(description);

        try
        {
            Registration registrationEntity = null;
            if (registration != null)
            {
                registrationEntity = (Registration)emanager.find(Registration.class, registration);
            }

            if (registrationEntity != null)
            {
                entity.setRegistration(registrationEntity);
            }

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

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

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

}