/**
 * 
 */
package com.rjconsultores.ventaboletos.relatorios.utilitarios;

import java.sql.ResultSet;
import java.sql.SQLException;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

import org.apache.log4j.Logger;

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

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

	protected Relatorio relatorio;
	protected ResultSet resultSet;
	protected Integer rowNum;

	public DataSource(Relatorio relatorio) throws Exception {
		this.relatorio = relatorio;
		this.initDados();
		this.rowNum = -1;
	}

	@Override
	public Object getFieldValue(JRField field) throws JRException {
		try {
			Object valueCustomField = this.valueCustomFields(field.getName());

			return (valueCustomField != null) ? valueCustomField : this.resultSet.getObject(field.getName());
		} catch (Exception e) {
			e.printStackTrace();
			throw new JRException(e);
			
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.sf.jasperreports.engine.JRDataSource#next()
	 */
	@Override
	public boolean next() {
		try {
			this.rowNum++;
			return this.resultSet != null && this.resultSet.next();
		} catch (SQLException e) {
			log.error("", e);
			return false;
		}
	}

	@Override
	public void initDados() throws Exception {

	}
	
	public Object valueCustomFields(String fieldName) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
