/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * PurchaseOrderItemServlet.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.PurchaseOrderItemBasicService_wsdl.PurchaseOrderItemBasicServicePortType;
import org.eu.adapt.services.demo_1_1.retailer.PurchaseOrderItemBasicService_wsdl.PurchaseOrderItemBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.PurchaseOrderItemBasicService_wsdl.types.PurchaseOrderItemState;

public class PurchaseOrderItemServlet 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                                purchaseOrderItemName                  = request.getParameter("name");
            String                                purchaseOrderItemBasicServiceURLString = getInitParameter("PurchaseOrderItemBasicServiceURL");
            PurchaseOrderItemBasicServiceLocator  purchaseOrderItemBasicServiceLocator   = new PurchaseOrderItemBasicServiceLocator();
            PurchaseOrderItemBasicServicePortType purchaseOrderItemBasicServicePortType  = purchaseOrderItemBasicServiceLocator.getPurchaseOrderItem(new URL(purchaseOrderItemBasicServiceURLString));
    
            if (purchaseOrderItemName == null)
            {
                String[] purchaseOrderItemNames = purchaseOrderItemBasicServicePortType.listPurchaseOrderItems();
    
                ADAPTLookAndFeel.generateTableHeader("Purchase Order Items", null, writer);
                for (int purchaseOrderItemNameIndex = 0; (purchaseOrderItemNames != null) && (purchaseOrderItemNameIndex < purchaseOrderItemNames.length); purchaseOrderItemNameIndex++)
                    ADAPTLookAndFeel.generateTableEntry(purchaseOrderItemNames[purchaseOrderItemNameIndex], "Item.html?name=" + URLEncoder.encode(purchaseOrderItemNames[purchaseOrderItemNameIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                PurchaseOrderItemState state = purchaseOrderItemBasicServicePortType.getPurchaseOrderItemState(purchaseOrderItemName);
    
                writer.println("<P><B>Item: " + purchaseOrderItemName + "</B></P>");

                String productName = state.getProductName();
                writer.println("<P>Product Name: ");
                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>");

                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 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>");
            }
        }
        catch (ServiceException serviceException)
        {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, serviceException.toString());
        }

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