/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * PurchaseOrderServlet.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.PurchaseOrderBasicService_wsdl.PurchaseOrderBasicServicePortType;
import org.eu.adapt.services.demo_1_1.retailer.PurchaseOrderBasicService_wsdl.PurchaseOrderBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.PurchaseOrderBasicService_wsdl.types.PurchaseOrderState;

public class PurchaseOrderServlet 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                            purchaseOrderName                  = request.getParameter("name");
            String                            purchaseOrderBasicServiceURLString = getInitParameter("PurchaseOrderBasicServiceURL");
            PurchaseOrderBasicServiceLocator  purchaseOrderBasicServiceLocator   = new PurchaseOrderBasicServiceLocator();
            PurchaseOrderBasicServicePortType purchaseOrderBasicServicePortType  = purchaseOrderBasicServiceLocator.getPurchaseOrder(new URL(purchaseOrderBasicServiceURLString));
    
            if (purchaseOrderName == null)
            {
                String[] purchaseOrderNames = purchaseOrderBasicServicePortType.listPurchaseOrders();
    
                ADAPTLookAndFeel.generateTableHeader("Purchase Orders", null, writer);
                for (int purchaseOrderNameIndex = 0; (purchaseOrderNames != null) && (purchaseOrderNameIndex < purchaseOrderNames.length); purchaseOrderNameIndex++)
                    ADAPTLookAndFeel.generateTableEntry(purchaseOrderNames[purchaseOrderNameIndex], "PurchaseOrder.html?name=" + URLEncoder.encode(purchaseOrderNames[purchaseOrderNameIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                PurchaseOrderState state = purchaseOrderBasicServicePortType.getPurchaseOrderState(purchaseOrderName);

                writer.println("<P><B>Purchase Order Number: " + purchaseOrderName + "</B></P>");

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

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

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