/*
 * Copyright (c) 2004, 2005, ADAPT project partners (IST-2001-37126).
 */

package org.eu.adapt.demonstrator.web.demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eu.adapt.demonstrator.web.ADAPTLookAndFeel;
import org.eu.adapt.demonstrator.web.Navigation;
import org.eu.adapt.services.demo_1_1.manufacturer.ManufacturerSetupBasicService_wsdl.ManufacturerSetupBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.manufacturer.ManufacturerSetupBasicService_wsdl.ManufacturerSetupBasicServicePortType;
import org.eu.adapt.services.demo_1_1.retailer.RetailerSetupBasicService_wsdl.RetailerSetupBasicServiceLocator;
import org.eu.adapt.services.demo_1_1.retailer.RetailerSetupBasicService_wsdl.RetailerSetupBasicServicePortType;

public class DemoDatabasesSetupServlet extends HttpServlet
{
    public void init(ServletConfig config)
        throws ServletException
    {
        super.init(config);
    }
    
    public void destroy()
    {
        super.destroy();
    }
    
    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                                retailerSetupBasicServiceURLString      = getInitParameter("RetailerSetupBasicServiceURL");
            String                                manufacturerASetupBasicServiceURLString = getInitParameter("ManufacturerASetupBasicServiceURL");
            String                                manufacturerBSetupBasicServiceURLString = getInitParameter("ManufacturerBSetupBasicServiceURL");
            String                                manufacturerCSetupBasicServiceURLString = getInitParameter("ManufacturerCSetupBasicServiceURL");
            RetailerSetupBasicServiceLocator      retailerSetupBasicServiceLocator        = new RetailerSetupBasicServiceLocator();
            RetailerSetupBasicServicePortType     retailerSetupBasicServicePortType       = retailerSetupBasicServiceLocator.getRetailerSetup(new URL(retailerSetupBasicServiceURLString));
            ManufacturerSetupBasicServiceLocator  manufacturerSetupBasicServiceLocator    = new ManufacturerSetupBasicServiceLocator();
            ManufacturerSetupBasicServicePortType manufacturerASetupBasicServicePortType  = manufacturerSetupBasicServiceLocator.getManufacturerSetup(new URL(manufacturerASetupBasicServiceURLString));
            ManufacturerSetupBasicServicePortType manufacturerBSetupBasicServicePortType  = manufacturerSetupBasicServiceLocator.getManufacturerSetup(new URL(manufacturerBSetupBasicServiceURLString));
            ManufacturerSetupBasicServicePortType manufacturerCSetupBasicServicePortType  = manufacturerSetupBasicServiceLocator.getManufacturerSetup(new URL(manufacturerCSetupBasicServiceURLString));

            ADAPTLookAndFeel.generateTableHeader("Database Setups", null, writer);

            if (retailerSetupBasicServicePortType.setup())
                ADAPTLookAndFeel.generateTableEntry("Retailer", null, "Successful", null, writer);
            else
                ADAPTLookAndFeel.generateTableEntry("Retailer", null, "Unsuccessful", null, writer);

            if (manufacturerASetupBasicServicePortType.setup())
                ADAPTLookAndFeel.generateTableEntry("Manufacturer A", null, "Successful", null, writer);
            else
                ADAPTLookAndFeel.generateTableEntry("Manufacturer A", null, "Unsuccessful", null, writer);

            if (manufacturerBSetupBasicServicePortType.setup())
                ADAPTLookAndFeel.generateTableEntry("Manufacturer B", null, "Successful", null, writer);
            else
                ADAPTLookAndFeel.generateTableEntry("Manufacturer B", null, "Unsuccessful", null, writer);

            if (manufacturerCSetupBasicServicePortType.setup())
                ADAPTLookAndFeel.generateTableEntry("Manufacturer C", null, "Successful", null, writer);
            else
                ADAPTLookAndFeel.generateTableEntry("Manufacturer C", null, "Unsuccessful", null, writer);

            ADAPTLookAndFeel.generateTableFooter(writer);
        }
        catch (Exception exception)
        {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, exception.toString());
        }
        catch (Error error)
        {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error.toString());
        }
    
        Navigation.generateNavigationFooter(writer);
        ADAPTLookAndFeel.generatePageFooter(writer);
    }
}
