package com.sun.cadre.handlers;

import com.sun.jsftemplating.annotation.Handler;
import com.sun.jsftemplating.annotation.HandlerInput;
import com.sun.jsftemplating.annotation.HandlerOutput;
import com.sun.jsftemplating.layout.descriptors.handler.HandlerContext;
import com.sun.jsftemplating.component.dataprovider.MultipleListDataProvider;
import com.sun.webui.jsf.component.TableRowGroup;
import com.sun.webui.jsf.component.TableRowGroup;
import com.sun.cadre.common.DataAccessLayer;
import com.sun.cadre.common.QueryBuilder;
import java.sql.SQLException;
import javax.sql.rowset.RowSetMetaDataImpl;
import com.sun.rowset.CachedRowSetImpl;
import java.util.List;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;
import java.util.ResourceBundle;

// =========================================== CADRE USAGE REPORTS ===============================================
public class TableHandlers {

    @Handler(id = "getRepUsageTableData", output = {
@HandlerOutput(name = "tableData", type = java.util.List.class)
})
    public static void fetchRepUsageTableData(HandlerContext context) {
        System.out.println("In fetchRepUsageTableData ...");
        List<Object> tableData = new ArrayList<Object>();
        try {
            DataAccessLayer dal = new DataAccessLayer();
            CachedRowSetImpl crs = dal.getRepWebUsage();
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            int rows = 0;
            while (crs.next()) {
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString());
                tableData.add(rows, rEntries);
                rows++;
            }

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("repUsageTableData rows: " + tableData.size());
        context.setOutputValue("tableData", tableData);
    }

    @Handler(id = "getDef4TableData", input = {
@HandlerInput(name = "toolName", type = String.class)
}, output = {
@HandlerOutput(name = "tableData", type = java.util.List.class)
})
    public static void fetchDef4TableData(HandlerContext context) {

        System.out.println("In fetchDef4TableData...");

        String toolName = (String) context.getInputValue("toolName");

        //System.out.println("toolName: " +toolName);

        List<Object> tableData = new ArrayList<Object>();
        try {
            DataAccessLayer dal = new DataAccessLayer();
            CachedRowSetImpl crs = dal.getToolUsgAcrossLavaVers(toolName.trim());
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            int rows = 0;
            while (crs.next()) {
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString());
                tableData.add(rows, rEntries);
                rows++;
            }

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("def4TableData rows: " + tableData.size());
        context.setOutputValue("tableData", tableData);
    }

    @Handler(id = "getDef3TableData", input = {
@HandlerInput(name = "lavaVersion", type = String.class),
@HandlerInput(name = "buildVersion", type = String.class),
@HandlerInput(name = "toolName", type = String.class)
}, output = {
@HandlerOutput(name = "tableData", type = java.util.List.class)
})
    public static void fetchDef3TableData(HandlerContext context) {

        System.out.println("In fetchDef3TableData...");

        String lavaVersion = (String) context.getInputValue("lavaVersion");
        String buildVersion = (String) context.getInputValue("buildVersion");
        String toolName = (String) context.getInputValue("toolName");

        //System.out.println("lavaVersion: " +lavaVersion+ " buildVersion: " +buildVersion+ " toolName: " +toolName);

        List<Object> tableData = new ArrayList<Object>();
        try {
            DataAccessLayer dal = new DataAccessLayer();
            String newBuild = dal.getDailyBuild(lavaVersion, buildVersion);
            CachedRowSetImpl crs = dal.getToolUsgForDailyBuild(lavaVersion.trim(), newBuild.trim(), toolName.trim());
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            int rows = 0;
            while (crs.next()) {
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString());
                tableData.add(rows, rEntries);
                rows++;
            }

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("def3TableData rows: " + tableData.size());
        context.setOutputValue("tableData", tableData);
    }

    @Handler(id = "getDef2TableData", input = {
@HandlerInput(name = "lavaVersion", type = String.class),
@HandlerInput(name = "toolName", type = String.class),
@HandlerInput(name = "startDate", type = String.class),
@HandlerInput(name = "endDate", type = String.class)
}, output = {
@HandlerOutput(name = "sDate", type = String.class),
@HandlerOutput(name = "eDate", type = String.class),
@HandlerOutput(name = "tableData", type = java.util.List.class)
})
    public static void fetchDef2TableData(HandlerContext context) {

        System.out.println("In fetchDef2TableData...");

        String lavaVersion = (String) context.getInputValue("lavaVersion");
        String toolName = (String) context.getInputValue("toolName");
        String startDate = (String) context.getInputValue("startDate");
        String endDate = (String) context.getInputValue("endDate");

        String sDate = null;
        String eDate = null;

        if (startDate.length() != 0) {
            sDate = convertTStoDate(startDate);
        }
        if (endDate.length() != 0) {
            eDate = convertTStoDate(endDate);
        }

        //System.out.println("lavaVersion: " +lavaVersion+ " toolName: " +toolName);
        //System.out.println("startDate: " +startDate+ " endDate: " +endDate);
        //System.out.println("startDate: " +sDate+ " endDate: " +eDate);

        List<Object> tableData = new ArrayList<Object>();
        try {
            DataAccessLayer dal = new DataAccessLayer();
            CachedRowSetImpl crs = dal.getUsageOverDuration(lavaVersion.trim(), toolName.trim(), sDate.trim(), eDate.trim());
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            int rows = 0;
            while (crs.next()) {
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString());
                tableData.add(rows, rEntries);
                rows++;
            }

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("def2TableData rows: " + tableData.size());
        context.setOutputValue("sDate", sDate);
        context.setOutputValue("eDate", eDate);
        context.setOutputValue("tableData", tableData);
    }

    @Handler(id = "getDef5TableData", input = {
@HandlerInput(name = "lavaProject", type = String.class),
@HandlerInput(name = "majorBuildsOption", type = String.class),
@HandlerInput(name = "startDate", type = String.class),
@HandlerInput(name = "endDate", type = String.class)
}, output = {
@HandlerOutput(name = "sDate", type = String.class),
@HandlerOutput(name = "eDate", type = String.class),
@HandlerOutput(name = "tableData", type = java.util.List.class)
})
    public static void fetchDef5TableData(HandlerContext context) {

        System.out.println("In fetchDef5TableData...");

        String project = (String) context.getInputValue("lavaProject");
        String majorBuildsOption = (String) context.getInputValue("majorBuildsOption");
        String startDate = (String) context.getInputValue("startDate");
        String endDate = (String) context.getInputValue("endDate");

        boolean majorBuildFlag = true;
        if (majorBuildsOption.equals("false")) {
            majorBuildFlag = false;
        }

        String sDate = null;
        String eDate = null;

        if (startDate.length() != 0) {
            sDate = convertTStoDate(startDate);
        }

        if (endDate.length() != 0) {
            eDate = convertTStoDate(endDate);
        }

        //System.out.println("lavaProject: " +project+ " majorBuildsOption: " +majorBuildsOption);
        //System.out.println("startDate: " +startDate+ " endDate: " +endDate);
        //System.out.println("sDate: " +sDate+ " eDate: " +eDate);
        //System.out.println(majorBuildFlag);

        List<Object> tableData = new ArrayList<Object>();
        try {
            DataAccessLayer dal = new DataAccessLayer();
            CachedRowSetImpl crs = dal.getUsageOverBuilds(project.trim(), sDate.trim(), eDate.trim(), majorBuildFlag);
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            int rows = 0;
            while (crs.next()) {
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString());
                tableData.add(rows, rEntries);
                rows++;
            }

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("def5TableData rows: " + tableData.size());
        context.setOutputValue("sDate", sDate);
        context.setOutputValue("eDate", eDate);
        context.setOutputValue("tableData", tableData);
    }

    @Handler(id = "getCustomQueryData", input = {
@HandlerInput(name = "userName", type = Object.class),
@HandlerInput(name = "toolName", type = Object.class),
@HandlerInput(name = "project", type = String.class),
@HandlerInput(name = "feature", type = String.class),
@HandlerInput(name = "platform", type = String.class),
@HandlerInput(name = "build", type = String.class),
@HandlerInput(name = "lavaRelease", type = String.class),
@HandlerInput(name = "db", type = String.class),
@HandlerInput(name = "startDate", type = String.class),
@HandlerInput(name = "endDate", type = String.class)
}, output = {
@HandlerOutput(name = "customQueryTableData", type = java.util.List.class),
@HandlerOutput(name = "sDate", type = String.class),
@HandlerOutput(name = "eDate", type = String.class)
})
    public static void fetchCustomQueryTableData(HandlerContext context) {

        System.out.println("In fetchCustomQueryTableData...");

        Object[] userName = (Object[]) context.getInputValue("userName");
        Object[] toolName = (Object[]) context.getInputValue("toolName");
        String project = (String) context.getInputValue("project");
        String feature = (String) context.getInputValue("feature");
        String platform = (String) context.getInputValue("platform");
        String build = (String) context.getInputValue("build");
        String lavaRelease = (String) context.getInputValue("lavaRelease");
        String db = (String) context.getInputValue("db");
        String startDate = (String) context.getInputValue("startDate");
        String endDate = (String) context.getInputValue("endDate");

        String sDate = null;
        String eDate = null;

        QueryBuilder qBuilder = new QueryBuilder();
        if (startDate != null) {
            if (startDate.length() != 0) {
                sDate = convertTStoDate(startDate);
                qBuilder.setStart_date(sDate);
            }
        }
        if (endDate != null) {
            if (endDate.length() != 0) {
                eDate = convertTStoDate(endDate);
                qBuilder.setEnd_date(eDate);
            }
        }

        ArrayList<String> userNameList = new ArrayList<String>();
        if (userName.length > 0) {
            for (int i = 0; i < userName.length; i++) {
                userNameList.add(userName[i].toString());
            }
            qBuilder.setUsers(userNameList);
        }
        //System.out.println("userNameList length: " +userNameList.size());
        /*
        if (userName.length() > 0) {
        userNameList  = convertToAList(userName);
        qBuilder.setUsers(userNameList);
        }
         */
        ArrayList<String> toolNameList = new ArrayList<String>();
        if (toolName.length > 0) {
            for (int j = 0; j < toolName.length; j++) {
                toolNameList.add(toolName[j].toString());
            }
            qBuilder.setTools(toolNameList);
        }
        //System.out.println("toolNameList length: " +toolNameList.size());
        /*
        if (toolName.length() > 0) {
        toolNameList = convertToAList(toolName);
        qBuilder.setTools(toolNameList);
        }
         */

        ArrayList<String> projectList = new ArrayList<String>();
        if (project != null) {
            if (project.length() > 0) {
                projectList = convertToAList(project);
                qBuilder.setProjects(projectList);
            }
        }
        //System.out.println("projectList length: " +projectList.size());

        ArrayList<String> featureList = new ArrayList<String>();
        if (feature != null) {
            if (feature.length() > 0) {
                featureList = convertToAList(feature);
                qBuilder.setFeatures(featureList);
            }
        }
        //System.out.println("featureList length: " +featureList.size());

        ArrayList<String> platformList = new ArrayList<String>();
        if (platform != null) {
            if (platform.length() > 0) {
                platformList = convertToAList(platform);
                qBuilder.setPlatforms(platformList);
            }
        }
        //System.out.println("platformList length: " +platformList.size());

        ArrayList<String> buildList = new ArrayList<String>();
        if (build != null) {
            if (build.length() > 0) {
                buildList = convertToAList(build);
                qBuilder.setBuilds(buildList);
            }
        }
        //System.out.println("buildList length: " +buildList.size());

        ArrayList<String> lavaReleaseList = new ArrayList<String>();
        if (lavaRelease != null) {
            if (lavaRelease.length() > 0) {
                lavaReleaseList = convertToAList(lavaRelease);
                qBuilder.setLavaversions(lavaReleaseList);
            }
        }
        //System.out.println("lavaReleaseList length: " +lavaReleaseList.size());

        qBuilder.setDatabase_name(db);

        ArrayList<String> out_columns = new ArrayList<String>();
        out_columns.add("all");
        qBuilder.setOutput_columns(out_columns);

        //System.out.println(" project: " +project+ "lavaRelease: " +lavaRelease+ " db: " +db);
        //System.out.println("feature: " +feature+ " platform: " +platform+ " build: " +build);
        //System.out.println("startDate: " +startDate+ " endDate: " +endDate);
        //System.out.println("startDate: " +sDate+ " endDate: " +eDate);

        List<Object> customQueryTableData = new ArrayList<Object>();
        try {
            CachedRowSetImpl crs = qBuilder.Execute();
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            int rows = 0;
            while (crs.next()) {
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString());
                customQueryTableData.add(rows, rEntries);
                rows++;
            }

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("customQueryTableData rows: " + customQueryTableData.size());
        context.setOutputValue("customQueryTableData", customQueryTableData);
        context.setOutputValue("sDate", sDate);
        context.setOutputValue("eDate", eDate);
    }

    @Handler(id = "getUsageSummaryTableData", input = {
@HandlerInput(name = "lavaVersion", type = String.class),
@HandlerInput(name = "buildVersion", type = String.class),
@HandlerInput(name = "toolName", type = String.class)
}, output = {
@HandlerOutput(name = "usageSummaryTableData", type = java.util.List.class)
})
    public static void fetchUsageSummaryTableData(HandlerContext context) {

        System.out.println("In fetchUsageSummaryTableData...");
        String lava = (String) context.getInputValue("lavaVersion");
        String build = (String) context.getInputValue("buildVersion");
        String tool = (String) context.getInputValue("toolName");
        //System.out.println("lava: " +lava+ ", build: " +build);

        ResourceBundle bundle = ResourceBundle.getBundle("com.sun.cadre.resources.Resources");
        String projects = bundle.getString(lava.concat("_projects"));
        String[] pSplit = projects.split(" ");

        ArrayList<String> projectsList = new ArrayList<String>();
        for (int i = 0; i < pSplit.length; i++) {
            projectsList.add(pSplit[i]);
        }

        List<Object> usageTableData = new ArrayList<Object>();

        try {
            //System.out.println("In fetchUsageSummaryTableData try block...");
            DataAccessLayer dal = new DataAccessLayer();
            String newBuild = dal.getDailyBuild(lava, build);
            CachedRowSetImpl crs = dal.getUsageSummary(lava.trim(), newBuild.trim(), projectsList, tool);
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            //System.out.println("Column count is: " + rsmd.getColumnCount());
            int rows = 0;

            while (crs.next()) {
                //System.out.println("In while loop: " + rows);
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString()+ " : " +rEntries.get("col4").toString()+ " : " +rEntries.get("col5").toString());
                usageTableData.add(rows, rEntries);
                rows++;
            }
        //System.out.println("Table rows: " + rows);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("usageSummaryTableData rows: " + usageTableData.size());
        context.setOutputValue("usageSummaryTableData", usageTableData);
    }

    @Handler(id = "getNewUsageSummaryTableData", input = {
@HandlerInput(name = "lavaVersion", type = String.class),
@HandlerInput(name = "buildVersion", type = String.class),
@HandlerInput(name = "toolName", type = String.class)
}, output = {
@HandlerOutput(name = "usageSummaryTableData", type = java.util.List.class),
@HandlerOutput(name = "columnHeaders", type = java.util.List.class),
@HandlerOutput(name = "columnValues", type = java.util.List.class),
@HandlerOutput(name = "columnSorts", type = java.util.List.class),
@HandlerOutput(name = "columnWidths", type = java.util.List.class),
@HandlerOutput(name = "columnSpacers", type = java.util.List.class),
@HandlerOutput(name = "columnAligns", type = java.util.List.class)
})
    public static void fetchNewUsageSummaryTableData(HandlerContext context) {

        System.out.println("In fetchNewUsageSummaryTableData...");
        String lava = (String) context.getInputValue("lavaVersion");
        String build = (String) context.getInputValue("buildVersion");
        String tool = (String) context.getInputValue("toolName");
        //System.out.println("lava: " +lava+ ", build: " +build);

        List<String> columnValues = new ArrayList<String>();
        List<String> columnHeaders = new ArrayList<String>();
        List<String> columnSorts = new ArrayList<String>();
        List<String> columnWidths = new ArrayList<String>();
        List<String> columnSpacers = new ArrayList<String>();
        List<String> columnAligns = new ArrayList<String>();

        columnHeaders.add("Tool/Flow");
        columnAligns.add("left");

        Hashtable<String, String> settingsHash = new Hashtable<String, String>();
        DataAccessLayer dal = new DataAccessLayer();
        settingsHash = dal.getWebReportSettings();

        String projects = settingsHash.get(lava.concat("_projects")).toString().trim();
        String[] pSplit = projects.split(" ");

        ArrayList<String> projectsList = new ArrayList<String>();
        for (int i = 0; i < pSplit.length; i++) {
            projectsList.add(pSplit[i]);
            columnHeaders.add(pSplit[i]);
        }
        columnHeaders.add(" ");

        List<Object> usageTableData = new ArrayList<Object>();

        try {
            //System.out.println("In fetchUsageSummaryTableData try block...");

            String newBuild = dal.getDailyBuild(lava, build);
            CachedRowSetImpl crs = dal.getUsageSummary(lava.trim(), newBuild.trim(), projectsList, tool);
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            //System.out.println("Column count is: " + rsmd.getColumnCount());
            int rows = 0;

            for (int i = 1; i <= count; i++) {
                String temp = "#{tRow.value.col" + i + "}";
                String temp2 = "col" + i;
                columnValues.add(temp);
                columnSorts.add(temp2);
                columnSpacers.add("#{false}");
                columnWidths.add(" ");
                if (i != 1) {
                    columnAligns.add("right");
                }
            }

            columnValues.add(" ");
            columnSorts.add("${null}");
            columnAligns.add("right");
            columnSpacers.add("#{true}");
            if (pSplit.length < 4) {
                columnWidths.add("80%");
            } else {
                if (pSplit.length == 4 && pSplit.length < 8) {
                    columnWidths.add("60%");
                } else {
                    if (pSplit.length == 8 && pSplit.length > 10) {
                        columnWidths.add("30%");
                    } else {
                        columnWidths.add("10%");
                    }
                }
            }

            while (crs.next()) {
                //System.out.println("In while loop: " + rows);
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(columnHeaders.get(i));
                    //System.out.println(crs.getObject(1));
                    //System.out.println(crs.getObject(i));
                    String project = null;
                    if (i != 1) {
                        project = projectsList.get(i - 2);
                    }

                    String toolName = crs.getObject(1).toString().trim();
                    String entry = crs.getObject(i).toString().trim();
                    String tmp = null;

                    if (i != 1) {
                        tmp = "<a href=usageReportDetail.jsf?lava=" + lava + "&build=" + build + "&project=" + project + "&tool=" + toolName + "&feature=All>" + entry + "</a>";
                    } else {
                        tmp = crs.getObject(1).toString().trim();
                    }

                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, tmp);
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString()+ " : " +rEntries.get("col4").toString()+ " : " +rEntries.get("col5").toString());
                usageTableData.add(rows, rEntries);

                rows++;
            }
        //System.out.println("Table rows: " + rows);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("usageSummaryTableData rows: " + usageTableData.size());
        //System.out.println("columnHeaders rows: " + columnHeaders.size());
        //System.out.println("columnValues rows: " + columnValues.size());

        context.setOutputValue("usageSummaryTableData", usageTableData);
        context.setOutputValue("columnHeaders", columnHeaders);
        context.setOutputValue("columnValues", columnValues);
        context.setOutputValue("columnSorts", columnSorts);
        context.setOutputValue("columnWidths", columnWidths);
        context.setOutputValue("columnSpacers", columnSpacers);
        context.setOutputValue("columnAligns", columnAligns);
    }

    @Handler(id = "getTable1Data", input = {
@HandlerInput(name = "lavaVersion", type = String.class),
@HandlerInput(name = "buildVersion", type = String.class),
@HandlerInput(name = "lavaProject", type = String.class)
}, output = {
@HandlerOutput(name = "table1Data", type = java.util.List.class)
})
    public static void fetchTable1Data(HandlerContext context) {

        System.out.println("In fetchTable1Data...");
        String lava = (String) context.getInputValue("lavaVersion");
        String build = (String) context.getInputValue("buildVersion");
        String project = (String) context.getInputValue("lavaProject");
        //System.out.println("lava: " +lava+ ", build: " +build+ ", project: " +project);

        List<Object> t1Data = new ArrayList<Object>();

        try {
            //System.out.println("In fetchTable1Data try block...");
            DataAccessLayer dal = new DataAccessLayer();
            String newBuild = dal.getDailyBuild(lava, build);
            CachedRowSetImpl crs = dal.getFirstLevelUsageStats(lava.trim(), newBuild.trim(), project.trim());
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            //System.out.println("Column count is: " + rsmd.getColumnCount());
            int rows = 0;

            while (crs.next()) {
                //System.out.println("In while loop: " + rows);
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString());
                t1Data.add(rows, rEntries);
                rows++;
            }
        //System.out.println("Table rows: " + rows);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("t1Data rows: " + t1Data.size());
        context.setOutputValue("table1Data", t1Data);
    }

    @Handler(id = "updateTableData", input = {
@HandlerInput(name = "newDataList", type = java.util.List.class),
@HandlerInput(name = "tableRowGroup", type = com.sun.webui.jsf.component.TableRowGroup.class)
})
    public static void updateTableRowGroupData(HandlerContext context) {
        List<List<Object>> tData = new ArrayList<List<Object>>();
        tData = (List<List<Object>>) context.getInputValue("newDataList");
        TableRowGroup trGroup = (TableRowGroup) context.getInputValue("tableRowGroup");

        MultipleListDataProvider dp = (MultipleListDataProvider) trGroup.getAttributes().get("sourceData");
        dp.setLists(tData);
    }

    @Handler(id = "getTable2Data", input = {
@HandlerInput(name = "lavaVersion", type = String.class),
@HandlerInput(name = "buildVersion", type = String.class),
@HandlerInput(name = "lavaProject", type = String.class),
@HandlerInput(name = "toolName", type = String.class)
}, output = {
@HandlerOutput(name = "table2Data", type = java.util.List.class)
})
    public static void fetchTable2Data(HandlerContext context) {

        System.out.println("In fetchTable2Data...");
        String lava = (String) context.getInputValue("lavaVersion");
        String build = (String) context.getInputValue("buildVersion");
        String project = (String) context.getInputValue("lavaProject");
        String tool = (String) context.getInputValue("toolName");
        //System.out.println("lava: " +lava+ ", build: " +build+ ", project: " +project+ ", tool: " +tool);

        List<Object> t2Data = new ArrayList<Object>();

        try {
            //System.out.println("In fetchTable2Data try block...");
            DataAccessLayer dal = new DataAccessLayer();
            String newBuild = dal.getDailyBuild(lava, build);
            CachedRowSetImpl crs = dal.getSecLevelUsageStats(lava.trim(), newBuild.trim(), project.trim(), tool.trim());
            //CachedRowSetImpl crs =  dal.getSecLevelUsageStats("11.00.00", null, "ROCK", "emirviewer");
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            //System.out.println("Column count is: " + rsmd.getColumnCount());
            int rows = 0;

            while (crs.next()) {
                //System.out.println("In while loop: " + rows);
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString());
                t2Data.add(rows, rEntries);
                rows++;
            }
        //System.out.println("Table rows: " + rows);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("t2Data rows: " + t2Data.size());
        context.setOutputValue("table2Data", t2Data);
    }

    @Handler(id = "getTable3Data", input = {
@HandlerInput(name = "lavaVersion", type = String.class),
@HandlerInput(name = "buildVersion", type = String.class),
@HandlerInput(name = "lavaProject", type = String.class),
@HandlerInput(name = "toolName", type = String.class),
@HandlerInput(name = "featureName", type = String.class)
}, output = {
@HandlerOutput(name = "table3Data", type = java.util.List.class)
})
    public static void fetchTable3Data(HandlerContext context) {

        System.out.println("In fetchTable3Data...");
        String lava = (String) context.getInputValue("lavaVersion");
        String build = (String) context.getInputValue("buildVersion");
        String project = (String) context.getInputValue("lavaProject");
        String tool = (String) context.getInputValue("toolName");
        String feature = (String) context.getInputValue("featureName");
        //System.out.println("lava: " +lava+ ", build: " +build+ ", project: " +project+ ", tool: " +tool+ ", feature: " +feature);

        List<Object> t3Data = new ArrayList<Object>();

        try {
            //System.out.println("In fetchTable3Data try block...");
            DataAccessLayer dal = new DataAccessLayer();
            String newBuild = dal.getDailyBuild(lava, build);
            CachedRowSetImpl crs = dal.getThirdLevelUsageStats(lava.trim(), newBuild.trim(), project.trim(), tool.trim(), feature.trim());
            //CachedRowSetImpl crs =  dal.getThirdLevelUsageStats("11.00.00", "20061121", "ROCK" , "emirviewer", "InitTesting");
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            //System.out.println("Column count is: " + rsmd.getColumnCount());
            int rows = 0;

            while (crs.next()) {
                //System.out.println("In while loop: " + rows);
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString());
                t3Data.add(rows, rEntries);
                rows++;
            }
        //System.out.println("Table rows: " + rows);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("t3Data rows: " + t3Data.size());
        context.setOutputValue("table3Data", t3Data);
    }

    public static ArrayList<String> convertToAList(String input) {
        ArrayList<String> newList = new ArrayList<String>();
        if (input.equals("")) {
        //System.out.println(input+ " is not defined.");
        } else if (input.contains(",")) {
            String[] tList = input.split(",");
            for (int i = 0; i < tList.length; i++) {
                newList.add(tList[i].trim());
            }
        } else if (input.contains(" ")) {
            String[] tList = input.split(" ");
            for (int i = 0; i < tList.length; i++) {
                newList.add(tList[i].trim());
            }
        } else {
            //System.out.println(input+ " is just one word.");
            newList.add(input.trim());
        }

        //System.out.println("List size for " +input+ " is :" +newList.size());
        return newList;
    }

    public static String convertTStoDate(String input) {
        String[] inputs = input.split(" ");
        String output = inputs[2].concat("-").concat(inputs[1]).concat("-").concat(inputs[5]);
        return output;
    }

    @Handler(id = "testAddRemove", input = {
@HandlerInput(name = "userName", type = Object.class)
})
    public static void outTestAddRemove(HandlerContext context) {

        System.out.println("In outTestAddRemove ...");

        Object[] userName = (Object[]) context.getInputValue("userName");

        for (int i = 0; i < userName.length; i++) {
            System.out.println(userName[i].toString());
        }
    }

    // =========================================== CADRE PM REPORTS ===============================================
    @Handler(id = "getPMDefaultReport", input = {
@HandlerInput(name = "lavaVersion", type = String.class),
@HandlerInput(name = "lavaProject", type = String.class),
@HandlerInput(name = "buildVersion", type = String.class),
@HandlerInput(name = "buildRefVersion", type = String.class)
}, output = {
@HandlerOutput(name = "tableData", type = java.util.List.class)
})
    public static void fetchPMDefaultReport(HandlerContext context) {

        System.out.println("In fetchPMDefaultReport ...");

        String lava = (String) context.getInputValue("lavaVersion");
        String project = (String) context.getInputValue("lavaProject");
        String build = (String) context.getInputValue("buildVersion");
        String buildRef = (String) context.getInputValue("buildRefVersion");

        System.out.println("lava: " + lava + " project: " + project + " build: " + build + " buildRef: " + buildRef);

        List<Object> tableData = new ArrayList<Object>();
        try {
            DataAccessLayer dal = new DataAccessLayer();
            CachedRowSetImpl crs = dal.getPMDefaultReport(lava, project, build, buildRef);
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            int rows = 0;
            while (crs.next()) {
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                Map<Object, Object> rEntriesDel = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i) + " " + crs.getObject(i));
                    if (i > 2) {
                        int col = getColumnNoPMDefReport(i);
                        if (i % 2 == 0) {
                            if (crs.getObject(i) == null) {
                                rEntries.put("col" + col, "-");
                            } else {
                                rEntriesDel.put("col" + col, crs.getObject(i));
                            }
                        } else {
                            if (crs.getObject(i) == null) {
                                rEntriesDel.put("col" + col, "-");
                            } else {
                                rEntries.put("col" + col, crs.getObject(i));
                            }
                        }
                    } else {
                        if (crs.getObject(i) == null) {
                            rEntries.put("col" + i, "-");
                            rEntriesDel.put("col" + i, "-");
                        } else {
                            rEntries.put("col" + i, crs.getObject(i));
                            rEntriesDel.put("col" + i, crs.getObject(i));
                        }
                    }
                }

                rEntries.put("col17", "value");
                rEntriesDel.put("col17", "&#916; %");

                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString());
                tableData.add(rows, rEntries);
                rows = rows + 1;
                tableData.add(rows, rEntriesDel);
                rows++;
            }

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("PMDefaultTableData rows: " + tableData.size());
        context.setOutputValue("tableData", tableData);
    }

    public static int getColumnNoPMDefReport(int column) {
        int col = 1;
        switch (column) {
            case 1:
                col = 1;
                break;
            case 2:
                col = 2;
                break;
            case 3:
                col = 3;
                break;
            case 4:
                col = 3;
                break;
            case 5:
                col = 4;
                break;
            case 6:
                col = 4;
                break;
            case 7:
                col = 5;
                break;
            case 8:
                col = 5;
                break;
            case 9:
                col = 6;
                break;
            case 10:
                col = 6;
                break;
            case 11:
                col = 7;
                break;
            case 12:
                col = 7;
                break;
            case 13:
                col = 8;
                break;
            case 14:
                col = 8;
                break;
            case 15:
                col = 9;
                break;
            case 16:
                col = 9;
                break;
            case 17:
                col = 10;
                break;
            case 18:
                col = 10;
                break;
            case 19:
                col = 11;
                break;
            case 20:
                col = 11;
                break;
            case 21:
                col = 12;
                break;
            case 22:
                col = 12;
                break;
            case 23:
                col = 13;
                break;
            case 24:
                col = 13;
                break;
            case 25:
                col = 14;
                break;
            case 26:
                col = 14;
                break;
            case 27:
                col = 15;
                break;
            case 28:
                col = 15;
                break;
            case 29:
                col = 16;
                break;
            case 30:
                col = 16;
                break;
            default:
                System.out.println("Column out of range.");
                break;
        }
        return col;
    }

    @Handler(id = "getPMToolReport", input = {
@HandlerInput(name = "lavaVersion", type = String.class),
@HandlerInput(name = "lavaProject", type = String.class),
@HandlerInput(name = "tool", type = String.class),
@HandlerInput(name = "buildVersion", type = String.class),
@HandlerInput(name = "buildRefVersion", type = String.class)
}, output = {
@HandlerOutput(name = "tableData", type = java.util.List.class)
})
    public static void fetchPMToolReport(HandlerContext context) {

        System.out.println("In fetchPMToolReport ...");

        String lava = (String) context.getInputValue("lavaVersion");
        String project = (String) context.getInputValue("lavaProject");
        String tool = (String) context.getInputValue("tool");
        String build = (String) context.getInputValue("buildVersion");
        String buildRef = (String) context.getInputValue("buildRefVersion");

        System.out.println("lava: " + lava + " project: " + project + " tool: " + tool + " build: " + build + " buildRef: " + buildRef);

        List<Object> tableData = new ArrayList<Object>();
        try {
            DataAccessLayer dal = new DataAccessLayer();
            CachedRowSetImpl crs = dal.getPMToolReport(lava, project, tool, build, buildRef);
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            int rows = 0;
            while (crs.next()) {
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                Map<Object, Object> rEntriesDel = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i) + " " + crs.getObject(i));
                    if (i > 1) {
                        int col = getColumnNoPMToolReport(i);
                        if (i % 2 == 0) {
                            if (crs.getObject(i) == null) {
                                rEntries.put("col" + col, "-");
                            } else {
                                rEntries.put("col" + col, crs.getObject(i));
                            }
                        } else {
                            if (crs.getObject(i) == null) {
                                rEntriesDel.put("col" + col, "-");
                            } else {
                                rEntriesDel.put("col" + col, crs.getObject(i));
                            }
                        }
                    } else {
                        if (crs.getObject(i) == null) {
                            rEntries.put("col" + i, "-");
                            rEntriesDel.put("col" + i, "-");
                        } else {
                            rEntries.put("col" + i, crs.getObject(i));
                            rEntriesDel.put("col" + i, crs.getObject(i));
                        }
                    }
                }

                rEntries.put("col18", "value");
                rEntriesDel.put("col18", "&#916; %");

                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString());
                tableData.add(rows, rEntries);
                rows = rows + 1;
                tableData.add(rows, rEntriesDel);
                rows++;
            }

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("PMToolTableData rows: " + tableData.size());
        context.setOutputValue("tableData", tableData);
    }

    public static int getColumnNoPMToolReport(int column) {
        int col = 1;
        switch (column) {
            case 1:
                col = 1;
                break;
            case 2:
                col = 2;
                break;
            case 3:
                col = 2;
                break;
            case 4:
                col = 3;
                break;
            case 5:
                col = 3;
                break;
            case 6:
                col = 4;
                break;
            case 7:
                col = 4;
                break;
            case 8:
                col = 5;
                break;
            case 9:
                col = 5;
                break;
            case 10:
                col = 6;
                break;
            case 11:
                col = 6;
                break;
            case 12:
                col = 7;
                break;
            case 13:
                col = 7;
                break;
            case 14:
                col = 8;
                break;
            case 15:
                col = 8;
                break;
            case 16:
                col = 9;
                break;
            case 17:
                col = 9;
                break;
            case 18:
                col = 10;
                break;
            case 19:
                col = 10;
                break;
            case 20:
                col = 11;
                break;
            case 21:
                col = 11;
                break;
            case 22:
                col = 12;
                break;
            case 23:
                col = 12;
                break;
            case 24:
                col = 13;
                break;
            case 25:
                col = 13;
                break;
            case 26:
                col = 14;
                break;
            case 27:
                col = 14;
                break;
            case 28:
                col = 15;
                break;
            case 29:
                col = 15;
                break;
            case 30:
                col = 16;
                break;
            case 31:
                col = 16;
                break;
            case 32:
                col = 17;
                break;
            case 33:
                col = 17;
                break;
            default:
                System.out.println("Column out of range.");
                break;
        }
        return col;
    }

    @Handler(id = "getPMTCReport", input = {
@HandlerInput(name = "lavaVersion", type = String.class),
@HandlerInput(name = "lavaProject", type = String.class),
@HandlerInput(name = "tool", type = String.class),
@HandlerInput(name = "design", type = String.class),
@HandlerInput(name = "buildVersion", type = String.class)
}, output = {
@HandlerOutput(name = "tableData", type = java.util.List.class)
})
    public static void fetchPMTCReport(HandlerContext context) {

        System.out.println("In fetchPMTCReport ...");

        String lava = (String) context.getInputValue("lavaVersion");
        String project = (String) context.getInputValue("lavaProject");
        String tool = (String) context.getInputValue("tool");
        String design = (String) context.getInputValue("design");
        String build = (String) context.getInputValue("buildVersion");

        System.out.println("lava: " + lava + " project: " + project + " tool: " + tool + " design: " + design + " build: " + build);

        List<Object> tableData = new ArrayList<Object>();
        try {
            DataAccessLayer dal = new DataAccessLayer();
            CachedRowSetImpl crs = dal.getPMTCReport(lava, project, tool, design, build);
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            int rows = 0;
            while (crs.next()) {
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i) + " " +crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }

                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString());
                tableData.add(rows, rEntries);
                rows++;
            }

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("PMTCTableData rows: " + tableData.size());
        context.setOutputValue("tableData", tableData);
    }

    // =========================================== CADRE PERFORMANCE REPORTS ===============================================
    @Handler(id = "getPerfUsageTableData", input = {
@HandlerInput(name = "toolName", type = String.class),
@HandlerInput(name = "userName", type = String.class)
}, output = {
@HandlerOutput(name = "perfTableData", type = java.util.List.class)
})
    public static void fetchPerfUsageTableData(HandlerContext context) {

        System.out.println("In fetchPerfUsageTableData...");
        String tool = (String) context.getInputValue("toolName");
        String user = (String) context.getInputValue("userName");

        String[] userSplit = user.split(" , ");
        String fName = userSplit[0];
        String lName = userSplit[1];

        //System.out.println("tool: " +tool+ ", user: " +user);

        List<Object> perfTableData = new ArrayList<Object>();

        try {
            //System.out.println("In fetchUsageSummaryTableData try block...");
            DataAccessLayer dal = new DataAccessLayer();
            CachedRowSetImpl crs = dal.getPerfTopParent(tool, fName, lName);
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            //System.out.println("Column count is: " + rsmd.getColumnCount());
            int rows = 0;
            String blank = "-";

            while (crs.next()) {
                //System.out.println("In while loop: " + rows);
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, (Object) blank);
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString()+ " : " +rEntries.get("col4").toString()+ " : " +rEntries.get("col5").toString());
                perfTableData.add(rows, rEntries);
                rows++;
            }
        //System.out.println("Table rows: " + rows);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("perfTableData rows: " + perfTableData.size());
        context.setOutputValue("perfTableData", perfTableData);
    }

    @Handler(id = "getPerfUsageTable1Data", input = {
@HandlerInput(name = "jobID", type = String.class)
}, output = {
@HandlerOutput(name = "perfTableData", type = java.util.List.class)
})
    public static void fetchPerfUsageTable1Data(HandlerContext context) {

        System.out.println("In fetchPerfUsageTable1Data...");
        String jobID = (String) context.getInputValue("jobID");

        //System.out.println("jobID: " +jobID);

        List<Object> perfTableData = new ArrayList<Object>();

        try {
            //System.out.println("In fetchUsageSummaryTableData try block...");
            DataAccessLayer dal = new DataAccessLayer();
            CachedRowSetImpl crs = dal.getPerfChildJobs(jobID);
            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            //System.out.println("Column count is: " + rsmd.getColumnCount());
            int rows = 0;
            String blank = "-";

            while (crs.next()) {
                //System.out.println("In while loop: " + rows);
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, (Object) blank);
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString()+ " : " +rEntries.get("col4").toString()+ " : " +rEntries.get("col5").toString());
                perfTableData.add(rows, rEntries);
                rows++;
            }
        //System.out.println("Table rows: " + rows);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("perfTableData rows: " + perfTableData.size());
        context.setOutputValue("perfTableData", perfTableData);
    }

    @Handler(id = "getPerfTableData", input = {
@HandlerInput(name = "lavaVersion", type = String.class),
@HandlerInput(name = "project", type = String.class),
@HandlerInput(name = "design", type = String.class),
@HandlerInput(name = "toolName", type = String.class),
@HandlerInput(name = "perfFunc", type = String.class),
@HandlerInput(name = "perfMetric", type = String.class),
@HandlerInput(name = "buildType", type = String.class)
}, output = {
@HandlerOutput(name = "tableData", type = java.util.List.class),
@HandlerOutput(name = "columnHeaders", type = java.util.List.class),
@HandlerOutput(name = "columnValues", type = java.util.List.class),
@HandlerOutput(name = "columnSorts", type = java.util.List.class),
@HandlerOutput(name = "columnWidths", type = java.util.List.class),
@HandlerOutput(name = "columnSpacers", type = java.util.List.class),
@HandlerOutput(name = "columnAligns", type = java.util.List.class)
})
    public static void fetchPerfTableData(HandlerContext context) {

        System.out.println("In fetchPerfTableData...");
        String lava = (String) context.getInputValue("lavaVersion");
        String project = (String) context.getInputValue("project");
        String design = (String) context.getInputValue("design");
        String tool = (String) context.getInputValue("toolName");
        String operation = (String) context.getInputValue("perfFunc");
        String metric = (String) context.getInputValue("perfMetric");
        String build_type = (String) context.getInputValue("buildType");
        System.out.println("lava: " + lava + ", project: " + project + ", design: " + design + ", tool: " + tool);
        System.out.println("operation: " + operation + ", metric: " + metric + ", build_type: " + build_type);

        List<String> columnValues = new ArrayList<String>();
        List<String> columnHeaders = new ArrayList<String>();
        List<String> columnSorts = new ArrayList<String>();
        List<String> columnWidths = new ArrayList<String>();
        List<String> columnSpacers = new ArrayList<String>();
        List<String> columnAligns = new ArrayList<String>();

        if (design == null && tool != null) {
            columnHeaders.add("Design Name");
        } else {
            columnHeaders.add("Tool/Flow");
        }

        columnAligns.add("left");

        List<Object> tableData = new ArrayList<Object>();

        try {
            //System.out.println("In fetchUsageSummaryTableData try block...");
            DataAccessLayer dal = new DataAccessLayer();
            CachedRowSetImpl crs = null;
            if (design == null && tool != null) {
                crs = dal.getPerfDesignTable(lava.trim(), project.trim(), tool.trim(), operation.trim(), metric.trim(), build_type.trim());
            } else {
                crs = dal.getPerfToolTable(lava.trim(), project.trim(), design.trim(), operation.trim(), metric.trim(), build_type.trim());
            }

            RowSetMetaDataImpl rsmd = (RowSetMetaDataImpl) crs.getMetaData();
            int count = rsmd.getColumnCount();
            //System.out.println("Column count is: " + rsmd.getColumnCount());
            int rows = 0;

            for (int i = 1; i <= count; i++) {
                String temp = "#{tRow.value.col" + i + "}";
                String temp2 = "col" + i;
                columnValues.add(temp);
                columnSorts.add(temp2);
                columnSpacers.add("#{false}");
                columnWidths.add(" ");
                if (i != 1) {
                    columnAligns.add("right");
                    columnHeaders.add(rsmd.getColumnName(i).substring(4));
                }
            }
            columnHeaders.add(" ");

            columnValues.add(" ");
            columnSorts.add("${null}");
            columnAligns.add("right");
            columnSpacers.add("#{true}");
            if (count < 4) {
                columnWidths.add("80%");
            } else {
                if (count == 4 && count < 8) {
                    columnWidths.add("60%");
                } else {
                    if (count == 8 && count > 10) {
                        columnWidths.add("30%");
                    } else {
                        columnWidths.add("10%");
                    }
                }
            }

            while (crs.next()) {
                //System.out.println("In while loop: " + rows);
                Map<Object, Object> rEntries = new Hashtable<Object, Object>();
                for (int i = 1; i <= count; i++) {
                    //System.out.println(rsmd.getColumnName(i));
                    //System.out.println(columnHeaders.get(i));
                    //System.out.println(crs.getObject(1));
                    //System.out.println(crs.getObject(i));
                    if (crs.getObject(i) == null) {
                        rEntries.put("col" + i, "-");
                    } else {
                        rEntries.put("col" + i, crs.getObject(i));
                    }
                }
                //System.out.println(rEntries.get("col1").toString() + " : " +rEntries.get("col2").toString() + " : " +rEntries.get("col3").toString()+ " : " +rEntries.get("col4").toString()+ " : " +rEntries.get("col5").toString());
                tableData.add(rows, rEntries);

                rows++;
            }
        //System.out.println("Table rows: " + rows);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // Set the output.
        System.out.println("tableData rows: " + tableData.size());
        //System.out.println("columnHeaders rows: " + columnHeaders.size());
        //System.out.println("columnValues rows: " + columnValues.size());

        context.setOutputValue("tableData", tableData);
        context.setOutputValue("columnHeaders", columnHeaders);
        context.setOutputValue("columnValues", columnValues);
        context.setOutputValue("columnSorts", columnSorts);
        context.setOutputValue("columnWidths", columnWidths);
        context.setOutputValue("columnSpacers", columnSpacers);
        context.setOutputValue("columnAligns", columnAligns);
    }
}