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

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

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.retailer.WarehouseBasicService_wsdl.WarehouseBasicServicePortType;
import org.eu.adapt.services.demo_1_1.retailer.WarehouseBasicService_wsdl.WarehouseBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.WarehouseBasicService_wsdl.types.WarehouseState;

public class WarehouseServlet 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                        warehouseName                  = request.getParameter("name");
            String                        warehouseBasicServiceURLString = getInitParameter("WarehouseBasicServiceURL");
            WarehouseBasicServiceLocator  warehouseBasicServiceLocator   = new WarehouseBasicServiceLocator();
            WarehouseBasicServicePortType warehouseBasicServicePortType  = warehouseBasicServiceLocator.getWarehouse(new URL(warehouseBasicServiceURLString));

            if (warehouseName == null)
            {
                String[] warehouseNames = warehouseBasicServicePortType.listWarehouses();
    
                ADAPTLookAndFeel.generateTableHeader("Warehouses", null, writer);
                for (int warehouseNameIndex = 0; (warehouseNames != null) && (warehouseNameIndex < warehouseNames.length); warehouseNameIndex++)
                    ADAPTLookAndFeel.generateTableEntry(warehouseNames[warehouseNameIndex], "Warehouse.html?name=" + URLEncoder.encode(warehouseNames[warehouseNameIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                WarehouseState state = warehouseBasicServicePortType.getWarehouseState(warehouseName);
    
                writer.println("<P><B>Warehouse: " + warehouseName + "</B></P>");

                String[] productNames = state.getProductNames();
                writer.println("<P>Products</P>");
                for (int productIndex = 0; (productNames != null) && (productIndex < productNames.length); productIndex++)
                {
                    writer.println("<P>" + productIndex + ": ");
                    writer.println("<A href=\"Product.html?name=" + URLEncoder.encode(productNames[productIndex], "UTF-8") + "\">" + productNames[productIndex] + "</A>");
                    writer.println("</P>");
                }
    
                String[] inventoryNumbers = state.getInventoryNumbers();
                writer.println("<P>Inventories</P>");
                for (int inventoryIndex = 0; (inventoryNumbers != null) && (inventoryIndex < inventoryNumbers.length); inventoryIndex++)
                {
                    writer.println("<P>" + inventoryIndex + ": ");
                    writer.println("<A href=\"Inventory.html?name=" + URLEncoder.encode(inventoryNumbers[inventoryIndex], "UTF-8") + "\">" + inventoryNumbers[inventoryIndex] + "</A>");
                    writer.println("</P>");
                }

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

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