/*
 * Created on Mar 7, 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.FileNotFoundException;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.ResultSet;

/**
 * @author randrews
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DDCUReport {
    Connection conn;
    String filename;
    

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


    /**
     * @throws FileNotFoundException
     * 
     */
    private void toFile() throws Exception {
        FileWriter out=new FileWriter(filename);
        ResultSet res=conn.createStatement().executeQuery("select distinct ddcu from locations");
        out.write("DDCU\tLocation\tRack Acronym\tName\tPower (kW)\tKeepalive Power (kW)\tOperating Time (hrs)\n");
        while(res.next())
        {
            String ddcu=res.getString("ddcu");
            if(ddcu==null)continue;
            out.write(ddcu);
            System.out.println(ddcu);
            out.write("\n");
            ResultSet racks=conn.createStatement().executeQuery("select acronym,racks.name as name,racks.power as power,racks.keepalive_power as keepalive,hrs_per_cycle * cycles_per_increment as runtime,locations.name as locname from racks,locations where racks.location=locations.idx and locations.ddcu=\""+ddcu+"\"");
            while(racks.next())
            {
                out.write("\t"+racks.getString("locname")+"\t");
                out.write(racks.getString("acronym")+"\t");
                out.write(racks.getString("name")+"\t");
                out.write(racks.getString("power")+"\t");
                out.write(racks.getString("keepalive")+"\t");
                out.write(racks.getString("runtime")+"\n");
            }
        }
        out.close();
    }
}
