/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * CustomerServlet.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.CustomerBasicService_wsdl.CustomerBasicServicePortType;
import org.eu.adapt.services.demo_1_1.manufacturer.CustomerBasicService_wsdl.CustomerBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.CustomerBasicService_wsdl.types.CustomerState;

public class CustomerServlet 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                       customerName                  = request.getParameter("name");
            String                       customerBasicServiceURLString = getInitParameter("CustomerBasicServiceURL");
            CustomerBasicServiceLocator  customerBasicServiceLocator   = new CustomerBasicServiceLocator();
            CustomerBasicServicePortType customerBasicServicePortType  = customerBasicServiceLocator.getCustomer(new URL(customerBasicServiceURLString));
    
            if (customerName == null)
            {
                String[] customerReferences = customerBasicServicePortType.listCustomers();

                ADAPTLookAndFeel.generateTableHeader("Customers", null, writer);
                for (int customerReferenceIndex = 0; (customerReferences != null) && (customerReferenceIndex < customerReferences.length); customerReferenceIndex++)
                    ADAPTLookAndFeel.generateTableEntry(customerReferences[customerReferenceIndex], "Customer.html?name=" + URLEncoder.encode(customerReferences[customerReferenceIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                CustomerState state = customerBasicServicePortType.getCustomerState(customerName);

                writer.println("<P>Customer: " + customerName + "</P>");

                writer.println("<P>Name: " + state.getName() + "</P>");

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

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