/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * FinishedGoodsServlet.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.FinishedGoodsBasicService_wsdl.FinishedGoodsBasicServicePortType;
import org.eu.adapt.services.demo_1_1.manufacturer.FinishedGoodsBasicService_wsdl.FinishedGoodsBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.FinishedGoodsBasicService_wsdl.types.FinishedGoodsState;

public class FinishedGoodsServlet 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                            finishedGoodsName                  = request.getParameter("name");
            String                            finishedGoodsBasicServiceURLString = getInitParameter("FinishedGoodsBasicServiceURL");
            FinishedGoodsBasicServiceLocator  finishedGoodsBasicServiceLocator   = new FinishedGoodsBasicServiceLocator();
            FinishedGoodsBasicServicePortType finishedGoodsBasicServicePortType  = finishedGoodsBasicServiceLocator.getFinishedGoods(new URL(finishedGoodsBasicServiceURLString));
    
            if (finishedGoodsName == null)
            {
                String[] finishedGoodsNames = finishedGoodsBasicServicePortType.listFinishedGoods();

                ADAPTLookAndFeel.generateTableHeader("Finished Goods", null, writer);
                for (int finishedGoodsNameIndex = 0; (finishedGoodsNames != null) && (finishedGoodsNameIndex < finishedGoodsNames.length); finishedGoodsNameIndex++)
                    ADAPTLookAndFeel.generateTableEntry(finishedGoodsNames[finishedGoodsNameIndex], "FinishedGoods.html?name=" + URLEncoder.encode(finishedGoodsNames[finishedGoodsNameIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                FinishedGoodsState state = finishedGoodsBasicServicePortType.getFinishedGoodsState(finishedGoodsName);
    
                writer.println("<P><B>Finished Goods: " + finishedGoodsName + "</B></P>");

                writer.println("<P>Description: " + state.getDescription() + "</B></P>");

                String[] inventoryItemIDs = state.getInventoryItemIDs();
                writer.println("<P>Inventory Items</P>");
                for (int inventoryItemIDIndex = 0; (inventoryItemIDs != null) && (inventoryItemIDIndex < inventoryItemIDs.length); inventoryItemIDIndex++)
                {
                    writer.println("<P>" + inventoryItemIDIndex + ": ");
                    writer.println("<A href=\"InventoryItem.html?name=" + URLEncoder.encode(inventoryItemIDs[inventoryItemIDIndex], "UTF-8") + "\">" + inventoryItemIDs[inventoryItemIDIndex] + "</A>");
                    writer.println("</P>");
                }

                String[] productionLineNames = state.getProductionLineNames();
                writer.println("<P>Production Lines</P>");
                for (int productionLineIndex = 0; (productionLineNames != null) && (productionLineIndex < productionLineNames.length); productionLineIndex++)
                {
                    writer.println("<P>" + productionLineIndex + ": ");
                    writer.println("<A href=\"ProductionLine.html?name=" + URLEncoder.encode(productionLineNames[productionLineIndex], "UTF-8") + "\">" + productionLineNames[productionLineIndex] + "</A>");
                    writer.println("</P>");
                }

                String[] itemIDs = state.getItemIDs();
                writer.println("<P>Items</P>");
                for (int itemIndex = 0; (itemIDs != null) && (itemIndex < itemIDs.length); itemIndex++)
                {
                    writer.println("<P>" + itemIndex + ": ");
                    writer.println("<A href=\"Item.html?name=" + URLEncoder.encode(itemIDs[itemIndex], "UTF-8") + "\">" + itemIDs[itemIndex] + "</A>");
                    writer.println("</P>");
                }
            }
        }
        catch (ServiceException serviceException)
        {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, serviceException.toString());
        }

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