/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 *
 * ItemBasicServiceBean.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.FinishedGoodsLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.ItemHomeLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.ItemLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.PurchaseOrderLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.StorehouseLocal;
import org.eu.adapt.services.demo_1_1.manufacturer.ItemBasicService_wsdl.types.ItemState;
import org.eu.adapt.services.demo_1_1.manufacturer.ItemBasicService_wsdl.ItemBasicServicePortType;

public class ItemBasicServiceBean implements SessionBean, ItemBasicServicePortType
{
    public String[] listItems()
       throws RemoteException
    {
        String[] itemIDs = null;
        
        try
        {
            Collection items         = _itemHome.findAll();
            Iterator   itemsIterator = items.iterator();

            itemIDs = new String[items.size()];
            int index = 0;
            while (itemsIterator.hasNext())
            {
                itemIDs[index] = ((ItemLocal) itemsIterator.next()).getId();
                index++;
            }
        }
        catch (FinderException findException)
        {
            itemIDs = new String[0];
            System.out.println("Unable to find all items: \"" + findException + "\"");
        }
        catch (Exception exception)
        {
            itemIDs = new String[0];
            System.out.println("Exception: \"" + exception + "\"");
        }
        catch (Error error)
        {
            itemIDs = new String[0];
            System.out.println("Error: \"" + error + "\"");
        }

        return itemIDs;
    }

    public ItemState getItemState(String itemID)
        throws RemoteException
    {
        ItemState state = new ItemState();
        
        try
        {
            ItemLocal item = _itemHome.findByPrimaryKey(itemID);
            
            state.setId(item.getId());
            state.setQuantity(item.getQuantity());
            state.setPrice(item.getPrice());
            
            PurchaseOrderLocal purchaseOrder = item.getPurchaseOrder();
            if (purchaseOrder != null)
                state.setPurchaseOrderNumber(purchaseOrder.getOrderNumber());
            else
                state.setPurchaseOrderNumber(null);

            FinishedGoodsLocal finishedGoods = item.getFinishedGoods();
            if (finishedGoods != null)
                state.setFinishGoodsName(finishedGoods.getId());
            else
                state.setFinishGoodsName(null);

            StorehouseLocal storehouse = item.getStorehouse();
            if (storehouse != null)
                state.setStorehouseName(storehouse.getName());
            else
                state.setStorehouseName(null);
        }
        catch (FinderException finderException)
        {
            System.out.println("Unable to find item: \"" + itemID + "\"");
        }
        catch (Exception exception)
        {
            System.out.println("ItemBasicServiceBean.getItemState exception: \"" + exception + "\"");
            throw new RemoteException("ItemBasicServiceBean.getItemState exception", exception);
        }
        catch (Error error)
        {
            System.out.println("ItemBasicServiceBean.getItemState error: \"" + error + "\"");
            throw new RemoteException("ItemBasicServiceBean.getItemState error", error);
        }

        return state;
    }

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

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

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

//            _itemHome = (ItemHomeLocal) initialContext.lookup("java:comp/env/ejb/ItemHomeLocal");
            _itemHome = (ItemHomeLocal) initialContext.lookup("local/Manufacturer_ItemEB");

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

    private SessionContext _sessionContext = null;
    
    private transient ItemHomeLocal _itemHome = null;
}