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

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

import java.io.*;
import java.net.*;
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.manufacturer.ProductionLineBasicService_wsdl.ProductionLineBasicServicePortType;
import org.eu.adapt.services.demo_1_1.manufacturer.ProductionLineBasicService_wsdl.ProductionLineBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.ProductionLineBasicService_wsdl.types.ProductionLineState;

public class ProductionLineServlet 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                             productionLineName                  = request.getParameter("name");
            String                             productionLineBasicServiceURLString = getInitParameter("ProductionLineBasicServiceURL");
            ProductionLineBasicServiceLocator  productionLineBasicServiceLocator   = new ProductionLineBasicServiceLocator();
            ProductionLineBasicServicePortType productionLineBasicServicePortType  = productionLineBasicServiceLocator.getProductionLine(new URL(productionLineBasicServiceURLString));

            if (productionLineName == null)
            {
                String[] productionLineNames = productionLineBasicServicePortType.listProductionLines();

                ADAPTLookAndFeel.generateTableHeader("Production Lines", null, writer);
                for (int productionLineNameIndex = 0; (productionLineNames != null) && (productionLineNameIndex < productionLineNames.length); productionLineNameIndex++)
                    ADAPTLookAndFeel.generateTableEntry(productionLineNames[productionLineNameIndex], "ProductionLine.html?name=" + URLEncoder.encode(productionLineNames[productionLineNameIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                ProductionLineState state = productionLineBasicServicePortType.getProductionLineState(productionLineName);
    
                writer.println("<P><B>Production Line: " + productionLineName + "</B></P>");

                String storehouseName = state.getStorehouseName();
                writer.println("<P>Storehouse: ");
                if (storehouseName != null)
                    writer.println("<A href=\"Storehouse.html?name=" + URLEncoder.encode(storehouseName, "UTF-8") + "\">" + storehouseName + "</A>");
                else
                    writer.println("N/A");
                writer.println("</P>");
    
                String[] finishedGoodsNames = state.getFinishedGoodsNames();
                writer.println("<P>Finished Goods</P>");
                for (int finishedGoodsIndex = 0; (finishedGoodsNames != null) && (finishedGoodsIndex < finishedGoodsNames.length); finishedGoodsIndex++)
                {
                    writer.println("<P>" + finishedGoodsIndex + ": ");
                    writer.println("<A href=\"FinishedGoods.html?name=" + URLEncoder.encode(finishedGoodsNames[finishedGoodsIndex], "UTF-8") + "\">" + finishedGoodsNames[finishedGoodsIndex] + "</A>");
                    writer.println("</P>");
                }
            }
        }
        catch (ServiceException serviceException)
        {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, serviceException.toString());
        }

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