/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 * 
 * CatalogServlet.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.CatalogBasicService_wsdl.CatalogBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.CatalogBasicService_wsdl.CatalogBasicServicePortType;
import org.eu.adapt.services.demo_1_1.retailer.CatalogBasicService_wsdl.types.CatalogState;

public class CatalogServlet 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                      catalogName                  = request.getParameter("name");
            String                      catalogBasicServiceURLString = getInitParameter("CatalogBasicServiceURL");
            CatalogBasicServiceLocator  catalogBasicServiceLocator   = new CatalogBasicServiceLocator();
            CatalogBasicServicePortType catalogBasicServicePortType  = catalogBasicServiceLocator.getCatalog(new URL(catalogBasicServiceURLString));
    
            if (catalogName == null)
            {
                String[] catalogNames = catalogBasicServicePortType.listCatalogs();
    
                ADAPTLookAndFeel.generateTableHeader("Catalogs", null, writer);
                for (int catalogReferenceIndex = 0; (catalogNames != null) && (catalogReferenceIndex < catalogNames.length); catalogReferenceIndex++)
                    ADAPTLookAndFeel.generateTableEntry(catalogNames[catalogReferenceIndex], "Catalog.html?name=" + URLEncoder.encode(catalogNames[catalogReferenceIndex], "UTF-8"), writer);
                ADAPTLookAndFeel.generateTableFooter(writer);
            }
            else
            {
                CatalogState state = catalogBasicServicePortType.getCatalogState(catalogName);
    
                writer.println("<P>Catalog: " + catalogName + "</P>");
    
                writer.println("<P>Name: " + state.getName() + "</P>");
    
                String[] catalogItemNames = state.getCatalogItemNames();
                writer.println("<P>Catalog Item</P>");
                for (int catalogItemIndex = 0; (catalogItemNames != null) && (catalogItemIndex < catalogItemNames.length); catalogItemIndex++)
                {
                    writer.println("<P>" + catalogItemIndex + ": ");
                    writer.println("<A href=\"CatalogItem.html?name=" + URLEncoder.encode(catalogItemNames[catalogItemIndex], "UTF-8") + "\">" + catalogItemNames[catalogItemIndex] + "</A>");
                    writer.println("</P>");
                }
            }
        }
        catch (ServiceException serviceException)
        {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, serviceException.toString());
        }
    
        Navigation.generateNavigationFooter(writer);
        ADAPTLookAndFeel.generatePageFooter(writer);
    }
}