/*
 * Created on Mar 9, 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 RackInputReport {
    Connection conn;
    String filename;
    Vector fields;
    
    /**
     * @param conn
     * @param filename
     */
    public RackInputReport(Connection conn, String filename) {
        super();
        this.conn = conn;
        this.filename = filename;
        initArrays();
        try {
            toFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @throws Exception
     * 
     */
    private void toFile() throws Exception {
        ResultSet res=conn.createStatement().executeQuery("select racks.*,locations.name as locname,modules.acronym as modname, upmass_resupply/stowage as upmass_ratio from racks,locations,modules where racks.location=locations.idx and locations.module=modules.idx");
        FileWriter out=new FileWriter(filename);
        
        for(int n=0;n!=fields.size();n++)
            out.write(((Field)fields.get(n)).label+"\t");
        out.write("\n");
        
        
        while(res.next())
        {
            Iterator i=fields.iterator();
            while(i.hasNext())
            {
                Field f=(Field) i.next();
                out.write(res.getObject(f.field)+"\t");
            }
            out.write("\n");
        }
        
        out.close();
    }

    /**
     * 
     */
    private void initArrays() {
        fields=new Vector();

        fields.add(new Field("acronym","Acronym"));
        fields.add(new Field("modname","Module")); //
        fields.add(new Field("locname","Rack Location")); //
        fields.add(new Field("cycles_per_increment","No. Cycles per Increment"));
        fields.add(new Field("hrs_per_cycle","Hours per Cycle (hrs)"));
        fields.add(new Field("upmass_resupply","Upmass-Resupply (kg)"));
        fields.add(new Field("mid_resupply","No. Shuttle Middecks-Resupply (MLEs)"));
        fields.add(new Field("stowage","On-Orbit Stowage Vol (MLEs)"));
        fields.add(new Field("upmass_ratio","Upmass-to-Stowage Volume Ratio (kg/MLE)")); //
        fields.add(new Field("power","Power (kW)"));
        fields.add(new Field("keepalive_power","Keep-Alive Power (kW)"));
        fields.add(new Field("crew_training","Crew Training (hrs)"));
        fields.add(new Field("crew_time","Crew Time (hrs/increment)"));
        fields.add(new Field("MTL_flow","MTL Flow Rate (lbs/hr)"));
        fields.add(new Field("MTL_flow_keepalive","MTL Flow Rate/Keep-Alive Mode (lbs/hr)"));
        fields.add(new Field("LTL_flow","LTL Flow Rate (lbs/hr)"));
        fields.add(new Field("LTL_flow_keepalive","LTL Flow Rate/Keep-Alive Mode (lbs/hr)"));
        fields.add(new Field("video_uplink","Video Uplink Duration (hrs)"));
        fields.add(new Field("video_downlink","Video Downlink Duration (hrs)"));
        fields.add(new Field("video_rate","Video Data Rate (Mbps)"));
        fields.add(new Field("nominal_uplink","Nominal Uplink Data Rate w/o Video (Mbps)"));
        fields.add(new Field("peak_uplink","Peak Uplink Data Rate (Mbps)"));
        fields.add(new Field("peak_uplink_duration","Peak Uplink Duration (hrs)"));
        fields.add(new Field("nominal_1553","Nominal 1553 Data Rate (Mbps)"));
        fields.add(new Field("nominal_ethernet","Nominal Ethernet Data Rate (Mbps)"));
        fields.add(new Field("nominal_HRL","Nominal HRL Data Rate (Mbps)"));
        fields.add(new Field("nominal_downlink","Nominal Downlink Data Rate w/o Video (Mbps)"));
        fields.add(new Field("peak_downlink","Peak Downlink Data Rate (Mbps)"));
        fields.add(new Field("peak_downlink_duration","Peak Downlink Duration (hrs)"));
        fields.add(new Field("peak_1553","Peak 1553 Data Rate (Mbps)"));
        fields.add(new Field("peak_1553_duration","Peak 1553 Duration (hrs)"));
        fields.add(new Field("peak_ethernet","Peak Ethernet Data Rate (Mbps)"));
        fields.add(new Field("peak_ethernet_duration","Peak Ethernet Duration (hrs)"));
        fields.add(new Field("peak_HRL","Peak HRL Data Rate (Mbps)"));
        fields.add(new Field("peak_HRL_duration","Peak HRL Duration (hrs)"));
    }
    
    private class Field{
        public String field,label;
        public Field(String field,String label){
            this.field=field;
            this.label=label;
        }
    }
}
