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

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

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.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.Configuration_xsd.*;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.Configurator_wsdl.ConfiguratorFailedFault;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_10.ManufacturerPO_xsd.*;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_10.Manufacturer_wsdl.*;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_10.Manufacturer.CallBack.*;
import org.eu.adapt.services.demo_1_1.manufacturer.PurchaseOrderBasicService_wsdl.PurchaseOrderBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.PurchaseOrderBasicService_wsdl.PurchaseOrderBasicServicePortType;

public class ManufacturerOrderServiceImpl implements ManufacturerPortType, ServiceLifecycle
{
    public ManufacturerOrderServiceImpl()
    {
    }

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

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

    public void destroy()
    {
        _purchaseOrderBasicServicePortType = null;
    }

    public boolean submitPO(PurchOrdType purchaseOrder, ConfigurationType configuration, StartHeaderType startHeader)
        throws RemoteException, ConfiguratorFailedFault, SubmitPOFaultType
    {
        boolean validPurchaseOrder = false;

        try
        {
            validPurchaseOrder = _purchaseOrderBasicServicePortType.checkPurchaseOrder(purchaseOrder);

            if (validPurchaseOrder)
                _purchaseOrderBasicServicePortType.addPurchaseOrder(purchaseOrder);
        }
        catch (Exception exception)
        {
            System.err.print("Exception: " + exception);
            exception.printStackTrace(System.err);
            throw new RemoteException("ManufacturerOrderServiceImpl submitPO failed", exception);
        }
        catch (Error error)
        {
            System.err.print("Error: " + error);
            error.printStackTrace(System.err);
            throw new RemoteException("ManufacturerOrderServiceImpl submitPO failed", error);
        }

    	return validPurchaseOrder;
    }

    private PurchaseOrderBasicServicePortType _purchaseOrderBasicServicePortType = null;
}
