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

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

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.retailer.PurchaseOrderLocal;
import org.eu.adapt.demonstrator.eb.retailer.CustomerReferenceHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.CustomerReferenceLocal;
import org.eu.adapt.services.demo_1_1.retailer.CustomerReferenceBasicService_wsdl.types.CustomerReferenceState;
import org.eu.adapt.services.demo_1_1.retailer.CustomerReferenceBasicService_wsdl.CustomerReferenceBasicServicePortType;

public class CustomerReferenceBasicServiceBean implements SessionBean, CustomerReferenceBasicServicePortType
{
    public String[] listCustomerReferences()
       throws RemoteException
    {
        String[] customerReferenceNames = null;

        try
        {
            Collection customerReferences         = _customerReferenceHome.findAll();
            Iterator   customerReferencesIterator = customerReferences.iterator();

            customerReferenceNames = new String[customerReferences.size()];
            int index = 0;
            while (customerReferencesIterator.hasNext())
            {
                customerReferenceNames[index] = ((CustomerReferenceLocal) customerReferencesIterator.next()).getName();
                index++;
            }
        }
        catch (FinderException findException)
        {
            customerReferenceNames = new String[0];
            System.out.println("Unable to find all customer references: \"" + findException + "\"");
        }
        catch (Exception exception)
        {
            customerReferenceNames = new String[0];
            System.out.println("Exception: \"" + exception + "\"");
        }
        catch (Error error)
        {
            customerReferenceNames = new String[0];
            System.out.println("Error: \"" + error + "\"");
        }

        return customerReferenceNames;
    }

    public CustomerReferenceState getCustomerReferenceState(String customerReferenceName)
        throws RemoteException
    {
        CustomerReferenceState state = new CustomerReferenceState();
        
        try
        {
            CustomerReferenceLocal customerReference = _customerReferenceHome.findByPrimaryKey(customerReferenceName);
            
            state.setName(customerReference.getName());
            state.setStreet1(customerReference.getStreet1());
            state.setStreet2(customerReference.getStreet2());
            state.setCity(customerReference.getCity());
            state.setState(customerReference.getState());
            state.setZip(customerReference.getZip());
            state.setCountry(customerReference.getCountry());
            
            Collection purchaseOrders         = customerReference.getPurchaseOrders();
            Iterator   purchaseOrdersIterator = purchaseOrders.iterator();
            String[]   purchaseOrderNumbers   = new String[purchaseOrders.size()];
            int index = 0;
            while (purchaseOrdersIterator.hasNext())
            {
                purchaseOrderNumbers[index] = ((PurchaseOrderLocal) purchaseOrdersIterator.next()).getNumber();
                index++;
            }
            state.setPurchaseOrderNumbers(purchaseOrderNumbers);
       }
        catch (FinderException finderException)
        {
            System.out.println("Unable to find customerReference: \"" + customerReferenceName + "\"");
        }
        catch (Exception exception)
        {
            System.out.println("CustomerReferenceBasicServiceBean.getCustomerReferenceState exception: \"" + exception + "\"");
            throw new RemoteException("CustomerReferenceBasicServiceBean.getCustomerReferenceState exception", exception);
        }
        catch (Error error)
        {
            System.out.println("CustomerReferenceBasicServiceBean.getCustomerReferenceState error: \"" + error + "\"");
            throw new RemoteException("CustomerReferenceBasicServiceBean.getCustomerReferenceState error", error);
        }

        return state;
    }

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

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

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

//            _customerReferenceHome = (CustomerReferenceHomeLocal) initialContext.lookup("java:comp/env/ejb/CustomerReferenceHomeLocal");
            _customerReferenceHome = (CustomerReferenceHomeLocal) initialContext.lookup("local/Retailer_CustomerReferenceEB");

            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()
    {
        _customerReferenceHome = null;
    }

    private SessionContext _sessionContext = null;
    
    private transient CustomerReferenceHomeLocal _customerReferenceHome = null;
}