/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * InventoryServlet.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.InventoryBasicService_wsdl.InventoryBasicServicePortType;
import org.eu.adapt.services.demo_1_1.retailer.InventoryBasicService_wsdl.InventoryBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.InventoryBasicService_wsdl.types.InventoryState;

public class InventoryServlet 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                        inventoryName                  = request.getParameter("name");
            String                        inventoryBasicServiceURLString = getInitParameter("InventoryBasicServiceURL");
            InventoryBasicServiceLocator  inventoryBasicServiceLocator   = new InventoryBasicServiceLocator();
            InventoryBasicServicePortType inventoryBasicServicePortType  = inventoryBasicServiceLocator.getInventory(new URL(inventoryBasicServiceURLString));
    
            if (inventoryName == null)
            {
                String[] inventoryNames = inventoryBasicServicePortType.listInventories();

                ADAPTLookAndFeel.generateTableHeader("Inventorys", null, writer);
                for (int inventoryNameIndex = 0; (inventoryNames != null) && (inventoryNameIndex < inventoryNames.length); inventoryNameIndex++)
                    ADAPTLookAndFeel.generateTableEntry(inventoryNames[inventoryNameIndex], "Inventory.html?name=" + URLEncoder.encode(inventoryNames[inventoryNameIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                InventoryState state = inventoryBasicServicePortType.getInventoryState(inventoryName);

                writer.println("<P><B>Inventory: " + inventoryName + "</B></P>");

                writer.println("<P>Amount:     " + state.getAmount() + "</P>");

                writer.println("<P>Min Amount: " + state.getMinAmount() + "</P>");

                writer.println("<P>Max Amount: " + state.getMaxAmount() + "</P>");

                String warehouseName = state.getWarehouseName();
                writer.println("<P>Warehouse: ");
                if (warehouseName != null)
                    writer.println("<A href=\"Warehouse.html?name=" + URLEncoder.encode(warehouseName, "UTF-8") + "\">" + warehouseName + "</A>");
                else
                    writer.println("N/A");
                writer.println("</P>");

                String productName = state.getProductName();
                writer.println("<P>Product: ");
                if (productName != null)
                    writer.println("<A href=\"Product.html?name=" + URLEncoder.encode(productName, "UTF-8") + "\">" + productName + "</A>");
                else
                    writer.println("N/A");
                writer.println("</P>");
            }
        }
        catch (ServiceException serviceException)
        {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, serviceException.toString());
        }

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