Thursday, January 27, 2011

BIRT: Dynamically Adding a Data Set to a Report

So a co-worker asked me today to help him by dynamically adding a data set to a report. He ended up going a slightly different route using the StructureElementFactory class, but this is what I came up with using the reports existing ElementFactory, and dynamically adding in the parameters and query text. This example can be expanded upon of course to set this up in whatever way you need, in coordination with using the DEAPI to dynamically add a table for a more dynamic report. It will even save off the report design at the end for future reference. I used the beforeFactory event, but this will work just as well in the Initialize event as well.


public class ReportEventHandler extends ReportEventAdapter {

@Override
public void beforeFactory(IReportDesign report, IReportContext reportContext) {
super.beforeFactory(report, reportContext);

try {
ReportDesignHandle reportDesign = reportContext.getDesignHandle();
OdaDataSetHandle dataSet = reportDesign.getElementFactory().newOdaDataSet("Data Set", "org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet");

dataSet.setDataSource("Data Source");
dataSet.setQueryText("select employeenumber from employees where employeenumber = ?");

OdaDataSetParameter param = new OdaDataSetParameter();

param.setName("param_1");
param.setParamName("empNo");
param.setNativeName("");
param.setDataType("integer");
param.setNativeDataType(4);
param.setPosition(1);
param.setIsInput(true);
param.setIsOutput(false);

dataSet.getPropertyHandle(DataSetHandle.PARAMETERS_PROP).addItem(param);

OdaResultSetColumn result = new OdaResultSetColumn();
result.setColumnName("EMPLOYEENUMBER");
result.setNativeName("EMPLOYEENUMBER");
result.setDataType("integer");
result.setPosition(1);
result.setNativeDataType(4);
dataSet.getPropertyHandle(DataSetHandle.RESULT_SET_PROP).addItem(result);

ColumnHint resultHint = new ColumnHint();
resultHint.setProperty(ColumnHint.COLUMN_NAME_MEMBER, "EMPLOYEENUMBER");
resultHint.setProperty(ColumnHint.COLUMN_NAME_MEMBER, "integer");

dataSet.getPropertyHandle(DataSet.COLUMN_HINTS_PROP).addItem(resultHint);

//this works
reportDesign.getDataSets().add(dataSet);

//and so does this
//reportDesign.getSlot(ReportDesignHandle.DATA_SET_SLOT).add(dataSet);

//just saving for debug purposes, you can ignore this
reportDesign.saveAs("C:/TEMP/MyTestDesign.rptdesign");
} catch (ContentException e) {
e.printStackTrace();
} catch (NameException e) {
e.printStackTrace();
} catch (SemanticException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

1 comment:

Anonymous said...

how do I find out the possible set of parameter values for these methods?

param.setDataType"integer"); param.setNativeDataType(4);

how to I found out how to bind to a varchar for bit data column in the database ?

Regards
AFarrell@curamsoftware.com