package com.rjconsultores.ventaboletos.relatorios.utilitarios;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.rjconsultores.ventaboletos.web.utilerias.NamedParameterStatement;

public abstract class ArrayCustomDataSourceRelatorio extends ArrayDataSource {

	public ArrayCustomDataSourceRelatorio(Relatorio relatorio) throws Exception {
		super(relatorio);
	}

	@Override
	public void initDados() throws Exception {
		Connection conexao = this.relatorio.getConexao();
		Map<String, Object> parametros = this.relatorio.getParametros();
		StringBuilder instrucaoSql = new StringBuilder();
		
		getSql(instrucaoSql, parametros);

		NamedParameterStatement stmt = new NamedParameterStatement(conexao, instrucaoSql.toString());
		
		configuraParametrosConsultaSql(parametros, stmt);

		ResultSet rset = stmt.executeQuery();
		
		while (rset.next()) {
			Map<String, Object> dataResult = new HashMap<String, Object>();
			populaValoresDataResult(dataResult, rset);
			dados.add(dataResult);
		}
		resultSet = rset;
	}

	protected abstract void getSql(StringBuilder instrucaoSql, Map<String, Object> parametros);
	protected abstract void populaValoresDataResult(Map<String, Object> dataResult, ResultSet resultSet) throws SQLException;
	protected abstract void configuraParametrosConsultaSql(Map<String, Object> parametros, NamedParameterStatement stmt) throws SQLException;
}
