/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * ProductServlet.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.ProductBasicService_wsdl.ProductBasicServicePortType;
import org.eu.adapt.services.demo_1_1.retailer.ProductBasicService_wsdl.ProductBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.ProductBasicService_wsdl.types.ProductState;

public class ProductServlet 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                      productName                  = request.getParameter("name");
            String                      productBasicServiceURLString = getInitParameter("ProductBasicServiceURL");
            ProductBasicServiceLocator  productBasicServiceLocator   = new ProductBasicServiceLocator();
            ProductBasicServicePortType productBasicServicePortType  = productBasicServiceLocator.getProduct(new URL(productBasicServiceURLString));
    
            if (productName == null)
            {
                String[] productNames = productBasicServicePortType.listProducts();

                ADAPTLookAndFeel.generateTableHeader("Products", null, writer);
                for (int productNameIndex = 0; (productNames != null) && (productNameIndex < productNames.length); productNameIndex++)
                    ADAPTLookAndFeel.generateTableEntry(productNames[productNameIndex], "Product.html?name=" + URLEncoder.encode(productNames[productNameIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                ProductState state = productBasicServicePortType.getProductState(productName);
    
                writer.println("<P><B>Product: " + productName + "</B></P>");

                String[] catalogItemNames = state.getCatalogItemNames();
                writer.println("<P>Catalog Items</P>");
                for (int catalogItemIndex = 0; (catalogItemNames != null) && (catalogItemIndex < catalogItemNames.length); catalogItemIndex++)
                {
                    writer.println("<P>" + catalogItemIndex + ": ");
                    writer.println("<A href=\"CatalogItem.html?name=" + URLEncoder.encode(catalogItemNames[catalogItemIndex], "UTF-8") + "\">" + catalogItemNames[catalogItemIndex] + "</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], "UTF-8") + "\">" + purchaseOrderItemNumbers[purchaseOrderItemIndex] + "</A>");
                    writer.println("</P>");
                }

                String[] manufacturerNames = state.getManufacturerNames();
                writer.println("<P>Manufacturers</P>");
                for (int manufacturerIndex = 0; (manufacturerNames != null) && (manufacturerIndex < manufacturerNames.length); manufacturerIndex++)
                {
                    writer.println("<P>" + manufacturerIndex + ": ");
                    writer.println("<A href=\"Manufacturer.html?name=" + URLEncoder.encode(manufacturerNames[manufacturerIndex], "UTF-8") + "\">" + manufacturerNames[manufacturerIndex] + "</A>");
                    writer.println("</P>");
                }

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

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