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


import gov.nasa.jsc.plum.PlumUtil.DatabaseWindow;
import gov.nasa.jsc.plum.PlumUtil.ListPanel;

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

import javax.swing.JOptionPane;

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

    Connection conn;
    TreeMap runs;
    
    /**
     * @param parent
     * @param conn
     */
    public RunListPanel(DatabaseWindow parent, Connection conn) {
        super(parent);
        this.conn = conn;
        runs=new TreeMap();
        
        updateWindow();
    }
    
    /* (non-Javadoc)
     * @see gov.nasa.jsc.plum.PlumRunner.ListPanel#deleteElement(java.lang.Object)
     */
    public void deleteElement(Object selectedValue) {
        super.deleteElement(selectedValue);
        try{
            ResultSet idxres=conn.createStatement().executeQuery("select idx from runs where name=\""+selectedValue+"\"");
            idxres.next();
            int idx=idxres.getInt(1);
            conn.createStatement().execute("delete from runs where name=\""+selectedValue+"\"");
            conn.createStatement().execute("delete from runcons where run="+idx);
            conn.createStatement().execute("delete from selection where run="+idx);
            conn.createStatement().execute("delete from comments where tblname=\"runs\" and value=\""+idx+"\"");
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    /* (non-Javadoc)
     * @see gov.nasa.jsc.plum.PlumRunner.ListPanel#newElement()
     */
    public void newElement() {
        String name=(String)JOptionPane.showInputDialog(
                null,
                "Name of run to create:",
                "Run name",
                JOptionPane.PLAIN_MESSAGE);
        if(name==null || name.equals(""))return;
        System.out.println("name: "+name);
        
        try{
            conn.createStatement().execute("insert into runs (name) values (\""+name+"\")");
            Statement stmt=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
            ResultSet res=stmt.executeQuery("select * from runs where name=\""+name+"\"");
            res.next();
            runs.put(name,res);
            int idx=res.getInt("idx");
            
            conn.createStatement().execute("insert into selection (rack,status,run) select idx as rack,0 as status,"+idx+" as run from racks"); 
            
            model.addElement(name);
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    /* (non-Javadoc)
     * @see gov.nasa.jsc.plum.PlumRunner.ListPanel#handleSelection(java.lang.Object)
     */
    public void handleSelection(Object selected) {
        ResultSet res=(ResultSet) runs.get(selected);
        RunWindow window;
        try {
            window = new RunWindow(conn,parent,res);
            window.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /* (non-Javadoc)
     * @see gov.nasa.jsc.plum.PlumUtil.Updateable#update()
     */
    public void updateWindow() {
        try{
            ResultSet res=conn.createStatement().executeQuery("select name from runs");
            Vector names=new Vector();
            while(res.next())
            {
                String name=res.getString("name");
                Statement stmt=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
                ResultSet run=stmt.executeQuery("select * from runs where name=\""+name+"\"");
                run.next();
                runs.put(name,run);
                names.add(name);
            }
            
            fillModel(names);
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}
