Monday, March 05, 2012

BIRT: Running a Multi-Select Parameter Report from Java

Below is an example of how to call a multi-select parameter report from Java using the BIRT Report Engine API.


import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineException;
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.impl.ReportEngine;

public class MultiParamCall {
public static void main(String[] args) {
try {
String designFIle = "C:\\Users\\jwward\\workspace\\Examples\\MultiValue.rptdesign";

//create engine config, start engine and platform
EngineConfig config = new EngineConfig();
Platform.startup();
ReportEngine engine = new ReportEngine(config);

//open the report design and create a new run task
IReportRunnable reportDesign = engine.openReportDesign(designFIle);
IRunAndRenderTask runTask = engine.createRunAndRenderTask(reportDesign);

//the multi-select values to use. This just needs to be Object[].
String[] values = {"V1", "V2", "V3"};
runTask.setParameterValue("ListParam", values);

//set the render outputs
PDFRenderOption renderOption = new PDFRenderOption();
renderOption.setOutputFileName("C:/TEMP/output.pdf");
renderOption.setOutputFormat("PDF");
runTask.setRenderOption(renderOption);

//run report, close the task, and destroy the engine.
runTask.run();
runTask.close();
engine.destroy();
} catch (EngineException e) {
e.printStackTrace();
} catch (BirtException e) {
e.printStackTrace();
}
}
}


The XML for the report design is below.

<?xml version="1.0" encoding="UTF-8"?>
<report xmlns="http://www.eclipse.org/birt/2005/design" version="3.2.22" id="1">
<property name="createdBy">Eclipse BIRT Designer Version 3.7.1.v20110905 Build &lt;3.7.1.v20110905-1820></property>
<property name="units">in</property>
<property name="iconFile">/templates/blank_report.gif</property>
<property name="bidiLayoutOrientation">ltr</property>
<property name="imageDPI">96</property>
<parameters>
<scalar-parameter name="ListParam" id="7">
<property name="valueType">static</property>
<property name="dataType">string</property>
<property name="distinct">true</property>
<list-property name="selectionList">
<structure>
<property name="value">V1</property>
</structure>
<structure>
<property name="value">V2</property>
</structure>
<structure>
<property name="value">V3</property>
</structure>
</list-property>
<property name="paramType">multi-value</property>
<property name="controlType">list-box</property>
<property name="mustMatch">true</property>
<property name="fixedOrder">true</property>
<structure name="format">
<property name="category">Unformatted</property>
</structure>
</scalar-parameter>
</parameters>
<styles>
<style name="report" id="4">
<property name="fontFamily">sans-serif</property>
<property name="fontSize">10pt</property>
</style>
<style name="crosstab-cell" id="5">
<property name="borderBottomColor">#CCCCCC</property>
<property name="borderBottomStyle">solid</property>
<property name="borderBottomWidth">1pt</property>
<property name="borderLeftColor">#CCCCCC</property>
<property name="borderLeftStyle">solid</property>
<property name="borderLeftWidth">1pt</property>
<property name="borderRightColor">#CCCCCC</property>
<property name="borderRightStyle">solid</property>
<property name="borderRightWidth">1pt</property>
<property name="borderTopColor">#CCCCCC</property>
<property name="borderTopStyle">solid</property>
<property name="borderTopWidth">1pt</property>
</style>
<style name="crosstab" id="6">
<property name="borderBottomColor">#CCCCCC</property>
<property name="borderBottomStyle">solid</property>
<property name="borderBottomWidth">1pt</property>
<property name="borderLeftColor">#CCCCCC</property>
<property name="borderLeftStyle">solid</property>
<property name="borderLeftWidth">1pt</property>
<property name="borderRightColor">#CCCCCC</property>
<property name="borderRightStyle">solid</property>
<property name="borderRightWidth">1pt</property>
<property name="borderTopColor">#CCCCCC</property>
<property name="borderTopStyle">solid</property>
<property name="borderTopWidth">1pt</property>
</style>
</styles>
<page-setup>
<simple-master-page name="Simple MasterPage" id="2">
<page-footer>
<text id="3">
<property name="contentType">html</property>
<text-property name="content"><![CDATA[<value-of>new Date()</value-of>]]></text-property>
</text>
</page-footer>
</simple-master-page>
</page-setup>
<body>
<data id="8">
<list-property name="boundDataColumns">
<structure>
<property name="name">ListParam</property>
<expression name="expression" type="javascript">params["ListParam"].toString()</expression>
<property name="dataType">string</property>
<property name="allowExport">true</property>
</structure>
</list-property>
<property name="resultSetColumn">ListParam</property>
</data>
</body>
</report>