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

package org.eu.adapt.demonstrator.web.demo;

import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.rpc.ServiceException;
import org.eu.adapt.demonstrator.web.ADAPTLookAndFeel;
import org.eu.adapt.demonstrator.web.Navigation;
import org.eu.adapt.services.demo_1_1.ManufacturerService_wsdl.ManufacturerServiceLocator;
import org.eu.adapt.services.demo_1_1.ManufacturerService_wsdl.ManufacturerStockPortType;
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.PurchaseOrderState;

public class CheckStockServlet extends HttpServlet
{
    public void init()
        throws ServletException
    {
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException 
    {
        response.setContentType("text/html");
        response.setHeader("Cache-Control", "no-cache");

        PrintWriter writer = response.getWriter();

        ADAPTLookAndFeel.generatePageHeader("..", writer);
        Navigation.generateNavigationHeader("..", writer);

        try
        {
            String                            purchaseOrderName                  = request.getParameter("name");
            String                            purchaseOrderBasicServiceURLString = getInitParameter("PurchaseOrderBasicServiceURL");
            PurchaseOrderBasicServiceLocator  purchaseOrderBasicServiceLocator   = new PurchaseOrderBasicServiceLocator();
            PurchaseOrderBasicServicePortType purchaseOrderBasicServicePortType  = purchaseOrderBasicServiceLocator.getPurchaseOrder(new URL(purchaseOrderBasicServiceURLString));

            if (purchaseOrderName == null)
            {
                String[] purchaseOrderNames = purchaseOrderBasicServicePortType.listPurchaseOrders();
                
                ADAPTLookAndFeel.generateTableHeader("Purchase Order Check Events", null, writer);
                ADAPTLookAndFeel.generateTableFooter(writer);

                for (int purchaseOrderNameIndex = 0; (purchaseOrderNames != null) && (purchaseOrderNameIndex < purchaseOrderNames.length); purchaseOrderNameIndex++)
                {
                   String currentPurchaseOrderName = purchaseOrderNames[purchaseOrderNameIndex];
                   PurchaseOrderState state = purchaseOrderBasicServicePortType.getPurchaseOrderState(currentPurchaseOrderName);

                   ADAPTLookAndFeel.generateTableHeader("Purchase Orders", null, writer);
                   ADAPTLookAndFeel.generateTableEntry(currentPurchaseOrderName, "CheckStock.html?name=" + URLEncoder.encode(currentPurchaseOrderName, "UTF-8"), writer);

                   String customerReference = state.getCustomerReference();
                   if (customerReference != null)
                       ADAPTLookAndFeel.generateTableEntry("Customer Reference", null, customerReference, null, writer);
                   else
                       ADAPTLookAndFeel.generateTableEntry("Customer Reference", null, "N/A", null, writer);

                   String[] itemIDs = state.getItemIDs();
                   for (int itemIndex = 0; (itemIDs != null) && (itemIndex < itemIDs.length); itemIndex++)
                       ADAPTLookAndFeel.generateTableEntry(String.valueOf(itemIndex), null, itemIDs[itemIndex], null, writer);

                   ADAPTLookAndFeel.generateTableEntry("Total", null, String.valueOf(state.getTotal()), null, writer);

                   String formText =
                       "<FORM method=\"GET\" action=\"" + request.getRequestURI() + "\">" +
                           "<INPUT type=\"HIDDEN\" name=\"name\" value=\"" + currentPurchaseOrderName + "\">" +
                           "<INPUT type=\"SUBMIT\" value=\"Check\">" +
                       "</FORM>";

                   ADAPTLookAndFeel.generateTableEntry(formText, null, writer);
                   ADAPTLookAndFeel.generateTableFooter(writer);
                }
            }
            else
            {
                doPurchaseOrderCheck(purchaseOrderName, writer);
            }
        }
        catch (ServiceException serviceException)
        {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, serviceException.toString());
        }
        
        Navigation.generateNavigationFooter(writer);
        ADAPTLookAndFeel.generatePageFooter(writer);
    }

    private void doPurchaseOrderCheck(String purchaseOrderName, PrintWriter writer)
    {
        try
        {
            String                     manufacturerStockServiceURLString = getInitParameter("ManufacturerStockServiceURL");
            ManufacturerServiceLocator manufacturerStockServiceLocator   = new ManufacturerServiceLocator();
            ManufacturerStockPortType  manufacturerStockServicePortType  = manufacturerStockServiceLocator.getStock(new URL(manufacturerStockServiceURLString));

            boolean done = manufacturerStockServicePortType.checkPurchaseOrder(purchaseOrderName);

            if (done)
                writer.println("<P><B>Now complete</B></P>");
            else
                writer.println("<P><B>Not completed</P>");
        }
        catch (Exception exception)
        {
            writer.println("<B>Failed</B></P>");
            System.err.println("Exception in \"doPurchaseOrderCheck\": " + exception);
        }
        catch (Error error)
        {
            writer.println("<B>Failed</B></P>");
            System.err.println("Error in \"doPurchaseOrderCheck\": " + error);
        }
    }
}