/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 *
 * FinishedGoodsBasicServiceBean.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.FinishedGoodsHomeLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.FinishedGoodsLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.InventoryItemLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.ItemLocal;
import org.eu.adapt.demonstrator.eb.manufacturer.ProductionLineLocal;
import org.eu.adapt.services.demo_1_1.manufacturer.FinishedGoodsBasicService_wsdl.types.FinishedGoodsState;
import org.eu.adapt.services.demo_1_1.manufacturer.FinishedGoodsBasicService_wsdl.FinishedGoodsBasicServicePortType;

public class FinishedGoodsBasicServiceBean implements SessionBean, FinishedGoodsBasicServicePortType
{
    public String[] listFinishedGoods()
       throws RemoteException
    {
        String[] finishedGoodsIDs = null;
        
        try
        {
            Collection finishedGoods         = _finishedGoodsHome.findAll();
            Iterator   finishedGoodsIterator = finishedGoods.iterator();
            
            finishedGoodsIDs = new String[finishedGoods.size()];
            int index = 0;
            while (finishedGoodsIterator.hasNext())
            {
                finishedGoodsIDs[index] = ((FinishedGoodsLocal) finishedGoodsIterator.next()).getId();
                index++;
            }
        }
        catch (FinderException findException)
        {
            finishedGoodsIDs = new String[0];
            System.out.println("Unable to find all finished goods: \"" + findException + "\"");
        }
        catch (Exception exception)
        {
            finishedGoodsIDs = new String[0];
            System.out.println("Exception: \"" + exception + "\"");
        }
        catch (Error error)
        {
            finishedGoodsIDs = new String[0];
            System.out.println("Error: \"" + error + "\"");
        }

        return finishedGoodsIDs;
    }

    public FinishedGoodsState getFinishedGoodsState(String finishedGoodsID)
        throws RemoteException
    {
        FinishedGoodsState state = new FinishedGoodsState();
        
        try
        {
            FinishedGoodsLocal finishedGoods = _finishedGoodsHome.findByPrimaryKey(finishedGoodsID);

            state.setId(finishedGoods.getId());

            state.setDescription(finishedGoods.getDescription());
            
            Collection productionLines         = finishedGoods.getProductionLines();
            Iterator   productionLinesIterator = productionLines.iterator();
            String[]   productionLineNames     = new String[productionLines.size()];
            int index = 0;
            while (productionLinesIterator.hasNext())
            {
                productionLineNames[index] = ((ProductionLineLocal) productionLinesIterator.next()).getName();
                index++;
            }
            state.setProductionLineNames(productionLineNames);
            
            Collection inventoryItems         = finishedGoods.getInventoryItems();
            Iterator   inventoryItemsIterator = inventoryItems.iterator();
            String[]   inventoryItemNames     = new String[inventoryItems.size()];
            index = 0;
            while (inventoryItemsIterator.hasNext())
            {
                inventoryItemNames[index] = ((InventoryItemLocal) inventoryItemsIterator.next()).getId();
                index++;
            }
            state.setInventoryItemIDs(inventoryItemNames);

            Collection items         = finishedGoods.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 finished good: \"" + finishedGoodsID + "\"");
        }
        catch (Exception exception)
        {
            System.out.println("FinishedGoodsBasicServiceBean.getFinishedGoodsState exception: \"" + exception + "\"");
            throw new RemoteException("FinishedGoodsBasicServiceBean.getFinishedGoodsState exception", exception);
        }
        catch (Error error)
        {
            System.out.println("FinishedGoodsBasicServiceBean.getFinishedGoodsState error: \"" + error + "\"");
            throw new RemoteException("FinishedGoodsBasicServiceBean.getFinishedGoodsState error", error);
        }

        return state;
    }

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

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

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

//            _finishedGoodsHome = (FinishedGoodsHomeLocal) initialContext.lookup("java:comp/env/ejb/FinishedGoodsHomeLocal");
            _finishedGoodsHome = (FinishedGoodsHomeLocal) initialContext.lookup("local/Manufacturer_FinishedGoodsEB");

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

    private SessionContext _sessionContext = null;
    
    private transient FinishedGoodsHomeLocal _finishedGoodsHome = null;
}