/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 *
 * PurchaseOrderItemBasicServiceBean.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.PurchaseOrderItemHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.PurchaseOrderItemLocal;
import org.eu.adapt.demonstrator.eb.retailer.ProductLocal;
import org.eu.adapt.demonstrator.eb.retailer.PurchaseOrderLocal;
import org.eu.adapt.demonstrator.eb.retailer.WarehouseLocal;
import org.eu.adapt.services.demo_1_1.retailer.PurchaseOrderItemBasicService_wsdl.types.PurchaseOrderItemState;
import org.eu.adapt.services.demo_1_1.retailer.PurchaseOrderItemBasicService_wsdl.PurchaseOrderItemBasicServicePortType;

public class PurchaseOrderItemBasicServiceBean implements SessionBean, PurchaseOrderItemBasicServicePortType
{
    public String[] listPurchaseOrderItems()
       throws RemoteException
    {
        String[] purchaseOrderItemNames = null;

        try
        {
            Collection purchaseOrderItems         = _purchaseOrderItemHome.findAll();
            Iterator   purchaseOrderItemsIterator = purchaseOrderItems.iterator();

            purchaseOrderItemNames = new String[purchaseOrderItems.size()];
            int index = 0;
            while (purchaseOrderItemsIterator.hasNext())
            {
                purchaseOrderItemNames[index] = ((PurchaseOrderItemLocal) purchaseOrderItemsIterator.next()).getNumber();
                index++;
            }
        }
        catch (FinderException findException)
        {
            purchaseOrderItemNames = new String[0];
            System.out.println("Unable to find all purchaseOrderItems: \"" + findException + "\"");
        }
        catch (Exception exception)
        {
            purchaseOrderItemNames = new String[0];
            System.out.println("Exception: \"" + exception + "\"");
        }
        catch (Error error)
        {
            purchaseOrderItemNames = new String[0];
            System.out.println("Error: \"" + error + "\"");
        }

        return purchaseOrderItemNames;
    }

    public PurchaseOrderItemState getPurchaseOrderItemState(String purchaseOrderItemName)
        throws RemoteException
    {
        PurchaseOrderItemState state = new PurchaseOrderItemState();

        try
        {
            PurchaseOrderItemLocal purchaseOrderItem = _purchaseOrderItemHome.findByPrimaryKey(purchaseOrderItemName);

            state.setNumber(purchaseOrderItem.getNumber());
            state.setQuantity(purchaseOrderItem.getQuantity());
            state.setPrice(purchaseOrderItem.getPrice());

            int index = 0;

            ProductLocal product = purchaseOrderItem.getProduct();
            if (product != null)
                state.setProductName(product.getName());
            else
                state.setProductName(null);

            PurchaseOrderLocal purchaseOrder = purchaseOrderItem.getPurchaseOrder();
            if (purchaseOrder != null)
                state.setPurchaseOrderNumber(purchaseOrder.getNumber());
            else
                state.setPurchaseOrderNumber(null);

            WarehouseLocal warehouse = purchaseOrderItem.getWarehouse();
            if (warehouse != null)
                state.setWarehouseName(warehouse.getName());
            else
                state.setWarehouseName(null);
        }
        catch (FinderException finderException)
        {
            System.out.println("Unable to find purchaseOrderItem: \"" + purchaseOrderItemName + "\"");
        }
        catch (Exception exception)
        {
            System.out.println("PurchaseOrderItemBasicServiceBean.getPurchaseOrderItemState exception: \"" + exception + "\"");
            throw new RemoteException("PurchaseOrderItemBasicServiceBean.getPurchaseOrderItemState exception", exception);
        }
        catch (Error error)
        {
            System.out.println("PurchaseOrderItemBasicServiceBean.getPurchaseOrderItemState error: \"" + error + "\"");
            throw new RemoteException("PurchaseOrderItemBasicServiceBean.getPurchaseOrderItemState error", error);
        }

        return state;
    }

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

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

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

//            _purchaseOrderItemHome = (PurchaseOrderItemHomeLocal) initialContext.lookup("java:comp/env/ejb/PurchaseOrderItemHomeLocal");
            _purchaseOrderItemHome = (PurchaseOrderItemHomeLocal) initialContext.lookup("local/Retailer_PurchaseOrderItemEB");

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

    private SessionContext _sessionContext = null;
    
    private transient PurchaseOrderItemHomeLocal _purchaseOrderItemHome = null;
}