/*
 * 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 localhost.jboss_net.services.MonitorConsole.MonitorConsole;
import localhost.jboss_net.services.MonitorConsole.MonitorConsoleServiceLocator;

import org.eu.adapt.demonstrator.web.ADAPTLookAndFeel;
import org.eu.adapt.demonstrator.web.Navigation;

public class DemoSensorsSetupServlet 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                       retailerMonitorConsoleURLString      = getInitParameter("RetailerMonitorConsoleURL");
            String                       manufacturerAMonitorConsoleURLString = getInitParameter("ManufacturerAMonitorConsoleURL");
            String                       manufacturerBMonitorConsoleURLString = getInitParameter("ManufacturerBMonitorConsoleURL");
            String                       manufacturerCMonitorConsoleURLString = getInitParameter("ManufacturerCMonitorConsoleURL");
            MonitorConsoleServiceLocator monitorConsoleServiceLocator         = new MonitorConsoleServiceLocator();
            MonitorConsole               retailerMonitorConsole               = monitorConsoleServiceLocator.getMonitorConsole(new URL(retailerMonitorConsoleURLString));
            MonitorConsole               manufacturerAMonitorConsole          = monitorConsoleServiceLocator.getMonitorConsole(new URL(manufacturerAMonitorConsoleURLString));
            MonitorConsole               manufacturerBMonitorConsole          = monitorConsoleServiceLocator.getMonitorConsole(new URL(manufacturerBMonitorConsoleURLString));
            MonitorConsole               manufacturerCMonitorConsole          = monitorConsoleServiceLocator.getMonitorConsole(new URL(manufacturerCMonitorConsoleURLString));

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

            try
            {
                String[] sensorNames = retailerMonitorConsole.listSensors();
                for (int sensorNameIndex = 0; (sensorNames != null) && (sensorNameIndex < sensorNames.length); sensorNameIndex++)
                    retailerMonitorConsole.removeSensor(sensorNames[sensorNameIndex]);

                String[] periodicSensorNames = retailerMonitorConsole.listPeriodicSensors();
                for (int periodicSensorNameIndex = 0; (periodicSensorNames != null) && (periodicSensorNameIndex < periodicSensorNames.length); periodicSensorNameIndex++)
                    retailerMonitorConsole.removePeriodicSensor(periodicSensorNames[periodicSensorNameIndex]);

                retailerMonitorConsole.addSensor("Retailer_SetupBasicService");
                retailerMonitorConsole.addPeriodicSensor("Retailer_SetupBasicService");
                retailerMonitorConsole.addSensor("Retailer_CustomerReferenceBasicService");
                retailerMonitorConsole.addPeriodicSensor("Retailer_CustomerReferenceBasicService");
                retailerMonitorConsole.addSensor("Retailer_PurchaseOrderBasicService");
                retailerMonitorConsole.addPeriodicSensor("Retailer_PurchaseOrderBasicService");
                retailerMonitorConsole.addSensor("Retailer_CatalogBasicService");
                retailerMonitorConsole.addPeriodicSensor("Retailer_CatalogBasicService");
                retailerMonitorConsole.addSensor("Retailer_CatalogItemBasicService");
                retailerMonitorConsole.addPeriodicSensor("Retailer_CatalogItemBasicService");
                retailerMonitorConsole.addSensor("Retailer_PurchaseOrderItemBasicService");
                retailerMonitorConsole.addPeriodicSensor("Retailer_PurchaseOrderItemBasicService");
                retailerMonitorConsole.addSensor("Retailer_ProductBasicService");
                retailerMonitorConsole.addPeriodicSensor("Retailer_ProductBasicService");
                retailerMonitorConsole.addSensor("Retailer_ManufacturerBasicService");
                retailerMonitorConsole.addPeriodicSensor("Retailer_ManufacturerBasicService");
                retailerMonitorConsole.addSensor("Retailer_InventoryBasicService");
                retailerMonitorConsole.addPeriodicSensor("Retailer_InventoryBasicService");
                retailerMonitorConsole.addSensor("Retailer_WarehouseBasicService");
                retailerMonitorConsole.addPeriodicSensor("Retailer_WarehouseBasicService");
                
                if (! retailerMonitorConsole.monitoringActive())
                    retailerMonitorConsole.startMonitoring();

                ADAPTLookAndFeel.generateTableEntry("Retailer", null, "Successful", null, writer);
            }
            catch (Exception exception)
            {
                ADAPTLookAndFeel.generateTableEntry("Retailer", null, "Unsuccessful", null, writer);
                System.err.println("Exception: " + exception);
                exception.printStackTrace(System.err);
            }
            catch (Error error)
            {
                ADAPTLookAndFeel.generateTableEntry("Retailer", null, "Unsuccessful", null, writer);
                System.err.println("Error: " + error);
                error.printStackTrace(System.err);
            }

            try
            {
                String[] sensorNames = manufacturerAMonitorConsole.listSensors();
                for (int sensorNameIndex = 0; (sensorNames != null) && (sensorNameIndex < sensorNames.length); sensorNameIndex++)
                    manufacturerAMonitorConsole.removeSensor(sensorNames[sensorNameIndex]);

                String[] periodicSensorNames = manufacturerAMonitorConsole.listPeriodicSensors();
                for (int periodicSensorNameIndex = 0; (periodicSensorNames != null) && (periodicSensorNameIndex < periodicSensorNames.length); periodicSensorNameIndex++)
                    manufacturerAMonitorConsole.removePeriodicSensor(periodicSensorNames[periodicSensorNameIndex]);
                
                manufacturerAMonitorConsole.addSensor("Manufacturer_SetupBasicService");
                manufacturerAMonitorConsole.addPeriodicSensor("Manufacturer_SetupBasicService");
                manufacturerAMonitorConsole.addSensor("Manufacturer_CustomerBasicService");
                manufacturerAMonitorConsole.addPeriodicSensor("Manufacturer_CustomerBasicService");
                manufacturerAMonitorConsole.addSensor("Manufacturer_PurchaseOrderBasicService");
                manufacturerAMonitorConsole.addPeriodicSensor("Manufacturer_PurchaseOrderBasicService");
                manufacturerAMonitorConsole.addSensor("Manufacturer_ItemBasicService");
                manufacturerAMonitorConsole.addPeriodicSensor("Manufacturer_ItemBasicService");
                manufacturerAMonitorConsole.addSensor("Manufacturer_FinishedGoodsBasicService");
                manufacturerAMonitorConsole.addPeriodicSensor("Manufacturer_FinishedGoodsBasicService");
                manufacturerAMonitorConsole.addSensor("Manufacturer_ProductionLineBasicService");
                manufacturerAMonitorConsole.addPeriodicSensor("Manufacturer_ProductionLineBasicService");
                manufacturerAMonitorConsole.addSensor("Manufacturer_InventoryItemBasicService");
                manufacturerAMonitorConsole.addPeriodicSensor("Manufacturer_InventoryItemBasicService");
                manufacturerAMonitorConsole.addSensor("Manufacturer_StorehouseBasicService");
                manufacturerAMonitorConsole.addPeriodicSensor("Manufacturer_StorehouseBasicService");
                
                if (! manufacturerAMonitorConsole.monitoringActive())
                    manufacturerAMonitorConsole.startMonitoring();

                ADAPTLookAndFeel.generateTableEntry("Manufacturer A", null, "Successful", null, writer);
            }
            catch (Exception exception)
            {
                ADAPTLookAndFeel.generateTableEntry("Manufacturer A", null, "Unsuccessful", null, writer);
                System.err.println("Exception: " + exception);
                exception.printStackTrace(System.err);
            }
            catch (Error error)
            {
                ADAPTLookAndFeel.generateTableEntry("Manufacturer A", null, "Unsuccessful", null, writer);
                System.err.println("Error: " + error);
                error.printStackTrace(System.err);
            }

            try
            {
                String[] sensorNames = manufacturerBMonitorConsole.listSensors();
                for (int sensorNameIndex = 0; (sensorNames != null) && (sensorNameIndex < sensorNames.length); sensorNameIndex++)
                    manufacturerBMonitorConsole.removeSensor(sensorNames[sensorNameIndex]);

                String[] periodicSensorNames = manufacturerBMonitorConsole.listPeriodicSensors();
                for (int periodicSensorNameIndex = 0; (periodicSensorNames != null) && (periodicSensorNameIndex < periodicSensorNames.length); periodicSensorNameIndex++)
                    manufacturerBMonitorConsole.removePeriodicSensor(periodicSensorNames[periodicSensorNameIndex]);

                manufacturerBMonitorConsole.addSensor("Manufacturer_SetupBasicService");
                manufacturerBMonitorConsole.addPeriodicSensor("Manufacturer_SetupBasicService");
                manufacturerBMonitorConsole.addSensor("Manufacturer_CustomerBasicService");
                manufacturerBMonitorConsole.addPeriodicSensor("Manufacturer_CustomerBasicService");
                manufacturerBMonitorConsole.addSensor("Manufacturer_PurchaseOrderBasicService");
                manufacturerBMonitorConsole.addPeriodicSensor("Manufacturer_PurchaseOrderBasicService");
                manufacturerBMonitorConsole.addSensor("Manufacturer_ItemBasicService");
                manufacturerBMonitorConsole.addPeriodicSensor("Manufacturer_ItemBasicService");
                manufacturerBMonitorConsole.addSensor("Manufacturer_FinishedGoodsBasicService");
                manufacturerBMonitorConsole.addPeriodicSensor("Manufacturer_FinishedGoodsBasicService");
                manufacturerBMonitorConsole.addSensor("Manufacturer_ProductionLineBasicService");
                manufacturerBMonitorConsole.addPeriodicSensor("Manufacturer_ProductionLineBasicService");
                manufacturerBMonitorConsole.addSensor("Manufacturer_InventoryItemBasicService");
                manufacturerBMonitorConsole.addPeriodicSensor("Manufacturer_InventoryItemBasicService");
                manufacturerBMonitorConsole.addSensor("Manufacturer_StorehouseBasicService");
                manufacturerBMonitorConsole.addPeriodicSensor("Manufacturer_StorehouseBasicService");
                
                if (! manufacturerBMonitorConsole.monitoringActive())
                    manufacturerBMonitorConsole.startMonitoring();

                ADAPTLookAndFeel.generateTableEntry("Manufacturer B", null, "Successful", null, writer);
            }
            catch (Exception exception)
            {
                ADAPTLookAndFeel.generateTableEntry("Manufacturer B", null, "Unsuccessful", null, writer);
                System.err.println("Exception: " + exception);
                exception.printStackTrace(System.err);
            }
            catch (Error error)
            {
                ADAPTLookAndFeel.generateTableEntry("Manufacturer B", null, "Unsuccessful", null, writer);
                System.err.println("Error: " + error);
                error.printStackTrace(System.err);
            }

            try
            {
                String[] sensorNames = manufacturerCMonitorConsole.listSensors();
                for (int sensorNameIndex = 0; (sensorNames != null) && (sensorNameIndex < sensorNames.length); sensorNameIndex++)
                    manufacturerCMonitorConsole.removeSensor(sensorNames[sensorNameIndex]);

                String[] periodicSensorNames = manufacturerCMonitorConsole.listPeriodicSensors();
                for (int periodicSensorNameIndex = 0; (periodicSensorNames != null) && (periodicSensorNameIndex < periodicSensorNames.length); periodicSensorNameIndex++)
                    manufacturerCMonitorConsole.removePeriodicSensor(periodicSensorNames[periodicSensorNameIndex]);

                manufacturerCMonitorConsole.addSensor("Manufacturer_SetupBasicService");
                manufacturerCMonitorConsole.addPeriodicSensor("Manufacturer_SetupBasicService");
                manufacturerCMonitorConsole.addSensor("Manufacturer_CustomerBasicService");
                manufacturerCMonitorConsole.addPeriodicSensor("Manufacturer_CustomerBasicService");
                manufacturerCMonitorConsole.addSensor("Manufacturer_PurchaseOrderBasicService");
                manufacturerCMonitorConsole.addPeriodicSensor("Manufacturer_PurchaseOrderBasicService");
                manufacturerCMonitorConsole.addSensor("Manufacturer_ItemBasicService");
                manufacturerCMonitorConsole.addPeriodicSensor("Manufacturer_ItemBasicService");
                manufacturerCMonitorConsole.addSensor("Manufacturer_FinishedGoodsBasicService");
                manufacturerCMonitorConsole.addPeriodicSensor("Manufacturer_FinishedGoodsBasicService");
                manufacturerCMonitorConsole.addSensor("Manufacturer_ProductionLineBasicService");
                manufacturerCMonitorConsole.addPeriodicSensor("Manufacturer_ProductionLineBasicService");
                manufacturerCMonitorConsole.addSensor("Manufacturer_InventoryItemBasicService");
                manufacturerCMonitorConsole.addPeriodicSensor("Manufacturer_InventoryItemBasicService");
                manufacturerCMonitorConsole.addSensor("Manufacturer_StorehouseBasicService");
                manufacturerCMonitorConsole.addPeriodicSensor("Manufacturer_StorehouseBasicService");
                
                if (! manufacturerCMonitorConsole.monitoringActive())
                    manufacturerCMonitorConsole.startMonitoring();

                ADAPTLookAndFeel.generateTableEntry("Manufacturer C", null, "Successful", null, writer);
            }
            catch (Exception exception)
            {
                ADAPTLookAndFeel.generateTableEntry("Manufacturer C", null, "Unsuccessful", null, writer);
                System.err.println("Exception: " + exception);
                exception.printStackTrace(System.err);
            }
            catch (Error error)
            {
                ADAPTLookAndFeel.generateTableEntry("Manufacturer C", null, "Unsuccessful", null, writer);
                System.err.println("Error: " + error);
                error.printStackTrace(System.err);
            }

            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);
    }
}
