/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * RetailerServiceImpl.java
 */

package org.eu.adapt.demonstrator.ws.retailer;

import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.server.ServiceLifecycle;
import javax.xml.rpc.server.ServletEndpointContext;
import org.eu.adapt.services.demo_1_1.retailer.CatalogBasicService_wsdl.CatalogBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.CatalogBasicService_wsdl.CatalogBasicServicePortType;
import org.eu.adapt.services.demo_1_1.retailer.PurchaseOrderBasicService_wsdl.PurchaseOrderBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.PurchaseOrderBasicService_wsdl.PurchaseOrderBasicServicePortType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.Configuration_xsd.*;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.RetailCatalog_xsd.CatalogType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.RetailOrder_xsd.InvalidProductCodeType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.RetailOrder_xsd.PartsOrderType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.RetailOrder_xsd.CustomerDetailsType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.RetailOrder_xsd.PartsOrderResponseType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.Retailer_wsdl.*;

public class RetailerServiceImpl implements RetailerPortType, ServiceLifecycle
{
    public RetailerServiceImpl()
    {
    }

    public void init(Object context)
        throws ServiceException
    {
        try
        {
            MessageContext                   messageContext                     = ((ServletEndpointContext) context).getMessageContext();
            String                           catalogBasicServiceURLString       = (String) messageContext.getProperty("CatalogBasicServiceURL");
            String                           purchaseOrderBasicServiceURLString = (String) messageContext.getProperty("PurchaseOrderBasicServiceURL");
            CatalogBasicServiceLocator       catalogBasicServiceLocator         = new CatalogBasicServiceLocator();
            PurchaseOrderBasicServiceLocator purchaseOrderBasicServiceLocator   = new PurchaseOrderBasicServiceLocator();

            _catalogBasicServicePortType       = catalogBasicServiceLocator.getCatalog(new URL(catalogBasicServiceURLString));
            _purchaseOrderBasicServicePortType = purchaseOrderBasicServiceLocator.getPurchaseOrder(new URL(purchaseOrderBasicServiceURLString));
        }
        catch (Exception exception)
        {
            throw new ServiceException("RetailerServiceImpl init failed", exception);
        }
        catch (Error error)
        {
            throw new ServiceException("RetailerServiceImpl init failed", error);
        }
    }

    public void destroy()
    {
        _catalogBasicServicePortType = null;
    }
    
    public CatalogType getCatalog()
        throws RemoteException
    {
        return _catalogBasicServicePortType.getCatalog();
    }

    public PartsOrderResponseType submitOrder(PartsOrderType partsOrder, CustomerDetailsType customerDetails, ConfigurationType configurationHeader)
        throws RemoteException, InvalidProductCodeType, BadOrderFault, ConfigurationFaultType
    {
        PartsOrderResponseType partsOrderResponse = new PartsOrderResponseType();
        
        try
        {
            if (_purchaseOrderBasicServicePortType.checkPurchaseOrder(partsOrder))
                partsOrderResponse = _purchaseOrderBasicServicePortType.addPurchaseOrder(partsOrder);
            else
                partsOrderResponse = _purchaseOrderBasicServicePortType.createEmptyPartsOrderResponse();
        }
        catch (Exception exception)
        {
            System.err.print("Exception: " + exception);
            exception.printStackTrace(System.err);
            throw new RemoteException("RetailerServiceImpl submitOrder failed", exception);
        }
        catch (Error error)
        {
            System.err.print("Error: " + error);
            error.printStackTrace(System.err);
            throw new RemoteException("RetailerServiceImpl submitOrder failed", error);
        }

        return partsOrderResponse;
    }

    private CatalogBasicServicePortType       _catalogBasicServicePortType       = null;
    private PurchaseOrderBasicServicePortType _purchaseOrderBasicServicePortType = null;
}
