package com.alodar.example.ejb;

/**
 * @author Mark Lui
 *
 * @ejb:bean
 *    type="Stateless"
 *    name="CustomerBO"
 *    jndi-name="com.alodar.example.interfaces.CustomerBO"
 *    view-type="local"
 *
 * @ejb:transaction type="Required"
 * @ejb:transaction-type type="Container"
 *
 * @jboss-net:web-service urn="CustomerBO"
 *                        expose-all="true"
 *
 */
public class CustomerBOBean implements javax.ejb.SessionBean
{

    /**
    * echo test method
    *
    * @param value - String value to return
    *
    * @ejb:interface-method view-type="local"
    */
    public String echo(String value)
    {
        return value;
    }

    /**
    * returns String value of com.alodar.example.interfaces.CustomerData
    *
    * @param customerData - data object
    *
    * @ejb:interface-method view-type="local"
    */
    public String test(com.alodar.example.interfaces.CustomerData customerData)
    {
        return customerData.toString();

    }

    /**
    * get Customer data object
    *
    * @param key - primary key
    *
    * @ejb:interface-method view-type="local"
    */
    public com.alodar.example.interfaces.CustomerData get(int key) throws CustomerBOException
    {

        try
        {

            com.alodar.example.interfaces.CustomerLocalHome customerHome =
                    (com.alodar.example.interfaces.CustomerLocalHome)com.alodar.example.interfaces.CustomerUtil.getLocalHome();
            com.alodar.example.interfaces.CustomerLocal customer =
                    customerHome.findByPrimaryKey(new com.alodar.example.interfaces.CustomerPK(new Integer(key)));

            return customer.getData();

        }
        catch(Exception e)
        {
            throw new CustomerBOException(e.getMessage());
        }
    }

    /**
    * creates new Customer
    *
    * @param customerData - data object
    *
    * @ejb:interface-method view-type="local"
    */
    public void create(com.alodar.example.interfaces.CustomerData customerData)
            throws CustomerBOException
    {

        try
        {

            com.alodar.example.interfaces.CustomerLocalHome customerHome =
                    (com.alodar.example.interfaces.CustomerLocalHome)com.alodar.example.interfaces.CustomerUtil.getLocalHome();
            customerHome.create(customerData);

        }
        catch(Exception e)
        {
            throw new CustomerBOException(e.getMessage());
        }

    }
    
    /**
    * Adds Item
    *
    * @param itemData - data object
    * @param key - Customer primary key
    *
    * @ejb:interface-method view-type="local"
    */
    public void addItem(com.alodar.example.interfaces.ItemData itemData,
            int key) throws CustomerBOException
    {
        try
        {
            com.alodar.example.interfaces.CustomerLocalHome customerHome =
                    (com.alodar.example.interfaces.CustomerLocalHome)com.alodar.example.interfaces.CustomerUtil.getLocalHome();
            com.alodar.example.interfaces.CustomerLocal customer =
                    customerHome.findByPrimaryKey(new com.alodar.example.interfaces.CustomerPK(new Integer(key)));
            customer.addItem(itemData);
        }
        catch(Exception e)
        {
            throw new CustomerBOException(e.getMessage());
        }

    }
    
    /**
    * Sets Address
    *
    * @param addressData - data object
    * @param key - Customer primary key
    *
    * @ejb:interface-method view-type="local"
    */
    public void setAddress(com.alodar.example.interfaces.AddressData addressData,
            int key) throws CustomerBOException
    {
        try
        {
            com.alodar.example.interfaces.CustomerLocalHome customerHome =
                    (com.alodar.example.interfaces.CustomerLocalHome)com.alodar.example.interfaces.CustomerUtil.getLocalHome();
            com.alodar.example.interfaces.CustomerLocal customer =
                    customerHome.findByPrimaryKey(new com.alodar.example.interfaces.CustomerPK(new Integer(key)));
            customer.setAddress(addressData);
        }
        catch(Exception e)
        {
            throw new CustomerBOException(e.getMessage());
        }

    }
    
    /**
    * updates Customer
    *
    * @param customerData - data object
    *
    * @ejb:interface-method view-type="local"
    */
    public void update(com.alodar.example.interfaces.CustomerData customerData)
            throws CustomerBOException
    {

        try
        {

            com.alodar.example.interfaces.CustomerLocalHome customerHome =
                    (com.alodar.example.interfaces.CustomerLocalHome)com.alodar.example.interfaces.CustomerUtil.getLocalHome();
            com.alodar.example.interfaces.CustomerLocal customer =
                    customerHome.findByPrimaryKey(new com.alodar.example.interfaces.CustomerPK(customerData.getCustomerId()));
            customer.setData(customerData);
        }
        catch(Exception e)
        {
            throw new CustomerBOException(e.getMessage());
        }

    }

    /**
    * The container invokes this method when the instance is taken out of the pool of
    * available instances to become associated with a specific EJB object.
    */
    public void ejbActivate()
    {
    }

    /**
    * The container invokes this method on an instance before the instance becomes
    * disassociated with a specific EJB object.
    */
    public void ejbPassivate()
    {
    }

    /**
    * The container invokes this method before it removes the EJB object that is
    * currently associated with the instance.
    */
    public void ejbRemove()
    {
    }

    /**
    * The container invokes setSessionContext just once - when it creates the bean instance.
    */
    public void setSessionContext(javax.ejb.SessionContext sessionContext) {}
}