/*
 * -------------------------------------------------------------------------
 *      $Id: ReadStockData.java,v 1.4 2003/04/09 19:47:34 brophy Exp $
 * -------------------------------------------------------------------------
 *      Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved.
 *
 *      This software is confidential information which is proprietary to
 *      and a trade secret of Visual Numerics, Inc.  Use, duplication or
 *      disclosure is subject to the terms of an appropriate license
 *      agreement.
 *
 *      VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 *      SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 *      BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 *      FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL
 *      NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 *      AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 *      ITS DERIVATIVES.
 *--------------------------------------------------------------------------
 */

package com.imsl.demo.WallStreet;
import java.sql.*;
import java.text.*;



/**
 *http://kumo.swcp.com/stocks
 *  20010125,A,56.6875,57.25,54,55.25,30641
 *
 * 2001 01 25 ,A,56.6875,57.25,54,55.25,30641
 * @author  brophy
 * @created January 24, 2002
 */
class ReadStockData extends com.imsl.io.FlatFile {
    static private final String columnNames[] = {
        "Date", "Ticker", "Open", "High", "Low", "Close", "Volume"};

    static private final Class columnClass[] = {
        java.sql.Date.class, String.class,
        Double.class, Double.class, Double.class, Double.class, Double.class};

    static private final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");


    /** Creates new ReadStockData */
    public ReadStockData(String filename) throws java.io.IOException {
        super(filename);
        setColumnName(1, "Date");
        setColumnName(2, "Ticker");
        setColumnName(3, "Open");
        setColumnName(4, "High");
        setColumnName(5, "Low");
        setColumnName(6, "Close");
        setColumnName(7, "Volume");
        setColumnClass(1, java.sql.Date.class);
        setColumnClass(2, String.class);
        for (int k = 3;  k <= 7;  k++)  setColumnClass(k, Double.class);
    }

    public Object getObject(int columnIndex) throws SQLException {
        try {
            Object obj = super.getObject(columnIndex);
            if (!(obj instanceof String))  return obj;
            String s = (String)obj;
            switch (columnIndex) {
                case 1:
                    return dateFormat.parse(s);
                case 2:
                    return s;
                default:
                    return Double.valueOf(s);
            }
        } catch (ParseException e) {
            throw new SQLException(e.getMessage());
        } catch (NumberFormatException e) {
            throw new SQLException(e.getMessage());
        }
    }    
    
    /** Returns the number of columns in this ResultSet object.
     *
     * @return the number of columns
     * @exception SQLException if a database access error occurs
     *
     */
    public Class getColumnClass(int columnIndex) throws SQLException {
        return columnClass[columnIndex-1];
    }
}