/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 *
 * CatalogBasicServiceBean.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.CatalogHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.CatalogItemLocal;
import org.eu.adapt.demonstrator.eb.retailer.CatalogLocal;
import org.eu.adapt.services.demo_1_1.retailer.CatalogBasicService_wsdl.types.CatalogState;
import org.eu.adapt.services.demo_1_1.retailer.CatalogBasicService_wsdl.CatalogBasicServicePortType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.RetailCatalog_xsd.CatalogItem;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.RetailCatalog_xsd.CatalogType;

public class CatalogBasicServiceBean implements SessionBean, CatalogBasicServicePortType
{
    public String[] listCatalogs()
       throws RemoteException
    {
        String[] catalogNames = null;

        try
        {
            Collection catalogs         = _catalogHome.findAll();
            Iterator   catalogsIterator = catalogs.iterator();

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

        return catalogNames;
    }

    public CatalogState getCatalogState(String catalogName)
        throws RemoteException
    {
        CatalogState state = new CatalogState();

        try
        {
            CatalogLocal catalog = _catalogHome.findByPrimaryKey(catalogName);

            state.setName(catalog.getName());

            Collection catalogItems         = catalog.getCatalogItems();
            Iterator   catalogItemsIterator = catalogItems.iterator();
            String[]   catalogItemNames     = new String[catalogItems.size()];
            int index = 0;
            while (catalogItemsIterator.hasNext())
            {
                catalogItemNames[index] = ((CatalogItemLocal) catalogItemsIterator.next()).getId();
                index++;
            }
            state.setCatalogItemNames(catalogItemNames);
       }
        catch (FinderException finderException)
        {
            System.out.println("Unable to find catalog: \"" + catalogName + "\"");
        }
        catch (Exception exception)
        {
            System.out.println("CatalogBasicServiceBean.getCatalogState exception: \"" + exception + "\"");
            throw new RemoteException("CatalogBasicServiceBean.getCatalogState exception", exception);
        }
        catch (Error error)
        {
            System.out.println("CatalogBasicServiceBean.getCatalogState error: \"" + error + "\"");
            throw new RemoteException("CatalogBasicServiceBean.getCatalogState error", error);
        }

        return state;
    }

    public CatalogType getCatalog()
        throws RemoteException
    {
        CatalogType   catalog      = new CatalogType();
        CatalogItem[] catalogItems = null;

        try
        {
            Collection catalogs         = _catalogHome.findAll();
            Iterator   catalogsIterator = catalogs.iterator();

            catalogItems = new CatalogItem[catalogs.size()];
            int index = 0;
            while (catalogsIterator.hasNext())
            {
                CatalogItem catalogItem = (CatalogItem) catalogsIterator.next();
                
                catalogItems[index] = new CatalogItem();
                catalogItems[index].setName(catalogItem.getName());
                catalogItems[index].setDescription(catalogItem.getDescription());
                catalogItems[index].setBrand(catalogItem.getBrand());
                catalogItems[index].setPrice(catalogItem.getPrice());

                index++;
            }
        }
        catch (FinderException findException)
        {
            catalogItems = new CatalogItem[0];
            System.out.println("Unable to find all catalogs: \"" + findException + "\"");
        }
        catch (Exception exception)
        {
            catalogItems = new CatalogItem[0];
            System.out.println("Exception: \"" + exception + "\"");
        }
        catch (Error error)
        {
            catalogItems = new CatalogItem[0];
            System.out.println("Error: \"" + error + "\"");
        }

        catalog.setItem(catalogItems);
        
        return catalog;
    }

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

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

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

//            _catalogHome = (CatalogHomeLocal) initialContext.lookup("java:comp/env/ejb/CatalogHomeLocal");
            _catalogHome = (CatalogHomeLocal) initialContext.lookup("local/Retailer_CatalogEB");

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

    private SessionContext _sessionContext = null;
    
    private transient CatalogHomeLocal _catalogHome = null;
}