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

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

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;
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.InventoryItemHomeLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.InventoryItemLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.StorehouseLocal;
import org.eu.adapt.services.demo_1_1.manufacturer.InventoryItemBasicService_wsdl.*;
import org.eu.adapt.services.demo_1_1.manufacturer.InventoryItemBasicService_wsdl.types.InventoryItemState;

public class InventoryItemBasicServiceBean implements SessionBean, InventoryItemBasicServicePortType
{
    public String[] listInventoryItems()
       throws RemoteException
    {
        String[] inventoryItemIDs = null;
        
        try
        {
            Collection inventoryItems         = _inventoryItemHome.findAll();
            Iterator   inventoryItemsIterator = inventoryItems.iterator();
            
            inventoryItemIDs = new String[inventoryItems.size()];
            int index = 0;
            while (inventoryItemsIterator.hasNext())
            {
                inventoryItemIDs[index] = ((InventoryItemLocal) inventoryItemsIterator.next()).getId();
                index++;
            }
        }
        catch (FinderException findException)
        {
            inventoryItemIDs = new String[0];
            System.out.println("Unable to find all inventory items: \"" + findException + "\"");
        }
        catch (Exception exception)
        {
            inventoryItemIDs = new String[0];
            System.out.println("Exception: \"" + exception + "\"");
        }
        catch (Error error)
        {
            inventoryItemIDs = new String[0];
            System.out.println("Error: \"" + error + "\"");
        }

        return inventoryItemIDs;
    }

    public InventoryItemState getInventoryItemState(String inventoryItemID)
        throws RemoteException
    {
        InventoryItemState state = new InventoryItemState();
        
        try
        {
            InventoryItemLocal inventoryItem = _inventoryItemHome.findByPrimaryKey(inventoryItemID);
            
            state.setAmountFree(inventoryItem.getAmountFree());
            state.setAmountAllocated(inventoryItem.getAmountAllocated());
            state.setMinAmount(inventoryItem.getMinAmount());
            state.setMaxAmount(inventoryItem.getMaxAmount());

            StorehouseLocal storehouse = inventoryItem.getStorehouse();
            if (storehouse != null)
                state.setStorehouseName(storehouse.getName());
            else
                state.setStorehouseName(null);

            FinishedGoodsLocal finishedGoods = inventoryItem.getFinishedGoods();
            if (finishedGoods != null)
                state.setFinishedGoodsName(finishedGoods.getId());
            else
                state.setFinishedGoodsName(null);
        }
        catch (FinderException finderException)
        {
            System.out.println("Unable to find inventory item: \"" + inventoryItemID + "\"");
        }
        catch (Exception exception)
        {
            System.out.println("InventoryItemBasicServiceBean.getInventoryItemState exception: \"" + exception + "\"");
            throw new RemoteException("InventoryItemBasicServiceBean.getInventoryItemState exception", exception);
        }
        catch (Error error)
        {
            System.out.println("InventoryItemBasicServiceBean.getInventoryItemState error: \"" + error + "\"");
            throw new RemoteException("InventoryItemBasicServiceBean.getInventoryItemState error", error);
        }

        return state;
    }

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

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

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

//            _inventoryItemHome = (InventoryItemHomeLocal) initialContext.lookup("java:comp/env/ejb/InventoryItemHomeLocal");
            _inventoryItemHome = (InventoryItemHomeLocal) initialContext.lookup("local/Manufacturer_InventoryItemEB");

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

    private SessionContext _sessionContext = null;
    
    private transient InventoryItemHomeLocal _inventoryItemHome = null;
}