/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * InventoryItemServlet.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.InventoryItemBasicService_wsdl.InventoryItemBasicServicePortType;
import org.eu.adapt.services.demo_1_1.manufacturer.InventoryItemBasicService_wsdl.InventoryItemBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.InventoryItemBasicService_wsdl.types.InventoryItemState;

public class InventoryItemServlet 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                            inventoryItemName                  = request.getParameter("name");
            String                            inventoryItemBasicServiceURLString = getInitParameter("InventoryItemBasicServiceURL");
            InventoryItemBasicServiceLocator  inventoryItemBasicServiceLocator   = new InventoryItemBasicServiceLocator();
            InventoryItemBasicServicePortType inventoryItemBasicServicePortType  = inventoryItemBasicServiceLocator.getInventoryItem(new URL(inventoryItemBasicServiceURLString));
    
            if (inventoryItemName == null)
            {
                String[] inventoryItemNames = inventoryItemBasicServicePortType.listInventoryItems();

                ADAPTLookAndFeel.generateTableHeader("Inventory Items", null, writer);
                for (int inventoryItemNameIndex = 0; (inventoryItemNames != null) && (inventoryItemNameIndex < inventoryItemNames.length); inventoryItemNameIndex++)
                    ADAPTLookAndFeel.generateTableEntry(inventoryItemNames[inventoryItemNameIndex], "InventoryItem.html?name=" + URLEncoder.encode(inventoryItemNames[inventoryItemNameIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                InventoryItemState state = inventoryItemBasicServicePortType.getInventoryItemState(inventoryItemName);

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

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

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

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

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

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

                String storehouseName = state.getStorehouseName();
                writer.println("<P>Storehouse: ");
                if (storehouseName != null)
                    writer.println("<A href=\"Storehouse.html?name=" + URLEncoder.encode(storehouseName, "UTF-8") + "\">" + storehouseName + "</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);
    }
}