// SimpleTable.java
// A test of the JTable class using default table models and a convenience
// constructor.
//
package com.metlife.ins.product.disability.common.utils.gmi;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

import java.sql.Timestamp;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Vector;



import com.metlife.ins.product.disability.common.utils.gmi.IConverter;
import com.metlife.ins.product.disability.common.utils.gmi.GMIFactory;
import com.metlife.ins.product.disability.common.utils.gmi.GMIConstants;
import com.metlife.ins.product.disability.common.utils.MetSTDCIUtil;
import com.metlife.ins.product.disability.common.utils.Validation;
import com.metlife.ins.product.disability.common.utils.IMetConstants;
import com.metlife.ins.product.disability.common.data.GMIInputDO ;
import com.metlife.ins.product.disability.common.data.ClaimResultsetDO ;
import com.metlife.ins.product.disability.common.data.CaseStructureDO;
import com.metlife.ins.product.disability.common.exception.GMIException;
import com.metlife.ins.product.disability.common.exception.MetSTDCIException;
import com.metlife.ins.product.disability.common.manager.ConnectionManager;
import com.metlife.ins.product.disability.common.manager.PropertyManager;



// A basic implementation of the TableModel interface that fills out a Vector of
// String[] structures from a query's result set.
//


public class PriyankSimpleTable extends AbstractTableModel {
  Vector cache;  // will hold String[] objects . . .
  int colCount;
  String[] headers;
  Connection db;
  Statement statement;
  String currentURL;
    static
     {
            try
            {
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
                    //PropertyManager.initializeProperties("STDCI.properties");
            }catch (ClassNotFoundException cnfe)
            {
                    System.out.println("Error gettting Connection class");
                    cnfe.printStackTrace();
            }
            catch(Exception ex)
            {
                                    ex.printStackTrace();
            }

       } 


  public PriyankSimpleTable() {
    cache = new Vector(); 
      
    
   // new gsl.sql.driv.Driver();
  }
   
  public String getColumnName(int i) { return headers[i]; }
  public int getColumnCount() { return colCount; }
  public int getRowCount() { return cache.size();}
  public void addRow()
  {

  }

  public Object getValueAt(int row, int col) { 
    return ((String[])cache.elementAt(row))[col];
  }

  public void setHostURL(String url) {
    if (url.equals(currentURL)) {
      // same database, we can leave the current connection open
      return;
    }
    // Oops . . . new connection required
    closeDB();
    initDB(url);
    currentURL = url;
  }

  // All the real work happens here; in a real application, 
  // we'd probably perform the query in a separate thread.
  public void setQuery(String q) {
    cache = new Vector();
    try {
      // Execute the query and store the result set and its metadata
      ResultSet rs = statement.executeQuery(q);
      ResultSetMetaData meta = rs.getMetaData();
      colCount = meta.getColumnCount();

      // Now we must rebuild the headers array with the new column names
      headers = new String[colCount];
      for (int h=1; h <= colCount; h++) {
        headers[h-1] = meta.getColumnName(h);
      }

      // and file the cache with the records from our query.  This would not be
      // practical if we were expecting a few million records in response to our
      // query, but we aren't, so we can do this.
      while (rs.next()) {
        String[] record = new String[colCount];
        for (int i=0; i < colCount; i++) {
          record[i] = rs.getString(i + 1);
        }
        cache.addElement(record);
      }
      fireTableChanged(null); // notify everyone that we have a new table.
    }
    catch(Exception e) {
      cache = new Vector(); // blank it out and keep going.
      e.printStackTrace();
    }
  }

  public void initDB(String url) {
    try {
            String dsn = "MET_SESS";
            String userid = "stdci";
            String password = "stdci123";
                      
            db = DriverManager.getConnection("jdbc:db2:"+dsn, userid, password);
            System.out.println("connection to DB2 database established");
            statement = db.createStatement();
         }catch (SQLException sqle)
         {
            System.out.println("SQLError : Error getting connection from Legacy Database");
            sqle.printStackTrace();
         }
     	 catch(Exception ex)
  	 {
  		//	log.error(logContext,EVENTS.ADHOC,"Could not connect to simulation DB");
  			ex.printStackTrace();
         }
          
  }

  public void closeDB() {
    try {
      if (statement != null) { statement.close(); }
      if (db != null) {        db.close(); }
    }
    catch(Exception e) {
      System.out.println("Could not close the current connection.");
      e.printStackTrace();
    }
  }
}


