/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 *
 * InventoryBasicServiceBean.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.InventoryHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.InventoryLocal;
import org.eu.adapt.demonstrator.eb.retailer.ProductLocal;
import org.eu.adapt.demonstrator.eb.retailer.WarehouseLocal;
import org.eu.adapt.services.demo_1_1.retailer.InventoryBasicService_wsdl.InventoryBasicServicePortType;
import org.eu.adapt.services.demo_1_1.retailer.InventoryBasicService_wsdl.types.InventoryState;

public class InventoryBasicServiceBean implements SessionBean, InventoryBasicServicePortType
{
    public String[] listInventories()
       throws RemoteException
    {
        String[] inventoryNames = null;

        try
        {
            Collection inventories         = _inventoryHome.findAll();
            Iterator   inventoriesIterator = inventories.iterator();

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

        return inventoryNames;
    }

    public InventoryState getInventoryState(String inventoryName)
        throws RemoteException
    {
        InventoryState state = new InventoryState();
        
        try
        {
            InventoryLocal inventory = _inventoryHome.findByPrimaryKey(inventoryName);
            
            state.setNumber(inventory.getNumber());
            state.setAmount(inventory.getAmount());
            state.setMinAmount(inventory.getMinAmount());
            state.setMaxAmount(inventory.getMaxAmount());

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

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

        return state;
    }

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

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

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

//            _inventoryHome = (InventoryHomeLocal) initialContext.lookup("java:comp/env/ejb/InventoryHomeLocal");
            _inventoryHome = (InventoryHomeLocal) initialContext.lookup("local/Retailer_InventoryEB");

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

    private SessionContext _sessionContext = null;
    
    private transient InventoryHomeLocal _inventoryHome = null;
}