/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * StorehouseServlet.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.StorehouseBasicService_wsdl.StorehouseBasicServicePortType;
import org.eu.adapt.services.demo_1_1.manufacturer.StorehouseBasicService_wsdl.StorehouseBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.StorehouseBasicService_wsdl.types.StorehouseState;

public class StorehouseServlet 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                         storehouseName                  = request.getParameter("name");
            String                         storehouseBasicServiceURLString = getInitParameter("StorehouseBasicServiceURL");
            StorehouseBasicServiceLocator  storehouseBasicServiceLocator   = new StorehouseBasicServiceLocator();
            StorehouseBasicServicePortType storehouseBasicServicePortType  = storehouseBasicServiceLocator.getStorehouse(new URL(storehouseBasicServiceURLString));

            if (storehouseName == null)
            {
                String[] storehouseNames = storehouseBasicServicePortType.listStorehouses();
    
                ADAPTLookAndFeel.generateTableHeader("Storehouses", null, writer);
                for (int storehouseNameIndex = 0; (storehouseNames != null) && (storehouseNameIndex < storehouseNames.length); storehouseNameIndex++)
                    ADAPTLookAndFeel.generateTableEntry(storehouseNames[storehouseNameIndex], "Storehouse.html?name=" + URLEncoder.encode(storehouseNames[storehouseNameIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                StorehouseState state = storehouseBasicServicePortType.getStorehouseState(storehouseName);
    
                writer.println("<P><B>Storehouse: " + storehouseName + "</B></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[] inventoryItemIDs = state.getInventoryItemIDs();
                writer.println("<P>Inventory Items</P>");
                for (int inventoryItemIndex = 0; (inventoryItemIDs != null) && (inventoryItemIndex < inventoryItemIDs.length); inventoryItemIndex++)
                {
                    writer.println("<P>" + inventoryItemIndex + ": ");
                    writer.println("<A href=\"InventoryItem.html?name=" + URLEncoder.encode(inventoryItemIDs[inventoryItemIndex], "UTF-8") + "\">" + inventoryItemIDs[inventoryItemIndex] + "</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] + "\">" + itemIDs[itemIndex], "UTF-8") + "</A>");
                    writer.println("</P>");
                }
            }
        }
        catch (ServiceException serviceException)
        {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, serviceException.toString());
        }

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