/*
 * Copyright (c) 2004, ADAPT project partners (IST-2001-37126).
 * 
 * WarehouseCallbackServiceImpl.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.ProductBasicService_wsdl.ProductBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.ProductBasicService_wsdl.ProductBasicServicePortType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.Configuration_xsd.*;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_10.ManufacturerPO_xsd.SubmitPOFaultType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_10.ManufacturerSN_xsd.ShipmentNoticeType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_10.ManufacturerSN_xsd.Item;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_10.Manufacturer.CallBack.*;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_10.Manufacturer_wsdl.*;

public class WarehouseCallbackServiceImpl implements WarehouseCallbackPortType, ServiceLifecycle
{
    public WarehouseCallbackServiceImpl()
    {
    }

    public void init(Object context)
        throws ServiceException
    {
        try
        {
            MessageContext             messageContext               = ((ServletEndpointContext) context).getMessageContext();
            String                     productBasicServiceURLString = (String) messageContext.getProperty("ProductBasicServiceURL");
            ProductBasicServiceLocator productBasicServiceLocator   = new ProductBasicServiceLocator();

            _productBasicServicePortType = productBasicServiceLocator.getProduct(new URL(productBasicServiceURLString));
        }
        catch (Exception exception)
        {
            throw new ServiceException("WarehouseCallbackServiceImpl init failed", exception);
        }
        catch (Error error)
        {
            throw new ServiceException("WarehouseCallbackServiceImpl init failed", error);
        }
    }

    public void destroy()
    {
        _productBasicServicePortType = null;
    }
    
    public boolean submitSN(ShipmentNoticeType shipmentNotice, ConfigurationType configurationHeader, CallbackHeaderType callbackHeader)
        throws RemoteException, ConfigurationFaultType, CallbackFaultType
    {
        boolean result = false;

        try
        {
            Item[] items = shipmentNotice.getItems().getItem();
 
            for (int index = 0; index < items.length; index++)
                _productBasicServicePortType.addStock(items[index].getID(), items[index].getQty());

            result = true;
        }
        catch (Exception exception)
        {
            System.err.println("Exception in submitSN: " + exception);
            exception.printStackTrace(System.err);
        }
        catch (Error error)
        {
            System.err.println("Error in submitSN: " + error);
            error.printStackTrace(System.err);
        }
        
    	return result;
    }

    public boolean errorPO(SubmitPOFaultType processPOFault, ConfigurationType configurationHeader, CallbackHeaderType callbackHeader)
        throws RemoteException, ConfigurationFaultType, CallbackFaultType
    {
        boolean result = false;

        try
        {
            result = true;
        }
        catch (Exception exception)
        {
            System.err.println("Exception in errorPO: " + exception);
            exception.printStackTrace(System.err);
        }
        catch (Error error)
        {
            System.err.println("Error in errorPO: " + error);
            error.printStackTrace(System.err);
        }
        
    	return result;
    }

    private ProductBasicServicePortType _productBasicServicePortType = null;
}
