/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * CustomerReferenceServlet.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.CustomerReferenceBasicService_wsdl.CustomerReferenceBasicServicePortType;
import org.eu.adapt.services.demo_1_1.retailer.CustomerReferenceBasicService_wsdl.CustomerReferenceBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.CustomerReferenceBasicService_wsdl.types.CustomerReferenceState;

public class CustomerReferenceServlet 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                                customerReferenceName                  = request.getParameter("name");
            String                                customerReferenceBasicServiceURLString = getInitParameter("CustomerReferenceBasicServiceURL");
            CustomerReferenceBasicServiceLocator  customerReferenceBasicServiceLocator   = new CustomerReferenceBasicServiceLocator();
            CustomerReferenceBasicServicePortType customerReferenceBasicServicePortType  = customerReferenceBasicServiceLocator.getCustomer(new URL(customerReferenceBasicServiceURLString));
    
            if (customerReferenceName == null)
            {
                String[] customerReferences = customerReferenceBasicServicePortType.listCustomerReferences();

                ADAPTLookAndFeel.generateTableHeader("Customer References", null, writer);
                for (int customerReferenceIndex = 0; (customerReferences != null) && (customerReferenceIndex < customerReferences.length); customerReferenceIndex++)
                    ADAPTLookAndFeel.generateTableEntry(customerReferences[customerReferenceIndex], "CustomerReference.html?name=" + URLEncoder.encode(customerReferences[customerReferenceIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                CustomerReferenceState state = customerReferenceBasicServicePortType.getCustomerReferenceState(customerReferenceName);

                writer.println("<P>CustomerReference: " + customerReferenceName + "</P>");

                writer.println("<P>Name: " + state.getName() + "</P>");
                writer.println("<P>Street 1: " + state.getStreet1() + "</P>");
                writer.println("<P>Street 2: " + state.getStreet2() + "</P>");
                writer.println("<P>City: " + state.getCity() + "</P>");
                writer.println("<P>State: " + state.getState() + "</P>");
                writer.println("<P>Zip: " + state.getZip() + "</P>");
                writer.println("<P>Country: " + state.getCountry() + "</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);
    }
}