/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 *
 * CustomerBasicServiceBean.java
 */

package org.eu.adapt.demonstrator.bs.manufacturer;

import java.util.Collection;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.ejb.FinderException;
import javax.ejb.SessionContext;
import javax.ejb.SessionBean;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.eu.adapt.demonstrator.eb.manufacturer.CustomerHomeLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.CustomerLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.PurchaseOrderLocal;
import org.eu.adapt.services.demo_1_1.manufacturer.CustomerBasicService_wsdl.types.CustomerState;
import org.eu.adapt.services.demo_1_1.manufacturer.CustomerBasicService_wsdl.CustomerBasicServicePortType;

public class CustomerBasicServiceBean implements SessionBean, CustomerBasicServicePortType
{
    public String[] listCustomers()
       throws RemoteException
    {
        String[] customerReferences = null;
        
        try
        {
            Collection customers         = _customerHome.findAll();
            Iterator   customersIterator = customers.iterator();
            
            customerReferences = new String[customers.size()];
            int index = 0;
            while (customersIterator.hasNext())
            {
                customerReferences[index] = ((CustomerLocal) customersIterator.next()).getReference();
                index++;
            }
        }
        catch (FinderException findException)
        {
            customerReferences = new String[0];
            System.out.println("Unable to find all customers: \"" + findException + "\"");
        }
        catch (Exception exception)
        {
            customerReferences = new String[0];
            System.out.println("Exception: \"" + exception + "\"");
        }
        catch (Error error)
        {
            customerReferences = new String[0];
            System.out.println("Error: \"" + error + "\"");
        }

        return customerReferences;
    }

    public CustomerState getCustomerState(String customerName)
        throws RemoteException
    {
        CustomerState state = new CustomerState();
        
        try
        {
            CustomerLocal customer = _customerHome.findByPrimaryKey(customerName);
            
            state.setReference(customer.getReference());
            state.setName(customer.getName());
            
            Collection purchaseOrders         = customer.getPurchaseOrders();
            Iterator   purchaseOrdersIterator = purchaseOrders.iterator();
            String[]   purchaseOrderNumbers   = new String[purchaseOrders.size()];
            int index = 0;
            while (purchaseOrdersIterator.hasNext())
            {
                purchaseOrderNumbers[index] = ((PurchaseOrderLocal) purchaseOrdersIterator.next()).getOrderNumber();
                index++;
            }
            state.setPurchaseOrderNumbers(purchaseOrderNumbers);
       }
        catch (FinderException finderException)
        {
            System.out.println("Unable to find customer: \"" + customerName + "\"");
        }
        catch (Exception exception)
        {
            System.out.println("CustomerBasicServiceBean.getCustomerState exception: \"" + exception + "\"");
            throw new RemoteException("CustomerBasicServiceBean.getCustomerState exception", exception);
        }
        catch (Error error)
        {
            System.out.println("CustomerBasicServiceBean.getCustomerState error: \"" + error + "\"");
            throw new RemoteException("CustomerBasicServiceBean.getCustomerState error", error);
        }

        return state;
    }

    public void setSessionContext(SessionContext sessionContext)
    {
        _sessionContext = sessionContext;
    }

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

    public void ejbCreate()
    {
        try
        {
            InitialContext initialContext = new InitialContext();

//            _customerHome = (CustomerHomeLocal) initialContext.lookup("java:comp/env/ejb/CustomerHomeLocal");
            _customerHome = (CustomerHomeLocal) initialContext.lookup("local/Manufacturer_CustomerEB");

            initialContext.close();
        }
        catch (NamingException namingException)
        {
            System.out.println("Name lookup problem: \"" + namingException + "\"");
            namingException.printStackTrace(System.out);
        }
        catch (Exception exception)
        {
            System.out.println("Problem: \"" + exception + "\"");
            exception.printStackTrace(System.out);
        }
        catch (Error error)
        {
            System.out.println("Problem: \"" + error + "\"");
            error.printStackTrace(System.out);
        }
    }

    public void ejbActivate()
    {
    }

    public void ejbPassivate()
    {
    }

    public void ejbLoad()
    {
    }

    public void ejbStore()
    {
    }    

    public void ejbRemove()
    {
        _customerHome = null;
    }

    private SessionContext _sessionContext = null;
    
    private transient CustomerHomeLocal _customerHome = null;
}