/*
 * Created on Mar 20, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.nasa.jsc.plum.Reports;

import java.io.FileWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Vector;

/**
 * @author randrews
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ExpressReport {

    Connection conn;
    String filename;
    String[] labels,fields,comments,racknames;

    /**
     * @param conn
     * @param filename
     */
    public ExpressReport(Connection conn, String filename) {
        super();
        this.conn = conn;
        this.filename = filename;
        initArrays();
        try {
            toFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void initArrays(){
        labels=new String[]{
                "Experiment Runs/Increment",
                "Time/run (hrs)",
                "Upmass resupply (kg)",
                "Middeck resupply (MLEs)",
                "Stowage (MLEs)",
                "Power (kW)",
                "Keep-alive power (kW)",
                "Training (hrs)",
                "Crewtime (hrs)",
                "MTL flow(lbs/hr)",
                "MTL flow/keep-alive mode (lbs/hr)",
                "Vid. uplink duration (hrs)",
                "Vid. downlink duration (hrs)",
                "Vid. Data rate (Mbps)",
                "Uplink. data rate (Mbps)",
                "Peak uplink rate (Mbps)",
                "Peak uplink duration (hrs)",
                "Nom. 1553 rate (Mbps)",
                "Nom. Ethernet rate (Mbps)",
                "Nom. HRL rate (Mbps)",
                "Nom. Downlink rate w/o video (Mbps)",
                "Peak downlink rate (Mbps)",
                "Peak downlink duration (hrs)",
                "Peak 1553 rate (Mbps)",
                "Peak 1553 duration (hrs)",
                "Peak Ethernet rate (Mbps)",
                "Peak Ethernet duration (hrs)",
                "Peak HRL rate (Mbps)",
                "Peak HRL duration (hrs)"
        };
        
        fields=new String[]{
                "cycles_per_increment",
                "hrs_per_cycle",
                "upmass_resupply",
                "mid_resupply",
                "stowage",
                "power",
                "keepalive_power",
                "crew_training",
                "crew_time",
                "MTL_flow",
                "MTL_flow_keepalive",
                "video_uplink",
                "video_downlink",
                "video_rate",
                "nominal_uplink",
                "peak_uplink",
                "peak_uplink_duration",
                "nominal_1553",
                "nominal_ethernet",
                "nominal_HRL",
                "nominal_downlink",
                "peak_downlink",
                "peak_downlink_duration",
                "peak_1553",
                "peak_1553_duration",
                "peak_ethernet",
                "peak_ethernet_duration",
                "peak_HRL",
                "peak_HRL_duration"
        };
        
        comments=new String[]{
                "runtime_comment",
                "",
                "",
                "middecks_comment",
                "stowage_comment",
                "",
                "",
                "",
                "crewtime_comment",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                "",
                ""
        };
        
        racknames=new String[]{
                "EXP 1",
                "EXP 2A",
                "EXP 3A",
                "EXP 4",
                "EXP 5",
                "EXP 6",
                "EXP 7A",
                "EXP 8A"
        };
    }
    
    private void toFile() throws Exception{
        Vector expressracks=new Vector();
        for(int n=0;n!=racknames.length;n++)
        {
            ResultSet res=conn.createStatement().executeQuery("select racks.*,locations.name as locname from racks,locations where racks.location=locations.idx and racks.acronym=\""+racknames[n]+"\"");
            res.next();
            expressracks.add(res);
        }
        
        FileWriter out=new FileWriter(filename);
        out.write("<html>\n<head><title>EXPRESS Table</title></head>\n<body>\n\n");
        
        out.write("<table>\n");
        
        
        out.write("<tr>\n"); // Begin rack name row
        out.write("\t<td></td>\n"); // The first column is used for field names;
        Iterator i=expressracks.iterator();
        while(i.hasNext())
        {
            ResultSet res=(ResultSet) i.next();
            out.write("\t<td colspan=2>"+res.getString("acronym")+"</td>\n");
        }
        out.write("</tr>\n");
        
        out.write("<tr>\n"); // Begin express comment row
        out.write("\t<td></td>\n"); // The first column is used for field names;
        i=expressracks.iterator();
        while(i.hasNext())
        {
            ResultSet res=(ResultSet) i.next();
            out.write("\t<td colspan=2>"+res.getString("express_comment")+"</td>\n");
        }
        out.write("</tr>\n");
        
        out.write("<tr>\n"); // Begin location row
        out.write("\t<td>Location</td>\n");
        i=expressracks.iterator();
        while(i.hasNext())
        {
            ResultSet res=(ResultSet) i.next();
            out.write("\t<td colspan=2>"+res.getString("locname")+"</td>\n");
        }
        out.write("</tr>\n");
        
        out.write("<tr>\n"); // Begin name comment row
        out.write("\t<td>Configuration</td>\n");
        i=expressracks.iterator();
        while(i.hasNext())
        {
            ResultSet res=(ResultSet) i.next();
            out.write("\t<td colspan=2>"+res.getString("name_comment")+"</td>\n");
        }
        out.write("</tr>\n");
        
        for(int n=0;n!=fields.length;n++) // Begin all the rest of the rows
        {
            out.write("<tr>\n");
            out.write("\t<td>"+labels[n]+"</td>\n");
            i=expressracks.iterator();
            while(i.hasNext())
            {
                ResultSet res=(ResultSet) i.next();
                out.write("\t<td>"+res.getObject(fields[n])+"</td>\n");
                if(comments[n].equals(""))
                    out.write("\t<td></td>\n");
                else
                    out.write("\t<td>"+res.getString(comments[n])+"</td>\n");
            }
            out.write("</tr>\n");
        }
                
        out.write("</table>\n</body>\n</html>");
        out.close();
    }
}
