Return to Snippet

Revision: 5025
at February 6, 2008 08:38 by narkisr


Initial Code
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import java.util.*;

public class HibernateQueryResultDataSource implements JRDataSource {

	private final Iterator<Map<String, Object>> iterator;

	private Map<String, Object> currentValue;

	/**
	 this is only for clarity
	 */
	private final String name;

	public HibernateQueryResultDataSource(List<Map<String, Object>> list, String name) {
		this.iterator = list.iterator();
		this.name=name;
	}

	public Object getFieldValue(JRField field) throws JRException {
		return currentValue.get(field.getName());
	}

	public boolean next() throws JRException {
		currentValue = iterator.hasNext() ? iterator.next() : null;
		return (currentValue != null);
	}

	public Iterator<Map<String, Object>> getIterator() {
		return iterator;
	}
	
}


// A usage example 

private JRPdfExporter jRPdfExporter;

private boolean compileAndCreatePDF(final String pdfFileName, final JRDataSource ds){
        try {
            jRPdfExporter.setParameter(JRPdfExporterParameter.CHARACTER_ENCODING, "UTF-8");
            jRPdfExporter.setParameter(JRPdfExporterParameter.FONT_MAP, fontMap);
            final JasperPrint jasperPrint = JasperFillManager.fillReport(GeneratedPath + compiler.getRelevantEntryReportName(), new HashMap(0), ds);
            jRPdfExporter.setParameter(JRPdfExporterParameter.JASPER_PRINT, jasperPrint);
            jRPdfExporter.setParameter(JRPdfExporterParameter.OUTPUT_FILE_NAME, System.getProperty("java.io.tmpdir") + "/" + pdfFileName);
            jRPdfExporter.exportReport();
            return true;
        } catch (Exception e) {
            log.error(e);
            return false;
        }
    }

Initial URL


Initial Description
The data source which can be used in combination with jasper reports.

Initial Title
Jasper report hibernate data source

Initial Tags
java

Initial Language
Java