package com.rjconsultores.ventaboletos.relatorios.utilitarios;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.apache.log4j.Logger;

import com.rjconsultores.ventaboletos.relatorios.render.RenderRelatorioJasper;

/**
 * @author Bruno H. G. Gouvêa <bruno@rjconsultores.com.br>
 * 
 */
public abstract class Relatorio {

	private static Logger log = Logger.getLogger(Relatorio.class);

	private Connection conexao;
	protected Map<String, Object> parametros;
	protected RenderRelatorioJasper render;
	private IDataSource customDataSource;
	private IParametros processadorParametros;
	private Set<String> infoMsg;
	private JRBeanCollectionDataSource collectionDataSource;
	private String[] nomeSubReporte;

	protected Relatorio(Map<String, Object> parametros, Connection conexao) {
		this.parametros = parametros;
		this.conexao = conexao;
		this.infoMsg = new HashSet<String>();
	}
	
	public Connection getConexao() {
		return conexao;
	}

	public void setConexao(Connection conexao) {
		this.conexao = conexao;
	}

	public String getNome() {
		return this.getClass().getSimpleName();
	}

	public boolean isCustomDataSource() {
		return this.customDataSource != null;

	}

	public boolean isCollectionDataSource() {
		return this.collectionDataSource != null;

	}

	public Map<String, Object> getParametros() {
		return parametros;
	}

	public JRBeanCollectionDataSource getCollectionDataSource() {
		return collectionDataSource;
	}

	public void setCollectionDataSource(JRBeanCollectionDataSource collectionDataSource) {
		this.collectionDataSource = collectionDataSource;
	}

	/**
	 * @return the customDataSource
	 */
	public IDataSource getCustomDataSource() {
		return customDataSource;
	}

	/**
	 * @param iDataSource
	 *            the customDataSource to set
	 */
	public void setCustomDataSource(IDataSource iDataSource) {
		setCustomDataSource(true,iDataSource);
	}
	/**
	 * @param iDataSource
	 *            the customDataSource to set
	 */
	public void setCustomDataSource(boolean closeConnection,IDataSource iDataSource) {
		this.customDataSource = iDataSource;
		if (closeConnection){
			try {
				if (!this.conexao.isClosed())
					this.conexao.close();
			} catch (SQLException e) {
				log.error("Erro ao fechar a conexion", e); 
			}
		}
	}

	public IParametros getProcessadorParametros() {
		return processadorParametros;
	}

	public void setProcessadorParametros(IParametros processadorParametros) {
		this.processadorParametros = processadorParametros;
	}

	protected abstract void processaParametros() throws Exception;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rjconsultores.ventaboletos.relatorios.utilitarios.IRenderRelatorio#getConteudo()
	 */
	public byte[] getConteudo(SaidaRelatorio saida) throws Exception {
		byte[] conteudo = null;
		try {

			if (this.render == null) {
				this.processaParametros();
				this.render = new RenderRelatorioJasper(this);
			}

			conteudo = this.render.render(saida);

		} catch (Exception e) {
			if (!this.conexao.isClosed())
				this.conexao.close();
			throw e;
		} finally {
			if (!this.conexao.isClosed())
				this.conexao.close();
		}

		return conteudo;

	}

	public Set<String> getInfoMsg() {
		return infoMsg;
	}

	public void addInfoMsg(String msg) {
		this.infoMsg.add(msg);
	}

	public String[] getNomeSubReporte() {
		return nomeSubReporte;
	}

	public void setNomeSubReporte(String[] nomeSubReporte) {
		this.nomeSubReporte = nomeSubReporte;
	}

}