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

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

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

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

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

	protected Relatorio relatorio;
	protected ResultSet resultSet;
	private Integer rowNum;
	protected List<Map<String, Object>> dados;

	public ArrayDataSource(Relatorio relatorio) throws Exception {
		this.relatorio = relatorio;
		this.dados = new ArrayList<Map<String, Object>>();
		this.initDados();
		this.rowNum = -1;
	}

	@Override
	public Object getFieldValue(JRField field) throws JRException {
		try {

			Object valueCustomField = this.valueCustomFields(field.getName());

			return (valueCustomField != null) ? valueCustomField : getByName(field.getName());

		} catch (Exception e) {
			log.error("", e);
			throw new JRException(e);

		}
	}

	protected Object getByName(String name) {

		try {
			return this.dados.get(this.rowNum).get(name);
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.sf.jasperreports.engine.JRDataSource#next()
	 */
	@Override
	public boolean next() {

		try {
			this.rowNum++;
			if (this.dados != null && this.rowNum < this.dados.size()) {
				return true;
			}
		} catch (Exception e) {
			log.error("", e);
		}

		return false;

	}

	@Override
	public void initDados() throws Exception {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rjconsultores.ventaboletos.relatorios.utilitarios.IDataSource#valueCustomFields(java.lang.String)
	 */
	public Object valueCustomFields(String fieldName) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
