本文主要是介绍利用Java写birt的eventhandler,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
birt的各种对象,包括数据源(datasource)、数据集(dataset)及各种报表元素(report item)都可以添加eventhandler对象,实现对这些对象的事件的响应。eventhandler实现的方式有两种:JavaScript语言和Java语言。由于Java的可调试性,开发起来相对方便一些,我这里利用Java开发了dataset的一个eventhandler:
mport java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.birt.report.engine.api.script.IUpdatableDataSetRow;
import org.eclipse.birt.report.engine.api.script.ScriptException;
import org.eclipse.birt.report.engine.api.script.eventadapter.ScriptedDataSetEventAdapter;
import org.eclipse.birt.report.engine.api.script.instance.IDataSetInstance;
import com.tkxing.htmlparser.buss.report.model.BaseReportModel;
import com.tkxing.htmlparser.buss.report.model.SingleStockDataReportModel;
public class ScriptedDataSetEventHandler extends ScriptedDataSetEventAdapter {
Log log = LogFactory.getLog(ScriptedDataSetEventHandler.class);
private int num = 0;
private int total = 0;
private List dataList;
@Override
public void open(IDataSetInstance dataSet) {
// TODO Auto-generated method stub
super.open(dataSet);
BaseReportModel reportModel = new SingleStockDataReportModel();
dataList = (List) reportModel.getModel();
this.total = dataList.size();
}
@Override
public boolean fetch(IDataSetInstance dataSet, IUpdatableDataSetRow row) {
// TODO Auto-generated method stub
try {
if (num >= total) {
return false;
}
String[] dataRow = (String[]) this.dataList.get(num);
for (int i = 1; i <= dataRow.length; i++) {
row.setColumnValue(i, dataRow[i - 1]);
System.out.println(i + " = " + dataRow[i - 1]);
}
num++;
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
// return super.fetch(dataSet, row);
}
}
数据模式reportModel代码如下:
import java.util.ArrayList;
import java.util.List;
public class SingleStockDataReportModel extends BaseReportModel {
private List dataList;
public SingleStockDataReportModel() {
// TODO Auto-generated constructor stub
this.dataList = new ArrayList();
for(int i=0;i<10;i++)
{
String []rowData = new String[3];
for(int j=0;j<rowData.length;j++)
{
rowData[j] = new String(new Integer(i*10+j).toString());
}
this.dataList.add(rowData);
}
}
public List getDataList() {
/*
this.dataList = new ArrayList();
for(int i=0;i<10;i++)
{
String []rowData = new String[3];
for(int j=0;j<rowData.length;j++)
{
rowData[j] = new String(new Integer(i*10+j).toString());
}
dataList.add(rowData);
}
*/
return (dataList);
}
public void setDataList(List dataList) {
this.dataList = dataList;
}
@Override
public Object getModel() {
// TODO Auto-generated method stub
return this.dataList;
}
}
利用birt的ReportEngine输出报表或者通过URL访问报表,在相应的位置设置断点,就可以实现对eventhandler的调试了。
测试的report.rptdesign如下:
<?xml version="1.0" encoding="UTF-8"?>
<report xmlns="http://www.eclipse.org/birt/2005/design" version="3.2.15" id="1">
<data-sources>
<script-data-source name="ScriptedDataSource" id="5"/>
</data-sources>
<data-sets>
<script-data-set name="ScriptedDataSet" id="6">
<property name="eventHandlerClass">com.tkxing.htmlparser.presentation.report.ScriptedDataSetEventHandler</property>
<list-property name="resultSetHints">
<structure>
<property name="position">1</property>
<property name="name">rs1</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">2</property>
<property name="name">rs2</property>
<property name="dataType">string</property>
</structure>
<structure>
<property name="position">3</property>
<property name="name">rs3</property>
<property name="dataType">string</property>
</structure>
</list-property>
<list-property name="columnHints">
<structure>
<property name="columnName">rs1</property>
</structure>
<structure>
<property name="columnName">rs2</property>
</structure>
<structure>
<property name="columnName">rs3</property>
</structure>
</list-property>
<property name="dataSource">ScriptedDataSource</property>
</script-data-set>
</data-sets>
<styles>
<style name="crosstab" id="2">
<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-cell" id="3">
<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="Page Master" id="4"/>
</page-setup>
<body>
<table name="table" id="7">
<property name="width">100%</property>
<property name="dataSet">ScriptedDataSet</property>
<list-property name="boundDataColumns">
<structure>
<property name="name">rs1</property>
<expression name="expression">dataSetRow["rs1"]</expression>
</structure>
<structure>
<property name="name">rs2</property>
<expression name="expression">dataSetRow["rs2"]</expression>
</structure>
<structure>
<property name="name">rs3</property>
<expression name="expression">dataSetRow["rs3"]</expression>
</structure>
</list-property>
<column id="26"/>
<column id="27"/>
<column id="28"/>
<header>
<row id="8">
<cell id="9">
<label name="rs1" id="10">
<text-property name="text">rs1</text-property>
</label>
</cell>
<cell id="11">
<label name="rs2" id="12">
<text-property name="text">rs2</text-property>
</label>
</cell>
<cell id="13">
<label name="rs3" id="14">
<text-property name="text">rs3</text-property>
</label>
</cell>
</row>
</header>
<detail>
<row id="15">
<cell id="16">
<data name="data_rs1" id="17">
<property name="resultSetColumn">rs1</property>
</data>
</cell>
<cell id="18">
<data name="data_rs2" id="19">
<property name="resultSetColumn">rs2</property>
</data>
</cell>
<cell id="20">
<data name="data_rs3" id="21">
<property name="resultSetColumn">rs3</property>
</data>
</cell>
</row>
</detail>
<footer>
<row id="22">
<cell id="23"/>
<cell id="24"/>
<cell id="25"/>
</row>
</footer>
</table>
</body>
</report>
测试的结果如图:
rs1 rs2 rs3
0 1 2
10 11 12
20 21 22
这篇关于利用Java写birt的eventhandler的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!