/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 *
 * StorehouseBasicServiceBean.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.StorehouseHomeLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.StorehouseLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.ItemLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.ProductionLineLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.InventoryItemLocal;
import org.eu.adapt.services.demo_1_1.manufacturer.StorehouseBasicService_wsdl.types.StorehouseState;
import org.eu.adapt.services.demo_1_1.manufacturer.StorehouseBasicService_wsdl.StorehouseBasicServicePortType;

public class StorehouseBasicServiceBean implements SessionBean, StorehouseBasicServicePortType
{
    public String[] listStorehouses()
       throws RemoteException
    {
        String[] shorehouseNames = null;
        
        try
        {
            Collection shorehouses         = _storehouseHome.findAll();
            Iterator   shorehousesIterator = shorehouses.iterator();
            
            shorehouseNames = new String[shorehouses.size()];
            int index = 0;
            while (shorehousesIterator.hasNext())
            {
                shorehouseNames[index] = ((StorehouseLocal) shorehousesIterator.next()).getName();
                index++;
            }
        }
        catch (FinderException findException)
        {
            shorehouseNames = new String[0];
            System.out.println("Unable to find all storehouses: \"" + findException + "\"");
        }
        catch (Exception exception)
        {
            shorehouseNames = new String[0];
            System.out.println("Exception: \"" + exception + "\"");
        }
        catch (Error error)
        {
            shorehouseNames = new String[0];
            System.out.println("Error: \"" + error + "\"");
        }

        return shorehouseNames;
    }

    public StorehouseState getStorehouseState(String storehouseName)
        throws RemoteException
    {
        StorehouseState state = new StorehouseState();
        
        try
        {
            StorehouseLocal storehouse = _storehouseHome.findByPrimaryKey(storehouseName);

            Collection inventoryItems         = storehouse.getInventoryItems();
            Iterator   inventoryItemsIterator = inventoryItems.iterator();
            String[]   inventoryItemIDs       = new String[inventoryItems.size()];
            int index = 0;
            while (inventoryItemsIterator.hasNext())
            {
                inventoryItemIDs[index] = ((InventoryItemLocal) inventoryItemsIterator.next()).getId();
                index++;
            }
            state.setInventoryItemIDs(inventoryItemIDs);

            Collection productionLines         = storehouse.getProductionLines();
            Iterator   productionLinesIterator = productionLines.iterator();
            String[]   productionLineNames     = new String[productionLines.size()];
            index = 0;
            while (productionLinesIterator.hasNext())
            {
                productionLineNames[index] = ((ProductionLineLocal) productionLinesIterator.next()).getName();
                index++;
            }
            state.setProductionLineNames(productionLineNames);

            Collection items         = storehouse.getItems();
            Iterator   itemsIterator = items.iterator();
            String[]   itemNames     = new String[items.size()];
            index = 0;
            while (itemsIterator.hasNext())
            {
                itemNames[index] = ((ItemLocal) itemsIterator.next()).getId();
                index++;
            }
            state.setItemIDs(itemNames);
        }
        catch (FinderException finderException)
        {
            System.out.println("Unable to find storehouse: \"" + storehouseName + "\"");
        }
        catch (Exception exception)
        {
            System.out.println("StorehouseBasicServiceBean.getStorehouseState exception: \"" + exception + "\"");
            throw new RemoteException("StorehouseBasicServiceBean.getStorehouseState exception", exception);
        }
        catch (Error error)
        {
            System.out.println("StorehouseBasicServiceBean.getStorehouseState error: \"" + error + "\"");
            throw new RemoteException("StorehouseBasicServiceBean.getStorehouseState error", error);
        }

        return state;
    }

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

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

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

//            _storehouseHome = (StorehouseHomeLocal) initialContext.lookup("java:comp/env/ejb/StorehouseHomeLocal");
            _storehouseHome = (StorehouseHomeLocal) initialContext.lookup("local/Manufacturer_StorehouseEB");

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

    private SessionContext _sessionContext = null;
    
    private transient StorehouseHomeLocal _storehouseHome = null;
}