/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * ManufacturerStockServiceImpl.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.eu.adapt.services.demo_1_1.ManufacturerService_wsdl.ManufacturerStockPortType;
import org.eu.adapt.services.demo_1_1.RetailerService_wsdl.RetailerServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.PurchaseOrderBasicService_wsdl.PurchaseOrderBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.PurchaseOrderBasicService_wsdl.PurchaseOrderBasicServicePortType;
import org.eu.adapt.services.demo_1_1.manufacturer.PurchaseOrderBasicService_wsdl.types.PurchaseOrderStatus;
import org.eu.adapt.services.demo_1_1.manufacturer.ItemBasicService_wsdl.ItemBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.ItemBasicService_wsdl.ItemBasicServicePortType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_08.Configuration_xsd.ConfigurationType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_10.Manufacturer.CallBack.CallbackHeaderType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_10.ManufacturerSN_xsd.ShipmentNoticeType;
import org.ws_i.www.SampleApplications.SupplyChainManagement._2002_10.Manufacturer_wsdl.WarehouseCallbackPortType;

public class ManufacturerStockServiceImpl implements ManufacturerStockPortType, ServiceLifecycle
{
    public ManufacturerStockServiceImpl()
    {
    }

    public void init(Object context)
        throws ServiceException
    {
        try
        {
            MessageContext                   messageContext                     = ((ServletEndpointContext) context).getMessageContext();
            String                           purchaseOrderBasicServiceURLString = (String) messageContext.getProperty("PurchaseOrderBasicServiceURL");
            String                           itemBasicServiceURLString          = (String) messageContext.getProperty("ItemBasicServiceURL");
            String                           warehouseCallbackServiceURLString  = (String) messageContext.getProperty("WarehouseCallbackServiceURL");
            PurchaseOrderBasicServiceLocator purchaseOrderBasicServiceLocator   = new PurchaseOrderBasicServiceLocator();
            ItemBasicServiceLocator          itemBasicServiceLocator            = new ItemBasicServiceLocator();
            RetailerServiceLocator           retailerServiceLocator             = new RetailerServiceLocator();

            _purchaseOrderBasicServicePortType = purchaseOrderBasicServiceLocator.getPurchaseOrder(new URL(purchaseOrderBasicServiceURLString));
            _itemBasicServicePortType          = itemBasicServiceLocator.getItem(new URL(itemBasicServiceURLString));
            _warehouseCallbackPortType         = retailerServiceLocator.getWarehouseCallback(new URL(warehouseCallbackServiceURLString));
        }
        catch (Exception exception)
        {  
            System.err.println("Exception: " + exception);
            exception.printStackTrace(System.err);
            throw new ServiceException("ManufacturerStockServiceImpl init failed", exception);
        }
        catch (Error error)
        {
            System.err.println("Error: " + error);
            error.printStackTrace(System.err);
            throw new ServiceException("ManufacturerStockServiceImpl init failed", error);
        }
    }

    public void destroy()
    {
        _purchaseOrderBasicServicePortType = null;
    }

    public boolean checkPurchaseOrder(String purchaseOrderName)
        throws RemoteException
    {
        boolean done = false;

        try
        {
            PurchaseOrderStatus purchaseOrderStatus = _purchaseOrderBasicServicePortType.updatePurchaseOrder(purchaseOrderName);

            if (PurchaseOrderStatus.NOW_COMPLETE.equals(purchaseOrderStatus))
            {
                ShipmentNoticeType shipmentNotice = _purchaseOrderBasicServicePortType.createShipmentNotice(purchaseOrderName);
                ConfigurationType  configuration  = new ConfigurationType();
                CallbackHeaderType callbackHeader = new CallbackHeaderType(); 

                done = _warehouseCallbackPortType.submitSN(shipmentNotice, configuration, callbackHeader);
            }
        }
        catch (Exception exception)
        {
            System.err.println("Exception in \"checkPurchaseOrder\": " + exception);
            exception.printStackTrace(System.err);
        }
        catch (Error error)
        {
            System.err.println("Error in \"checkPurchaseOrder\": " + error);
            error.printStackTrace(System.err);
        }

        return done;
    }

    private PurchaseOrderBasicServicePortType _purchaseOrderBasicServicePortType = null;
    private ItemBasicServicePortType          _itemBasicServicePortType          = null;
    private WarehouseCallbackPortType         _warehouseCallbackPortType         = null;
}
