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

import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

/**
 * @author randrews
 *
 */
public class DataSet implements Cloneable{

    private Table table;
    private Vector rows;
    private static final char TERMINATOR=';';
    
    public Object clone() throws CloneNotSupportedException{
        return super.clone();
    }
    
    /**
     * Makes a new (empty) DataSet from a table
     * @param table The table structure
     */
    public DataSet(Table table){
        this.table=table;
        rows=new Vector();
    }

    
    /**
     * Makes a new DataSet with the structure of the given table, getting data from the given connection
     * @param table The table to use
     * @param conn A connection to the database
     */
    public DataSet(Table table,Connection conn){
        this.table=table;
        rows=new Vector();
        loadData(conn);
    }

    /**
     * Same as the last one, just with the name of the table instead of a Table object.
     * @param name the table's name
     * @param conn a connection to the DB.
     */
    public DataSet(String name,Connection conn){
        table=new Table(name,conn);
        rows=new Vector();
        loadData(conn);
    }
    
    private void loadData(Connection conn){
        try {
            ResultSet data=conn.createStatement().executeQuery("select * from "+table.getName());
            // This depends on the fields being returned in a regular, if not deterministic, order.
            while(data.next())
            {
                Row r=new Row(data);
                r.setStatus(Row.NEW);
                rows.add(r);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }   
    }
    
    /**
     * Write code to generate the rows in this DataSet (not the table) to the given output stream
     * @param out The output stream
     */
    public void write(OutputStream out){
    		for(int n=0;n!=rows.size();n++)
    		{
    			Row r=(Row)rows.get(n);
    			try {
					out.write(r.toString().getBytes());
					out.write('\n');
				} catch (IOException e) {
					e.printStackTrace();
				}
    		}
    }
    
    public String toString(){
        String str="";
        for(int n=0;n!=rows.size();n++)
        {
            Row r=(Row)rows.get(n);
            str+=r.toString();
            str+="\n";
        }
        return str;
    }


    /**
     * @return the name of the table
     */
    public String getName(){
    		return table.getName();
    }
    
    /**
     * @return Returns the table.
     */
    public Table getTable() {
        return table;
    }
    
    /**
     * @return Returns the rows.
     */
    public Vector getRows() {
        return rows;
    }
    
    /**
     * Adds a row to this dataset
     * @param r the row to add
     */
    public void addRow(Row r){
        rows.add(r);
    }
    
    /**
     * Finds and returns the first row that has the given value for the given field.
     * "First" means "has the lowest value for the primary key" if there are multiple.
     * @param field name of the field
     * @param value The value; use null for no value.
     * @return the row as a Row object.
     */
    public Row find(String field,Object value){
        for(int n=0;n!=rows.size();n++)
        {
            Row r=(Row)rows.get(n);
            if(r.get(field).equals(value))return r;
        }
        return null;
    }
}
