package zemplen.report;

import java.util.Map;
import java.util.logging.Level;

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IGetParameterDefinitionTask;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
import org.eclipse.birt.report.engine.api.RenderOption;

public class ReportGen {

	private static IReportEngine engine;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			generate_eredmeny();
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void generate(String designFileName, String exportFileName, boolean embeddable) throws EngineException {
		EngineConfig config = null;
		IReportEngine engine = null;
		try {
			//Configure the Engine and start the Platform
			config = new EngineConfig();
			config.setEngineHome("/home/espakm/birt-runtime-2_2_0/ReportEngine");

			//set log config using ( null, Level ) if you do not want a log file
			config.setLogConfig("/home/espakm/workspace/zm1/logs", Level.FINE);

			Platform.startup(config);
			IReportEngineFactory factory = (IReportEngineFactory) Platform.
			createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);

			engine = factory.createReportEngine(config);
			engine.changeLogLevel(Level.WARNING);
		}
		catch (Exception exc) {
			exc.printStackTrace();
		}

		IReportRunnable design = null;

		//Open the report design
		design = engine.openReportDesign(designFileName); 

		//Create task to run and render the report,
		IRunAndRenderTask task = engine.createRunAndRenderTask(design); 
		task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, ReportGen.class.getClassLoader());

		RenderOption options = null;
		
		if (exportFileName.endsWith(".html")) {
			//Set rendering options - such as file or stream output, 
			//output format, whether it is embeddable, etc
			options = new HTMLRenderOption();
			options.setBaseURL("http://localhost/");

			//Remove HTML and Body tags
			((HTMLRenderOption) options).setEmbeddable(embeddable);
			//Set output format
			options.setOutputFormat("html");
		}
		else if (exportFileName.endsWith(".pdf")) {
			options = new PDFRenderOption();
			options.setOutputFormat("pdf");
		}
		
		//Set output location
		options.setOutputFileName(exportFileName);

		task.setRenderOption(options);

		//run the report and destroy the engine
		//Note - If the program stays resident do not shutdown the Platform or the Engine
		task.run();
		task.close();

		engine.shutdown();
		Platform.shutdown();
	}
	
	public static void generate_eredmeny() throws EngineException {
		startEngine();

		IReportRunnable design;
		IGetParameterDefinitionTask paramTask;

		design = engine.openReportDesign("/home/espakm/workspace/zm1/src/zemplen/report/kategoriankent.rptdesign"); 

		paramTask = engine.createGetParameterDefinitionTask(design);
		paramTask.setParameterValue("rp_verseny_datum", "2005-07-24");
		paramTask.setParameterValue("rp_verseny_datum_cim", "2005. 07. 24.");
		paramTask.setParameterValue("rp_tav", "rövidtáv");
		runReport(design, paramTask.getParameterValues(), "2005/rovidtav-kategoriankent.html", true);
		paramTask.setParameterValue("rp_tav", "középtáv");
		runReport(design, paramTask.getParameterValues(), "2005/kozeptav-kategoriankent.html", true);
		paramTask.setParameterValue("rp_tav", "hosszútáv");
		runReport(design, paramTask.getParameterValues(), "2005/hosszutav-kategoriankent.html", true);
		paramTask.setParameterValue("rp_verseny_datum", "2006-07-23");
		paramTask.setParameterValue("rp_verseny_datum_cim", "2006. 07. 23.");
		paramTask.setParameterValue("rp_tav", "rövidtáv");
		runReport(design, paramTask.getParameterValues(), "2006/rovidtav-kategoriankent.html", true);
		paramTask.setParameterValue("rp_tav", "középtáv");
		runReport(design, paramTask.getParameterValues(), "2006/kozeptav-kategoriankent.html", true);
		paramTask.setParameterValue("rp_tav", "hosszútáv");
		runReport(design, paramTask.getParameterValues(), "2006/hosszutav-kategoriankent.html", true);
		paramTask.setParameterValue("rp_verseny_datum", "2007-07-22");
		paramTask.setParameterValue("rp_verseny_datum_cim", "2007. 07. 22.");
		paramTask.setParameterValue("rp_tav", "kismaraton");
		runReport(design, paramTask.getParameterValues(), "2007/kismaraton-kategoriankent.html", true);
		paramTask.setParameterValue("rp_tav", "félmaraton");
		runReport(design, paramTask.getParameterValues(), "2007/felmaraton-kategoriankent.html", true);
		paramTask.setParameterValue("rp_tav", "maraton");
		runReport(design, paramTask.getParameterValues(), "2007/maraton-kategoriankent.html", true);

		design = engine.openReportDesign("/home/espakm/workspace/zm1/src/zemplen/report/eredmenyek2.rptdesign"); 

		paramTask = engine.createGetParameterDefinitionTask(design);
		paramTask.setParameterValue("rp_verseny_datum", "2005-07-24");
		paramTask.setParameterValue("rp_verseny_datum_cim", "2005. 07. 24.");
		paramTask.setParameterValue("rp_tav", "rövidtáv");
		runReport(design, paramTask.getParameterValues(), "2005/rovidtav-abszolut.html", true);
		paramTask.setParameterValue("rp_tav", "középtáv");
		runReport(design, paramTask.getParameterValues(), "2005/kozeptav-abszolut.html", true);
		paramTask.setParameterValue("rp_tav", "hosszútáv");
		runReport(design, paramTask.getParameterValues(), "2005/hosszutav-abszolut.html", true);
		paramTask.setParameterValue("rp_verseny_datum", "2006-07-23");
		paramTask.setParameterValue("rp_verseny_datum_cim", "2006. 07. 23.");
		paramTask.setParameterValue("rp_tav", "rövidtáv");
		runReport(design, paramTask.getParameterValues(), "2006/rovidtav-abszolut.html", true);
		paramTask.setParameterValue("rp_tav", "középtáv");
		runReport(design, paramTask.getParameterValues(), "2006/kozeptav-abszolut.html", true);
		paramTask.setParameterValue("rp_tav", "hosszútáv");
		runReport(design, paramTask.getParameterValues(), "2006/hosszutav-abszolut.html", true);
		paramTask.setParameterValue("rp_verseny_datum", "2007-07-22");
		paramTask.setParameterValue("rp_verseny_datum_cim", "2007. 07. 22.");
		paramTask.setParameterValue("rp_tav", "kismaraton");
		runReport(design, paramTask.getParameterValues(), "2007/kismaraton-abszolut.html", true);
		paramTask.setParameterValue("rp_tav", "félmaraton");
		runReport(design, paramTask.getParameterValues(), "2007/felmaraton-abszolut.html", true);
		paramTask.setParameterValue("rp_tav", "maraton");
		runReport(design, paramTask.getParameterValues(), "2007/maraton-abszolut.html", true);

		stopEngine();
	}

	private static void stopEngine() {
		engine.shutdown();
		Platform.shutdown();
	}

	private static void runReport(IReportRunnable design, Map parameterValues, String exportFileName, boolean embeddable) throws EngineException {
		//Create task to run and render the report,
		IRunAndRenderTask task = engine.createRunAndRenderTask(design); 
		task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, ReportGen.class.getClassLoader());

		task.setParameterValues(parameterValues);
		
		RenderOption options = null;
		
		if (exportFileName.endsWith(".html")) {
			//Set rendering options - such as file or stream output, 
			//output format, whether it is embeddable, etc
			options = new HTMLRenderOption();
			options.setBaseURL("http://www.zemplenmaraton.hu/");

			//Remove HTML and Body tags
			((HTMLRenderOption) options).setEmbeddable(embeddable);
			//Set output format
			options.setOutputFormat("html");
		}
		else if (exportFileName.endsWith(".pdf")) {
			options = new PDFRenderOption();
			options.setOutputFormat("pdf");
		}
		
		//Set output location
		options.setOutputFileName(exportFileName);

		task.setRenderOption(options);

		//run the report and destroy the engine
		//Note - If the program stays resident do not shutdown the Platform or the Engine
		task.run();
		task.close();
	}

	private static void startEngine() {
		EngineConfig config = null;
		engine = null;
		try {
			//Configure the Engine and start the Platform
			config = new EngineConfig();
			config.setEngineHome("/home/espakm/birt-runtime-2_2_0/ReportEngine");

			//set log config using ( null, Level ) if you do not want a log file
			config.setLogConfig("/home/espakm/workspace/zm1/logs", Level.FINE);

			Platform.startup(config);
			IReportEngineFactory factory = (IReportEngineFactory) Platform.
			createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);

			engine = factory.createReportEngine(config);
			engine.changeLogLevel(Level.WARNING);
		}
		catch (Exception exc) {
			exc.printStackTrace();
		}
	}
	
	
}
