/**
 * 
 */
package es.dalocar.facturamas.reports;

import java.io.InputStream;
import java.util.Collection;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 * Clase base abstracta para la creación de clases de informes (Report)
 * 
 * @author dlopez
 *
 */
public abstract class ReportBase implements Report {
	
	private Map params;

	private Collection collection;

	
	
	/**
	 * Public constructor
	 * @param params Mapa de parámetros para el informe
	 * @param beans Collección de beans a mostrar
	 */
	public ReportBase(Map params, Collection beans) {
		this.params = params;
		this.collection = beans;
	}

	/**
	 * 
	 * @see es.valencia.proyeduca.report.Report#configure(java.util.Map)
	 */
	public void configure(Map params) {
		this.params = params;
	}

	/**
	 * Establecer la coleccion de beans
	 * @param collection
	 */
	public void setBeans(Collection collection) {
		this.collection = collection;
	}

	/**
	 * 
	 * @see es.valencia.proyeduca.report.Report#getParams()
	 */
	public Map getParams() {
		return params;
	}

	/**
	 * 
	 * @see es.valencia.proyeduca.report.Report#getBeans()
	 */
	public Collection getBeans() {
		return collection;
	}


	/**
	 * Establecer el nombre de archivo del informe
	 * @return
	 */
	protected abstract String getFilename();
	
	/**
	 * 
	 * @see es.valencia.proyeduca.report.Report#getJasperPrint()
	 */
	public JasperPrint getJasperPrint() {
		InputStream is = this.getClass().getResourceAsStream(getFilename());
		JRBeanCollectionDataSource jrds = new JRBeanCollectionDataSource(getBeans());
		try {
			return JasperFillManager.fillReport(is, getParams(), jrds);
		} catch (JRException e) {
			throw new RuntimeException(e);
		}
	}
}
