Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
THRUST SERVICES & TOOLS
CLOUD EDITIONS
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Error using ReportEventAdapter
chocba
I wrote a ReportEvent Adapter class, copied below, to add a table to report at run time. In Eclipse BIRT IDE, I created a blank report and a datasource source as 'datasource'. I updated the event handler property with the classname. I'm getting the following error when I try to preview the report. I wonder what could be the reason it is not able to find the columns. Please help me. Thanks.
Invalid bound column name: FIRSTNAME
Invalid bound column name: PHONE
import java.util.ArrayList;
import org.eclipse.birt.report.engine.api.script.IReportContext;
import org.eclipse.birt.report.engine.api.script.eventadapter.ReportEventAdapter;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.TableHandle;
public class DynamicReportEventAdapter extends ReportEventAdapter {
ReportDesignHandle designHandle = null;
ElementFactory designFactory = null;
public void initialize(IReportContext reportContext){
System.out.println("In Initialize event");
designFactory = reportContext.getDesignHandle().getElementFactory();
//DataSetHandle handle = reportContext.getDesignHandle().findDataSet("dataset");
designHandle = reportContext.getDesignHandle();
ArrayList<String> cols = new ArrayList<String>();
cols.add("FIRSTNAME");
cols.add("PHONE");
TableHandle table = designFactory.newTableItem( "table", cols.size());
try{
table.setWidth( "100%" );
table.setDataSet( designHandle.findDataSet( "dataset" ) );
RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );
for( int i=0; i < cols.size(); i++){
LabelHandle label1 = designFactory.newLabel( (String)cols.get(i) );
label1.setText((String)cols.get(i));
CellHandle cell = (CellHandle) tableheader.getCells( ).get( i );
cell.getContent( ).add( label1 );
}
// table detail
RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 );
for( int i=0; i < cols.size(); i++){
CellHandle cell = (CellHandle) tabledetail.getCells( ).get( i );
DataItemHandle data = designFactory.newDataItem(cols.get(i));
data.setResultSetColumn(cols.get(i));
cell.getContent( ).add( data );
}
designHandle.getBody( ).add( table );
} catch(Exception e){
e.printStackTrace();
}
}
}
Find more posts tagged with
Comments
rmurphy
If you look at the output columns of your dataset, do you have FIRSTNAME and PHONE? This is case sensitive.
Rob
chocba
Thanks for the response. Yes, I do have this columns in output in uppercase. I tried rewriting in javascript, but ended up with the same error. I placed the script on initialize event of the report. The javascript is below. The objective is to allow users to pick columns to be included in report. I defined a list type parameter which display all possible columns. In the script I get the metadata of dataset to compare add chosen columns to table. The report ran and header is created successfully, but fail to create the detail section. I've marked the code below with "//Failing here" to indicate where it is failing.
//get a reference to the ElementFactory
elementFactory = reportContext.getReportRunnable().designHandle.getElementFactory();
paramvalue =params["displaycols"].toString();
datasetcols = reportContext.getReportRunnable().getDesignInstance().getDataSet("table1").getCachedResultSetColumns()
datasetcolsize = datasetcols.size();
itr = datasetcols.iterator();
colsize=0;
while (itr.hasNext())
{
col = itr.next();
if (paramvalue.indexOf(col.getName(),0) >=0){
colsize=colsize+1;
}
}
dynamicTable = elementFactory.newTableItem("myNewTable",colsize,1,1,1);
dynamicTable.setWidth("100%");
dynamicTable.setDataSet(reportContext.getReportRunnable().designHandle.findDataSet( "table1" ) );
//Create header
titleRow = dynamicTable.getHeader().get(0);
itr1 = datasetcols.iterator();
i =0;
while (itr1.hasNext())
{
col1 =itr1.next();
if (paramvalue.indexOf(col1.getName(),0) >=0) {
title = titleRow.getCells().get(i);
label = elementFactory.newLabel(col1.getName());
label.setText(col1.getName());
//bind label
title.getContent().add(label);
i = i+1;
}
}
//create detail section
itr2 = datasetcols.iterator();
colindex=0;
detailRow = dynamicTable.getDetail().get(0);
while (itr2.hasNext())
{
col2 = itr2.next();
if (paramvalue.indexOf(col2.getName(),0) >=0) {
datacell = detailRow.getCells().get(colindex);
data = elementFactory.newDataItem(col2.getName());
data.setResultSetColumn(col2.getName());
datacell.getContent( ).add( data); //Failing here
colindex= colindex+1;
}
}
reportContext.getReportRunnable().designHandle.findElement("myNewTable");
chocba
I've attached the rpt design file to this thread. Thanks.
rmurphy
First, your script really should be in the beforeFactory method. Initialize can be called twice depending on the implementation of the engine in the application (i.e. once for generation, and once for presentation).<br />
<br />
The script does not create the necessary data bindings on the table to the dataset, thus the invalid bound column name message.<br />
<br />
The script to create the binding in your script would look something like:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
cs1 = StructureFactory.createComputedColumn( );
cs1.setName(""" + col2.getName() + """);
cs1.setExpression( "dataSetRow["" + col2.getName() + ""]");
computedSet.addItem( cs1 );
</pre>
<br />
Then when you add the data to the table, you will have code that looks something similar to:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
datacell = detailRow.getCells().get(colindex);
data = elementFactory.newDataItem(col2.getName());
data.setResultSetColumn(""" + col2.getName() + """);
datacell.getContent( ).add( data);
colindex= colindex+1;
</pre>
<br />
There is a post in the DevShare that does pretty much exactly what you are trying to accomplish. <br />
<a class='bbc_url' href='
http://www.birt-exchange.org/devshare/designing-birt-reports/716-dynamic-content-creation-in-birt-2-3-2/'>Dynamic
Content Creation in BIRT 2.3.2 - Designs & Code - BIRT Exchange</a><br />
<br />
Rob
Happy
I have another example that modifies an existing table at runtime using java event handlers.<br />
<br />
<a class='bbc_url' href='
http://www.birt-exchange.org/devshare/interactive-reporting/1062-ad-hoc-database-browser-report/#description'>Ad-Hoc
Database Browser Report - Tutorials - BIRT Exchange</a><br />
<br />
It borrowed from the article in the previous post and extended it's functionality
chocba
Rob,
Thanks. That works.