/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * ItemServlet.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.ItemBasicService_wsdl.ItemBasicServicePortType;
import org.eu.adapt.services.demo_1_1.manufacturer.ItemBasicService_wsdl.ItemBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.ItemBasicService_wsdl.types.ItemState;

public class ItemServlet 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                   itemName                  = request.getParameter("name");
            String                   itemBasicServiceURLString = getInitParameter("ItemBasicServiceURL");
            ItemBasicServiceLocator  itemBasicServiceLocator   = new ItemBasicServiceLocator();
            ItemBasicServicePortType itemBasicServicePortType  = itemBasicServiceLocator.getItem(new URL(itemBasicServiceURLString));
    
            if (itemName == null)
            {
                String[] itemNames = itemBasicServicePortType.listItems();
    
                ADAPTLookAndFeel.generateTableHeader("Items", null, writer);
                for (int itemNameIndex = 0; (itemNames != null) && (itemNameIndex < itemNames.length); itemNameIndex++)
                    ADAPTLookAndFeel.generateTableEntry(itemNames[itemNameIndex], "Item.html?name=" + URLEncoder.encode(itemNames[itemNameIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                ItemState state = itemBasicServicePortType.getItemState(itemName);
    
                writer.println("<P><B>Item: " + itemName + "</B></P>");

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

                writer.println("<P>Quantity: " + state.getQuantity() + "</P>");

                writer.println("<P>Price: " + state.getPrice() + "</P>");

                String purchaseOrderNumber = state.getPurchaseOrderNumber();
                writer.println("<P>Purchase Order Number: ");
                if (purchaseOrderNumber != null)
                    writer.println("<A href=\"PurchaseOrder.html?name=" + URLEncoder.encode(purchaseOrderNumber, "UTF-8") + "\">" + purchaseOrderNumber + "</A>");
                else
                    writer.println("N/A");
                writer.println("</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);
    }
}