package usda.weru.weps.reports.query;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import org.h2.tools.SimpleResultSet;

/**
 *
 * @author joelevin
 */
public abstract class WepsResultSet extends SimpleResultSet {
    
    private boolean olderRunCheck;
    
    public WepsResultSet () {
        super();
        olderRunCheck = false;
        this.setAutoClose(false);
   }


    public abstract void fill() throws SQLException;
    public abstract String getName();

    @Override
    public void close() {
        try {
            beforeFirst();
        } catch (SQLException e) {
        }
    }
    
    @Override
    public boolean next () throws SQLException {
        boolean b = super.next();
        if (!b) {
            super.beforeFirst();
        }
        return b;
    }
    
    public void dispose() {
        super.close();
    }

    /**
     * Creates a new Object array the size of the resultset getColumnCount().
     * @param add If true, the row will be added to the resultset
     * @return an empty object array.
     * @throws SQLException Upon an unexpected error. This should be very rare.
     */
    protected Object[] createNewRow(boolean add) throws SQLException {
        Object[] row = new Object[getColumnCount()];
        if (add) {
            addRow(row);
        }
        return row;
    }
    
    public void setOlderRunCheck(boolean b) {
        olderRunCheck = b;
    }

    public boolean getOlderRunCheck() {
        return olderRunCheck;
    }

    /**
     * Convenience method for adding values based on their column names. Slight
     * performance hit is to be expected, but helps prevent programming errors
     * due to future column reordering.
     * @param row
     * @param columnName column label to be resolved to the 0 based index.
     * @param value Object to store
     * @throws java.sql.SQLException
     */
    protected void setRowValue(Object[] row, String columnName, Object value) throws SQLException {
        int index = findColumn(columnName) - 1;
        row[index] = value;
    }

    protected <T> T getRowValue(T[] row, String columnName) throws SQLException {
        int index = findColumn(columnName) - 1;
        return row[index];
    }

    @Override
    public void addColumn(String name, int sqlType, int precision, int scale) {
        super.addColumn(name, sqlType, precision, scale);
    }

    @Override
    public void addColumn(String name, int sqlType, String sqlTypeName, int precision, int scale) {
        super.addColumn(name, sqlType, sqlTypeName, precision, scale);
    }
    
    public void dump () {
        try {
            ResultSetMetaData rsmd = this.getMetaData();
            int columnsNumber = rsmd.getColumnCount();
            System.out.println("ResultSet dump for: "+this.getName());
            while (this.next()) {
                for (int i = 1; i <= columnsNumber; i++) {
                    if (i > 1) System.out.print(",  ");
                    String columnValue = this.getString(i);
                    System.out.print(rsmd.getColumnName(i)+":" + columnValue + " | ");
                }
                System.out.println("");
            }
        } catch (Exception ex) {
        }
    }

}
