/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 *
 * CatalogItemBasicServiceBean.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.CatalogItemHomeLocal;
import org.eu.adapt.demonstrator.eb.retailer.CatalogItemLocal;
import org.eu.adapt.demonstrator.eb.retailer.CatalogLocal;
import org.eu.adapt.demonstrator.eb.retailer.ProductLocal;
import org.eu.adapt.services.demo_1_1.retailer.CatalogItemBasicService_wsdl.types.CatalogItemState;
import org.eu.adapt.services.demo_1_1.retailer.CatalogItemBasicService_wsdl.CatalogItemBasicServicePortType;

public class CatalogItemBasicServiceBean implements SessionBean, CatalogItemBasicServicePortType
{
    public String[] listCatalogItems()
       throws RemoteException
    {
        String[] catalogItemNames = null;

        try
        {
            Collection catalogItems         = _catalogItemHome.findAll();
            Iterator   catalogItemsIterator = catalogItems.iterator();

            catalogItemNames = new String[catalogItems.size()];
            int index = 0;
            while (catalogItemsIterator.hasNext())
            {
                catalogItemNames[index] = ((CatalogItemLocal) catalogItemsIterator.next()).getId();
                index++;
            }
        }
        catch (FinderException findException)
        {
            catalogItemNames = new String[0];
            System.out.println("Unable to find all catalog items: \"" + findException + "\"");
        }
        catch (Exception exception)
        {
            catalogItemNames = new String[0];
            System.out.println("Exception: \"" + exception + "\"");
        }
        catch (Error error)
        {
            catalogItemNames = new String[0];
            System.out.println("Error: \"" + error + "\"");
        }

        return catalogItemNames;
    }

    public CatalogItemState getCatalogItemState(String catalogItemName)
        throws RemoteException
    {
        CatalogItemState state = new CatalogItemState();
        
        try
        {
            CatalogItemLocal catalogItem = _catalogItemHome.findByPrimaryKey(catalogItemName);
            
            state.setName(catalogItem.getName());
            state.setDescription(catalogItem.getDescription());
            state.setBrand(catalogItem.getBrand());
            state.setPrice(catalogItem.getPrice());

            CatalogLocal catalog = catalogItem.getCatalog();
            if (catalog != null)
                state.setCatalogName(catalog.getName());
            else
                state.setCatalogName(null);

            ProductLocal product = catalogItem.getProduct();
            if (product != null)
                state.setProductName(product.getName());
            else
                state.setProductName(null);
        }
        catch (FinderException finderException)
        {
            System.out.println("Unable to find catalogItem: \"" + catalogItemName + "\"");
        }
        catch (Exception exception)
        {
            System.out.println("CatalogItemBasicServiceBean.getCatalogItemState exception: \"" + exception + "\"");
            throw new RemoteException("CatalogItemBasicServiceBean.getCatalogItemState exception", exception);
        }
        catch (Error error)
        {
            System.out.println("CatalogItemBasicServiceBean.getCatalogItemState error: \"" + error + "\"");
            throw new RemoteException("CatalogItemBasicServiceBean.getCatalogItemState error", error);
        }

        return state;
    }

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

    public void unsetSessionContext()
    {
        _sessionContext = null;
    }

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

//            _catalogItemHome = (CatalogItemHomeLocal) initialContext.lookup("java:comp/env/ejb/CatalogItemHomeLocal");
            _catalogItemHome = (CatalogItemHomeLocal) initialContext.lookup("local/Retailer_CatalogItemEB");

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

    private SessionContext _sessionContext = null;
    
    private transient CatalogItemHomeLocal _catalogItemHome = null;
}