/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 *
 * ManufacturerBasicServiceBean.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.ManufacturerHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.ManufacturerLocal;
import org.eu.adapt.demonstrator.eb.retailer.ProductLocal;
import org.eu.adapt.services.demo_1_1.retailer.ManufacturerBasicService_wsdl.types.ManufacturerState;
import org.eu.adapt.services.demo_1_1.retailer.ManufacturerBasicService_wsdl.ManufacturerBasicServicePortType;

public class ManufacturerBasicServiceBean implements SessionBean, ManufacturerBasicServicePortType
{
    public String[] listManufacturers()
       throws RemoteException
    {
        String[] manufacturerNames = null;

        try
        {
            Collection manufacturers         = _manufacturerHome.findAll();
            Iterator   manufacturersIterator = manufacturers.iterator();

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

        return manufacturerNames;
    }

    public ManufacturerState getManufacturerState(String manufacturerName)
        throws RemoteException
    {
        ManufacturerState state = new ManufacturerState();

        try
        {
            ManufacturerLocal manufacturer = _manufacturerHome.findByPrimaryKey(manufacturerName);

            state.setName(manufacturer.getName());

            Collection products         = manufacturer.getProducts();
            Iterator   productsIterator = products.iterator();
            String[]   productNames     = new String[products.size()];
            int index = 0;
            while (productsIterator.hasNext())
            {
                productNames[index] = ((ProductLocal) productsIterator.next()).getName();
                index++;
            }
            state.setProductNames(productNames);
        }
        catch (FinderException finderException)
        {
            System.out.println("Unable to find manufacturer: \"" + manufacturerName + "\"");
        }
        catch (Exception exception)
        {
            System.out.println("ManufacturerBasicServiceBean.getManufacturerState exception: \"" + exception + "\"");
            throw new RemoteException("ManufacturerBasicServiceBean.getManufacturerState exception", exception);
        }
        catch (Error error)
        {
            System.out.println("ManufacturerBasicServiceBean.getManufacturerState error: \"" + error + "\"");
            throw new RemoteException("ManufacturerBasicServiceBean.getManufacturerState error", error);
        }

        return state;
    }

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

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

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

//            _manufacturerHome = (ManufacturerHomeLocal) initialContext.lookup("java:comp/env/ejb/ManufacturerHomeLocal");
            _manufacturerHome = (ManufacturerHomeLocal) initialContext.lookup("local/Retailer_ManufacturerEB");

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

    private SessionContext _sessionContext = null;
    
    private transient ManufacturerHomeLocal _manufacturerHome = null;
}