package usda.weru.weps.reports.query;

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

/**
 *
 * @author joelevin
 */
public abstract class WepsResultSet extends SimpleResultSet {
    
    private boolean olderRunCheck;
// used for debugging    WepsResultSetMetaData metaData;
    
    public WepsResultSet () {
        super();
        olderRunCheck = false;
//        metaData = new WepsResultSetMetaData();
        //metaData.setColumnCnt(getColumnCount());
        //boolean b = this.getAutoClose();
        this.setAutoClose(false);
    }


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

    @Override
    public void close() {
        try {
            beforeFirst();
        } catch (SQLException e) {
//            e.printStackTrace();
        }
    }
    
    @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);
        // super will call addCol w/ TypeName (below)
        // it will call metaData.addCol
        //metaData.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);
//       metaData.addColumn(name, sqlType, sqlTypeName, precision, scale);
    }
    //colClass is a rawtype and method is never called.
    //public void addColumn(String name, int sqlType, int precision, int scale, Class colClass) {
      //  addColumn(name, sqlType, precision, scale);
//        int col;
//        try {
//            col = metaData.getColumnCount()-1;
//            metaData.setColumnClassName (col, colClass.getCanonicalName());
//        } catch (SQLException ex) {
//        }
   // }
    
//    @Override
//    public ResultSetMetaData getMetaData() {
//        return metaData;
//    }
    
    // Don't need all these unsupported returns
    // Just let the super handle it.
    // (Many of the super methods return the same)
    //
//    @Override
//    public RowId getRowId(int columnIndex) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public RowId getRowId(String columnLabel) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateRowId(int columnIndex, RowId x) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateRowId(String columnLabel, RowId x) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateNString(int columnIndex, String nString) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateNString(String columnLabel, String nString) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateNClob(int columnIndex, NClob nClob) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateNClob(String columnLabel, NClob nClob) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public NClob getNClob(int columnIndex) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public NClob getNClob(String columnLabel) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public SQLXML getSQLXML(int columnIndex) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public SQLXML getSQLXML(String columnLabel) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public Reader getNCharacterStream(int columnIndex) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public Reader getNCharacterStream(String columnLabel) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateClob(int columnIndex, Reader reader, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateClob(String columnLabel, Reader reader, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateCharacterStream(int columnIndex, Reader x) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateClob(int columnIndex, Reader reader) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateClob(String columnLabel, Reader reader) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateNClob(int columnIndex, Reader reader) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void updateNClob(String columnLabel, Reader reader) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public <T> T unwrap(Class<T> iface) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public boolean isWrapperFor(Class<?> iface) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }


    
    //
    // These all added for debugging.
    // Might as well leave the code for possible future use...
    //
//    @Override
//    public boolean previous () throws SQLException {
//        boolean b = super.previous();
//        return b;
//    }
//    
//    @Override
//    public boolean relative (int i) throws SQLException {
//        boolean b = super.relative(i);
//        return b;
//    }
//
//    
//    @Override
//    public int getRow () {
//        return super.getRow();
//    }
//    
//    @Override
//    public int getInt (int colIdx) throws SQLException {
//        return super.getInt(colIdx);
//    }
//    
//    @Override
//    public int getInt (String colName) throws SQLException {
//        return super.getInt(colName);
//    }
//    
//    @Override
//    public int getType () {
//        int t = super.getType();
//        int tt = 0;
//        try {
//            tt = this.getStatement().getResultSetType();
//        } catch (SQLException ex) {
//        }
//        return ResultSet.TYPE_FORWARD_ONLY;
//    }
//    
//    @Override
//    public int getFetchDirection() {
//        int d = super.getFetchDirection();
//        return ResultSet.FETCH_FORWARD;
//    }
//    
//    @Override
//    public boolean absolute(int row) throws SQLException {
//        boolean b = super.absolute(row);
//        return b;
//    }
//    
//    @Override
//    public void beforeFirst () throws SQLException {
//        try {
//            super.beforeFirst();
//        } catch (SQLException ex) {
//            int j = 1;
//        } catch (DbException ex) {
//            //jdbcSQLException e;
//            int j = 1;
//        }
//    }
//    
//    @Override
//    public boolean first () {
//        boolean b = false;
//        try {
//            b = super.first();
//        } catch (SQLException ex) {
//            Exceptions.printStackTrace(ex);
//        }
//        return b;
//    }
//    
//    @Override
//    public boolean last () throws SQLException {
//        boolean b = super.last();
//        return b;
//    }
//    
//    
//    @Override
//    public int getColumnCount(){
//        int i = super.getColumnCount();
//        return i;
//    }
//
//    @Override
//    public int getColumnDisplaySize(int column) {
//        int i = super.getColumnDisplaySize(column);
//        return i;
//    }
//
//    @Override
//    public String getColumnLabel(int column) throws SQLException {
//        String ret = super.getColumnLabel(column);
//        return ret;
//    }
//
//    @Override
//    public String getColumnName(int column) throws SQLException {
//        String ret = super.getColumnName(column);
//        return ret;
//    }
//
//    @Override
//    public int getColumnType(int column) throws SQLException {
//        int i = super.getColumnType(column);
//        return i;
//    }
//
//    @Override
//    public String getColumnTypeName(int column) throws SQLException {
//        String ret = super.getColumnTypeName(column);
//        return ret;
//    }
//
//    @Override
//    public String getColumnClassName(int column) throws SQLException {
//        String ret = super.getColumnClassName(column);
//        return ret;
//    }
//
//    @Override
//    public URL getURL (int i) throws SQLException {
//        URL ret = super.getURL(i);
//        return ret;
//    }
//
//    @Override
//    public URL getURL (String s) throws SQLException {
//        URL ret = super.getURL(s);
//        return ret;
//    }
//    
//    @Override
//    public boolean isAfterLast () throws SQLException {
//        boolean ret = super.isAfterLast();
//        return ret;
//    }
//    
//    @Override
//    public boolean isBeforeFirst () throws SQLException {
//        boolean ret = super.isBeforeFirst();
//        return ret;
//    }
//    
//    @Override
//    public boolean isClosed () {
//        boolean ret = super.isClosed();
//        return ret;
//    }
//    
//    @Override
//    public boolean isFirst () throws SQLException {
//        boolean ret = super.isFirst();
//        return ret;
//    }
//    
//    @Override
//    public boolean isLast () throws SQLException {
//        boolean ret = super.isLast();
//        return ret;
//    }
}
