/**
 * Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */

package com.opengamma.integration.copier.sheet.reader;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.util.ArgumentChecker;

/**
 * A class to facilitate importing portfolio data from a JDBC query result
 */
public class JdbcSheetReader extends SheetReader {

  private DataSource _dataSource;
  private JdbcTemplate _jdbcTemplate;
  private ResultSet _resultSet;
  private String[] _row;
  private List<Map<String, String>> _results;
  private Iterator<Map<String, String>> _iterator;
  
  public JdbcSheetReader(DataSource dataSource, String query) {
    init(dataSource, query, null);
  }
  
  public JdbcSheetReader(DataSource dataSource, String query, PreparedStatementSetter preparedStatementSetter) {
    ArgumentChecker.notNull(preparedStatementSetter, "prepared statement setter");
    init(dataSource, query, preparedStatementSetter);
  }
  
  protected void init(DataSource dataSource, String query, PreparedStatementSetter preparedStatementSetter) {
    ArgumentChecker.notNull(dataSource, "dataSource");
    
    _jdbcTemplate = new JdbcTemplate(dataSource);
     
    ResultSetExtractor<List<Map<String, String>>> extractor = new ResultSetExtractor<List<Map<String, String>>>() {
      @Override
      public List<Map<String, String>> extractData(ResultSet rs) throws SQLException, DataAccessException {
        String[] columns = new String[rs.getMetaData().getColumnCount()];
        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
          columns[i] = rs.getMetaData().getColumnName(i + 1);
        }
        setColumns(columns);
        List<Map<String, String>> entries = new ArrayList<Map<String, String>>();
        while (rs.next()) {
          String[] rawRow = new String[rs.getMetaData().getColumnCount()];
          for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
            rawRow[i] = rs.getString(i + 1);
          }
          Map<String, String> result = new HashMap<String, String>();
          // Map read-in row onto expected columns
          for (int i = 0; i < getColumns().length; i++) {
            if (i >= rawRow.length) {
              break;
            }
            if (rawRow[i] != null && rawRow[i].trim().length() > 0) {
              result.put(getColumns()[i], rawRow[i]);
            }
          }
          
        }
        return entries;
      }
    };
    if (preparedStatementSetter != null) {
      _results = getJDBCTemplate().query(query, preparedStatementSetter, extractor);
    } else {
      _results = getJDBCTemplate().query(query, extractor);
      _iterator = _results.iterator();
    }
  }
  
  private JdbcTemplate getJDBCTemplate() {
    return _jdbcTemplate;
  }

  @Override
  public Map<String, String> loadNextRow() {
    return _iterator.next();
  }

  @Override
  public void close() {
  }
}

@@ -485,24 +485,81 @@
 ataSource;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.s
@@ -1355,16 +1355,105 @@
 erator;%0A
+  private static final Logger s_logger = LoggerFactory.getLogger(JdbcSheetReader.class);%0A
   %0A  pub
@@ -2627,24 +2627,63 @@
 s.next()) %7B%0A
+          s_logger.info(%22Read a row%22);%0A
           St
@@ -3282,24 +3282,44 @@
 %7D%0A          
+entries.add(result);
 %0A        %7D%0A 
@@ -3555,24 +3555,28 @@
 tractor);%0A  
+  %7D%0A
     _iterato
@@ -3600,22 +3600,16 @@
 ator();%0A
-    %7D%0A
   %7D%0A  %0A 
@@ -3746,32 +3746,142 @@
     
-return _iterator.next();
+if (_iterator.hasNext()) %7B%0A      s_logger.info(%22Returned a row%22);%0A      return _iterator.next();%0A    %7D else %7B%0A      return null;%0A    %7D
 %0A  %7D

Make it actually work.