/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * ManufacturerServlet.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.ManufacturerBasicService_wsdl.ManufacturerBasicServicePortType;
import org.eu.adapt.services.demo_1_1.retailer.ManufacturerBasicService_wsdl.ManufacturerBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.ManufacturerBasicService_wsdl.types.ManufacturerState;

public class ManufacturerServlet 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                           manufacturerName                  = request.getParameter("name");
            String                           manufacturerBasicServiceURLString = getInitParameter("ManufacturerBasicServiceURL");
            ManufacturerBasicServiceLocator  manufacturerBasicServiceLocator   = new ManufacturerBasicServiceLocator();
            ManufacturerBasicServicePortType manufacturerBasicServicePortType  = manufacturerBasicServiceLocator.getManufacturer(new URL(manufacturerBasicServiceURLString));

            if (manufacturerName == null)
            {
                String[] manufacturerNames = manufacturerBasicServicePortType.listManufacturers();

                ADAPTLookAndFeel.generateTableHeader("Manufacturers", null, writer);
                for (int manufacturerNameIndex = 0; (manufacturerNames != null) && (manufacturerNameIndex < manufacturerNames.length); manufacturerNameIndex++)
                    ADAPTLookAndFeel.generateTableEntry(manufacturerNames[manufacturerNameIndex], "Manufacturer.html?name=" + URLEncoder.encode(manufacturerNames[manufacturerNameIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                ManufacturerState state = manufacturerBasicServicePortType.getManufacturerState(manufacturerName);
    
                writer.println("<P><B>Manufacturer: " + manufacturerName + "</B></P>");

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

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