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

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

import java.io.*;
import java.net.URL;
import javax.servlet.*;
import javax.servlet.http.*;
import org.eu.adapt.demonstrator.web.ADAPTLookAndFeel;
import org.eu.adapt.demonstrator.web.Navigation;
import org.eu.adapt.services.demo_1_1.manufacturer.ProductionLineBasicService_wsdl.ProductionLineBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.ProductionLineBasicService_wsdl.ProductionLineBasicServicePortType;
import org.eu.adapt.services.demo_1_1.manufacturer.ProductionLineBasicService_wsdl.types.ProductionLineState;
import org.eu.adapt.services.demo_1_1.manufacturer.FinishedGoodsBasicService_wsdl.FinishedGoodsBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.FinishedGoodsBasicService_wsdl.FinishedGoodsBasicServicePortType;
import org.eu.adapt.services.demo_1_1.manufacturer.FinishedGoodsBasicService_wsdl.types.FinishedGoodsState;

public class RestockServlet 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);

        String productionLineName = request.getParameter("productionLineName");
        String finishedGoodsID    = request.getParameter("finishedGoodsID");
        int    amount             = 0;
        try
	{
            if (request.getParameter("amount") != null)
                amount = Integer.parseInt(request.getParameter("amount"));
	}
        catch (NumberFormatException numberFormatException)
	{
            amount = 0;
            System.err.println("Exception in RestockServlet.doGet: obtaining amount: " + numberFormatException);
        }
        
        if ((productionLineName != null) && (finishedGoodsID != null) && (amount > 0))
            produceStock(productionLineName, finishedGoodsID, amount, writer);
        else
            displayProductionLines(request, writer);

        Navigation.generateNavigationFooter(writer);
        ADAPTLookAndFeel.generatePageFooter(writer);
    }

    private void produceStock(String productionLineName, String finishedGoodsID, int amount, PrintWriter writer)
    {
        try
        {
            String                             productionLineBasicServiceURLString = getInitParameter("ProductionLineBasicServiceURL");
            String                             finishedGoodsBasicServiceURLString  = getInitParameter("FinishedGoodsBasicServiceURL");
            ProductionLineBasicServiceLocator  productionLineBasicServiceLocator   = new ProductionLineBasicServiceLocator();
            FinishedGoodsBasicServiceLocator   finishedGoodsBasicServiceLocator    = new FinishedGoodsBasicServiceLocator();
            ProductionLineBasicServicePortType productionLineBasicServicePortType  = productionLineBasicServiceLocator.getProductionLine(new URL(productionLineBasicServiceURLString));
            FinishedGoodsBasicServicePortType  finishedGoodsBasicServicePortType   = finishedGoodsBasicServiceLocator.getFinishedGoods(new URL(finishedGoodsBasicServiceURLString));

            String  finishedGoodsDescription = finishedGoodsBasicServicePortType.getFinishedGoodsState(finishedGoodsID).getDescription();
            boolean done                     = productionLineBasicServicePortType.restock(productionLineName, finishedGoodsID, amount);

            ADAPTLookAndFeel.generateTableHeader("Product Finished Goods", null, writer);

            ADAPTLookAndFeel.generateTableEntry("Production Line", null, productionLineName, null, writer);
            ADAPTLookAndFeel.generateTableEntry("Finished Goods", null, finishedGoodsDescription, null, writer);
            ADAPTLookAndFeel.generateTableEntry("Amount", null, String.valueOf(amount), null, writer);
            if (done)
                ADAPTLookAndFeel.generateTableEntry("Status", null,"Successful", null, writer);
            else
                ADAPTLookAndFeel.generateTableEntry("Status", null,"Unsuccessful", null, writer);
            ADAPTLookAndFeel.generateTableFooter(writer);
        }
        catch (Exception exception)
        {
            System.err.println("Exception in \"produceStock\": " + exception);
            exception.printStackTrace(System.err);
        }
        catch (Error error)
        {
            System.err.println("Error in \"produceStock\": " + error);
            error.printStackTrace(System.err);
        }
    }

    private void displayProductionLines(HttpServletRequest request, PrintWriter writer)
    {
        try
        {
            String                             productionLineBasicServiceURLString = getInitParameter("ProductionLineBasicServiceURL");
            String                             finishedGoodsBasicServiceURLString  = getInitParameter("FinishedGoodsBasicServiceURL");
            ProductionLineBasicServiceLocator  productionLineBasicServiceLocator   = new ProductionLineBasicServiceLocator();
            FinishedGoodsBasicServiceLocator   finishedGoodsBasicServiceLocator    = new FinishedGoodsBasicServiceLocator();
            ProductionLineBasicServicePortType productionLineBasicServicePortType  = productionLineBasicServiceLocator.getProductionLine(new URL(productionLineBasicServiceURLString));
            FinishedGoodsBasicServicePortType  finishedGoodsBasicServicePortType   = finishedGoodsBasicServiceLocator.getFinishedGoods(new URL(finishedGoodsBasicServiceURLString));

            String[] productionLineNames = productionLineBasicServicePortType.listProductionLines();

            ADAPTLookAndFeel.generateTableHeader("Product Finished Goods Events", null, writer);
            ADAPTLookAndFeel.generateTableFooter(writer);

            for (int productionLineNameIndex = 0; (productionLineNames != null) && (productionLineNameIndex < productionLineNames.length); productionLineNameIndex++)
	    {
                String              productionLineName = productionLineNames[productionLineNameIndex];
                ProductionLineState state              = productionLineBasicServicePortType.getProductionLineState(productionLineName);

                ADAPTLookAndFeel.generateTableHeader("Production Lines: " + productionLineName, null, writer);

                String[] finishedGoodsIDs = state.getFinishedGoodsNames();
                for (int finishedGoodsIndex = 0; (finishedGoodsIDs != null) && (finishedGoodsIndex < finishedGoodsIDs.length); finishedGoodsIndex++)
		{
                    String             finishedGoodsID    = finishedGoodsIDs[finishedGoodsIndex];
                    FinishedGoodsState finishedGoodsState = finishedGoodsBasicServicePortType.getFinishedGoodsState(finishedGoodsID);
                    
                    String formText =
                        "<FORM method=\"GET\" action=\"" + request.getRequestURI() + "\">" +
                            "<INPUT type=\"HIDDEN\" name=\"productionLineName\" value=\"" + productionLineName + "\">" +
                            "<INPUT type=\"HIDDEN\" name=\"finishedGoodsID\" value=\"" + finishedGoodsID + "\">" +
                            "<INPUT type=\"text\" name=\"amount\" value=\"0\">" +
                            "<INPUT type=\"SUBMIT\" value=\"Submit\">" +
                        "</FORM>";

                    ADAPTLookAndFeel.generateTableEntry(finishedGoodsState.getDescription(), null, formText, null, writer);
		}
                ADAPTLookAndFeel.generateTableFooter(writer);
	    }
        }
        catch (Exception exception)
        {
            System.err.println("Exception in \"displayProductionLines\": " + exception);
            exception.printStackTrace(System.err);
        }
        catch (Error error)
        {
            System.err.println("Error in \"displayProductionLines\": " + error);
            error.printStackTrace(System.err);
        }
    }
}
