/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 *
 * WarehouseBasicServiceBean.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.InventoryLocal;
import org.eu.adapt.demonstrator.eb.retailer.PurchaseOrderItemLocal;
import org.eu.adapt.demonstrator.eb.retailer.WarehouseHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.WarehouseLocal;
import org.eu.adapt.demonstrator.eb.retailer.ProductLocal;
import org.eu.adapt.services.demo_1_1.retailer.WarehouseBasicService_wsdl.types.WarehouseState;
import org.eu.adapt.services.demo_1_1.retailer.WarehouseBasicService_wsdl.WarehouseBasicServicePortType;

public class WarehouseBasicServiceBean implements SessionBean, WarehouseBasicServicePortType
{
    public String[] listWarehouses()
       throws RemoteException
    {
        String[] warehouseNames = null;

        try
        {
            Collection warehouses         = _warehouseHome.findAll();
            Iterator   warehousesIterator = warehouses.iterator();

            warehouseNames = new String[warehouses.size()];
            int index = 0;
            while (warehousesIterator.hasNext())
            {
                warehouseNames[index] = ((WarehouseLocal) warehousesIterator.next()).getName();
                index++;
            }
        }
        catch (FinderException findException)
        {
            warehouseNames = new String[0];
            System.out.println("Unable to find all warehouses: \"" + findException + "\"");
        }
        catch (Exception exception)
        {
            warehouseNames = new String[0];
            System.out.println("Exception: \"" + exception + "\"");
        }
        catch (Error error)
        {
            warehouseNames = new String[0];
            System.out.println("Error: \"" + error + "\"");
        }

        return warehouseNames;
    }

    public WarehouseState getWarehouseState(String warehouseName)
        throws RemoteException
    {
        WarehouseState state = new WarehouseState();

        try
        {
            WarehouseLocal warehouse = _warehouseHome.findByPrimaryKey(warehouseName);

            state.setName(warehouse.getName());

            int index = 0;

            Collection products         = warehouse.getProducts();
            Iterator   productsIterator = products.iterator();
            String[]   productNames     = new String[products.size()];
            index = 0;
            while (productsIterator.hasNext())
            {
                productNames[index] = ((ProductLocal) productsIterator.next()).getName();
                index++;
            }
            state.setProductNames(productNames);

            Collection purchaseOrderItems         = warehouse.getPurchaseOrderItems();
            Iterator   purchaseOrderItemsIterator = purchaseOrderItems.iterator();
            String[]   purchaseOrderItemNumbers   = new String[purchaseOrderItems.size()];
            index = 0;
            while (purchaseOrderItemsIterator.hasNext())
            {
                purchaseOrderItemNumbers[index] = ((PurchaseOrderItemLocal) purchaseOrderItemsIterator.next()).getNumber();
                index++;
            }
            state.setPurchaseOrderItemNumbers(purchaseOrderItemNumbers);

            Collection inventories         = warehouse.getInventories();
            Iterator   inventoriesIterator = inventories.iterator();
            String[]   inventoryNumbers    = new String[inventories.size()];
            index = 0;
            while (inventoriesIterator.hasNext())
            {
                inventoryNumbers[index] = ((InventoryLocal) inventoriesIterator.next()).getNumber();
                index++;
            }
            state.setInventoryNumbers(inventoryNumbers);
        }
        catch (FinderException finderException)
        {
            System.out.println("Unable to find warehouse: \"" + warehouseName + "\"");
        }
        catch (Exception exception)
        {
            System.out.println("WarehouseBasicServiceBean.getWarehouseState exception: \"" + exception + "\"");
            throw new RemoteException("WarehouseBasicServiceBean.getWarehouseState exception", exception);
        }
        catch (Error error)
        {
            System.out.println("WarehouseBasicServiceBean.getWarehouseState error: \"" + error + "\"");
            throw new RemoteException("WarehouseBasicServiceBean.getWarehouseState error", error);
        }

        return state;
    }

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

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

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

//            _warehouseHome = (WarehouseHomeLocal) initialContext.lookup("java:comp/env/ejb/WarehouseHomeLocal");
            _warehouseHome = (WarehouseHomeLocal) initialContext.lookup("local/Retailer_WarehouseEB");

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

    private SessionContext _sessionContext = null;
    
    private transient WarehouseHomeLocal _warehouseHome = null;
}