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

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.TreeMap;

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

    public static boolean setTopology(Connection conn,String name){
        try {
            if(name==null || name.equals(""))
            {
                System.out.println("Told to set null topology; setting default instead.");
                name="default";
            }
            else
                System.out.println("Setting topology "+name);
            
            ResultSet topology=conn.createStatement().executeQuery("select * from topology where name=\""+name+"\"");
            TreeMap topo=new TreeMap();
            while(topology.next())
                topo.put(topology.getObject("rack"),topology.getObject("location"));
            
            Statement stmt=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
            ResultSet racks=stmt.executeQuery("select idx,location from racks");
            while(racks.next())
            {
                racks.updateObject("location",topo.get(racks.getObject("idx")));
                racks.updateRow();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    public static void makeRack(Connection conn,int idx,int location,String name) throws SQLException{
        conn.createStatement().execute("insert into racks (idx,location,acronym) values ("+idx+","+location+",\""+name+"\")");
        
        ResultSet res;
        
        // Insert into selections
        res=conn.createStatement().executeQuery("select distinct run from selection");
        while(res.next())
            conn.createStatement().execute("insert into selection (run,rack,status) values ("+res.getObject(1)+","+idx+","+"0)");
        
        // Insert into topologies
        res=conn.createStatement().executeQuery("select distinct name from topology");
        while(res.next())
            conn.createStatement().execute("insert into topology (name,rack,location) values (\""+res.getObject(1)+"\","+idx+","+location+")");
    }

    public static void deleteRack(Connection conn,String acronym) throws SQLException{
        ResultSet res=conn.createStatement().executeQuery("select idx from racks where acronym=\""+acronym+"\"");
        if(res.next())
            deleteRack(conn,res.getInt(1));
    }
    
    public static void deleteRack(Connection conn,int idx) throws SQLException{
        conn.createStatement().execute("delete from racks where idx="+idx);
        conn.createStatement().execute("delete from topology where rack="+idx);
        conn.createStatement().execute("delete from selection where rack="+idx);
        conn.createStatement().execute("delete from constraints where rack="+idx);
        conn.createStatement().execute("delete from runcons where cons not in (select name from constraints)");
        conn.createStatement().execute("delete from comments where tblname=\"racks\" and indexfield=\"idx\" and value=\""+idx+"\"");
    }
    
    public static void makeTopology(Connection conn,String name) throws SQLException{
        conn.createStatement().execute("insert into topology (rack,location,name) select rack,location,\""+name+"\" as name from topology where name=\"default\"");
    }
    
    public static void deleteTopology(Connection conn,String name) throws SQLException{
        conn.createStatement().execute("delete from topology where name=\""+name+"\"");
        conn.createStatement().execute("delete from comments where tblname=\"topology\" and indexfield=\"name\" and value=\""+name+"\"");
    }
}
