JasperReports Bar Chart JavaAgent

/**
 * Created: 2013.11.14.10.34.PM
 * JasperReportsCSVDataSourceBarChartJavaAgent
 * CSV data for JasperReports Chart, to be shown using Xpages/JavaBean
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

import lotus.domino.AgentBase;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRCsvDataSource;



/**
 * @author Dököll Solutions, Inc.
 * @version 2013.11.14.10.34.PM
 * 
 */

public class JasperReportsCSVBarChartDemo extends AgentBase{
	
	//Set up static path/file variables
    public static final String FILE_PATH ="C:\\Users\\Administrator\\JasperReportFiles\\";
    public static final String OUTFILE_NAME ="JasperReportsCSVBarChart.pdf";
    
    public JasperReportsCSVBarChartDemo(){
	
	}
    
    //Uncomment/Run this 'static void main' to Test code
	//public static void main (String[] args){
    //TODO: Test as JavaBean from Project Explorer
	public void NotesMain() { 
		try
		{

		HashMap param = new HashMap();	
		//Grab the CSV dataSource
    	JRCsvDataSource dataSource = getDataSource(); 
    	System.out.println("Datasource obtained...");
    	//plug dataSource records in Jasper file
        JasperPrint jasperPrint = JasperFillManager.fillReport("\\Jasper\\JasperSoft\\IReport-3.0.0\\AllOfficesOpenCaseByAgeBarChartCSVSource.jasper", param, dataSource );
        System.out.println(".jasper file obtained...");
		//Submit Chart as a PDF document
        OutputStream output = new FileOutputStream(new File("C:\\Users\\Administrator\\JasperReportFiles\\JasperReportsCSVBarChart.pdf"));
        //Prepare file to be run and shown in user Browser
		JasperExportManager.exportReportToPdfStream(jasperPrint, output);
		System.out.println("Output establised...");
		System.out.println("launching to browser...");
		Runtime run = Runtime.getRuntime();
		//Grab the file location, then load to Browser
		run.exec("explorer.exe  " + FILE_PATH+OUTFILE_NAME);//FILE_PATH+OUTFILE_NAME

		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	//get data from dataSource
	private static JRCsvDataSource getDataSource() throws JRException, IOException{
	    //JRLoader.getLocationInputStream(
	    JRCsvDataSource ds = new JRCsvDataSource(new File("c:\\temp\\CSV_DATA\\USER_DETAILS_DATA.csv"));
	    ds.setRecordDelimiter("\n");
	    ds.setUseFirstRowAsHeader(true);
	    ds.setFieldDelimiter(',');
	    return ds;
	}
	
	
	
}




//...
//...
//...
//...
//...


then in your JavaBean, add the following for a button to be called from Xpages


	
	
	public void doRunJasperReportsCSVBarCharts() throws NotesException{
		  // set up the loggers
	      Log log = LogFactory.getLog(RunJasperReportsWithCSVDataSourceBean.class);
	      Log debug = LogFactory.getLog("DEBUG");
	      
	      // tell what they are
	      System.out.println("log is a " + log.getClass().getName());
	      System.out.println("debug is a " + debug.getClass().getName());
	      
	      
	      log.info("an info message");
	      debug.debug("a debug message");

	      log.info("another info message");
	      debug.debug("another debug message");
		
		//get the current database being used
		Database database= (Database) FacesContext.getCurrentInstance()
		.getApplication().getVariableResolver()
		.resolveVariable(FacesContext.getCurrentInstance(), "database");
		System.out.println("Database Obtained..." + database);
		
		
		//Run the agent after all items have been submitted to page
		database.getAgent("JasperReportsCSVDataSourceBarChartJavaAgent").runOnServer();
	}
	





Use this XSnippet to load a JasperReports PDF file in your Browser/Explorer Window...  the PDF file/Chart contains data obtained from a CSV DataSource...  See JasperReports XML file and instructions momentarily...

Java
Köll S Cherizard
December 15, 2013 6:37 PM
Rating
29

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



No comments yetLogin first to comment...