/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * CatalogItemServlet.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.CatalogItemBasicService_wsdl.CatalogItemBasicServicePortType;
import org.eu.adapt.services.demo_1_1.retailer.CatalogItemBasicService_wsdl.CatalogItemBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.CatalogItemBasicService_wsdl.types.CatalogItemState;

public class CatalogItemServlet 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                          catalogItemName                  = request.getParameter("name");
            String                          catalogItemBasicServiceURLString = getInitParameter("CatalogItemBasicServiceURL");
            CatalogItemBasicServiceLocator  catalogItemBasicServiceLocator   = new CatalogItemBasicServiceLocator();
            CatalogItemBasicServicePortType catalogItemBasicServicePortType  = catalogItemBasicServiceLocator.getCatalogItem(new URL(catalogItemBasicServiceURLString));
    
            if (catalogItemName == null)
            {
                String[] catalogItemNames = catalogItemBasicServicePortType.listCatalogItems();

                ADAPTLookAndFeel.generateTableHeader("Catalog Items", null, writer);
                for (int catalogItemReferenceIndex = 0; (catalogItemNames != null) && (catalogItemReferenceIndex < catalogItemNames.length); catalogItemReferenceIndex++)
                    ADAPTLookAndFeel.generateTableEntry(catalogItemNames[catalogItemReferenceIndex], "CatalogItem.html?name=" + URLEncoder.encode(catalogItemNames[catalogItemReferenceIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                CatalogItemState state = catalogItemBasicServicePortType.getCatalogItemState(catalogItemName);

                writer.println("<P>Catalog Item: " + catalogItemName + "</P>");

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

                writer.println("<P>Description: " + state.getDescription() + "</P>");

                writer.println("<P>Brand: " + state.getBrand() + "</P>");

                writer.println("<P>Price: " + state.getPrice() + "</P>");

                String catalogName = state.getCatalogName();
                writer.println("<P>Catalog Name: ");
                if (catalogName != null)
                    writer.println("<A href=\"Catalog.html?name=" + URLEncoder.encode(catalogName, "UTF-8") + "\">" + catalogName + "</A>");
                else
                    writer.println("N/A");
                writer.println("</P>");

                String productName = state.getProductName();
                writer.println("<P>Product: ");
                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>");
            }
        }
        catch (ServiceException serviceException)
        {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, serviceException.toString());
        }

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