Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » how to access dynamically defined data-set columns
how to access dynamically defined data-set columns [message #492448] Tue, 20 October 2009 13:27 Go to next message
Csaba  is currently offline Csaba Friend
Messages: 24
Registered: July 2009
Junior Member
Hello Users,

How can I reference a data column in a data binding from a dynamic scripted data-set?

By dynamic scripted data-set I mean a data-set element without any statically defined columns but with a scripted data-set event handler class defining the dynamic columns like this:
public boolean describe(IDataSetInstance dataSet, IScriptedDataSetMetaData metaData) {
metaData.addColumn("A_Column", String.class);
return true;
}

row[1] /row[0] is the RowNo, I guess/ or row["A_Column"] does not work in a data binding in a report design file, it throws an exception.

Many thanks!
Csaba
Re: how to access dynamically defined data-set columns [message #492492 is a reply to message #492448] Tue, 20 October 2009 15:27 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Csaba,

Create a new data binding column say col1 that references
dataSetRow["A_Column"] or dataSetRow[3]. See attached example. Be sure
to look at the Binding on the table.


Jason

//Scripted Data Set Event Handler Class MyScriptedDataSetMeta.java

package my.event.handlers;

import org.eclipse.birt.report.engine.api.script.IScriptedDataSetMe taData;
import org.eclipse.birt.report.engine.api.script.IUpdatableDataSetR ow;
import
org.eclipse.birt.report.engine.api.script.eventadapter.Scrip tedDataSetEventAdapter;
import org.eclipse.birt.report.engine.api.script.instance.IDataSetI nstance;

public class MyScriptedDataSetMeta extends ScriptedDataSetEventAdapter {

private Integer cnt=0;
private Integer cntT=3;

@Override
public boolean fetch(IDataSetInstance dataSet, IUpdatableDataSetRow row) {

if( cnt < cntT){
try{
row.setColumnValue("col1", cnt);
row.setColumnValue("col2", "hello");
row.setColumnValue("A_Column", "jjj");

cnt++;
return true;
}catch (Exception e){

}
}
return false;
}

@Override
public boolean describe(IDataSetInstance dataSet,
IScriptedDataSetMetaData metaData) {
// TODO Auto-generated method stub
metaData.addColumn("col1", Integer.class);
metaData.addColumn("col2", String.class);
metaData.addColumn("A_Column", String.class);

return true;
//return super.describe(dataSet, metaData);
}

@Override
public void open(IDataSetInstance dataSet) {
// TODO Auto-generated method stub
super.open(dataSet);
}
}


//Report

<?xml version="1.0" encoding="UTF-8"?>
<report xmlns="http://www.eclipse.org/birt/2005/design" version="3.2.20"
id="1">
<property name="createdBy">Eclipse BIRT Designer Version
2.5.1.v20090903 Build &lt;2.5.1.v20090917-1447></property>
<property name="units">in</property>
<property name="iconFile">/templates/blank_report.gif</property>
<property name="bidiLayoutOrientation">ltr</property>
<data-sources>
<script-data-source name="Data Source" id="7"/>
</data-sources>
<data-sets>
<script-data-set name="Data Set" id="8">
<property
name="eventHandlerClass">my.event.handlers.MyScriptedDataSetMeta </property>
<list-property name="resultSetHints">
<structure>
<property name="position">1</property>
<property name="name">col1</property>
<property name="dataType">integer</property>
</structure>
<structure>
<property name="position">2</property>
<property name="name">col2</property>
<property name="dataType">string</property>
</structure>
</list-property>
<list-property name="columnHints">
<structure>
<property name="columnName">col1</property>
</structure>
<structure>
<property name="columnName">col2</property>
</structure>
</list-property>
<structure name="cachedMetaData">
<list-property name="resultSet">
<structure>
<property name="position">1</property>
<property name="name">col1</property>
<property name="dataType">integer</property>
</structure>
<structure>
<property name="position">2</property>
<property name="name">col2</property>
<property name="dataType">string</property>
</structure>
</list-property>
</structure>
<property name="dataSource">Data Source</property>
</script-data-set>
</data-sets>
<styles>
<style name="report" id="4">
<property name="fontFamily">sans-serif</property>
<property name="fontSize">10pt</property>
</style>
<style name="crosstab" 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-cell" 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>
<table id="9">
<property name="dataSet">Data Set</property>
<list-property name="boundDataColumns">
<structure>
<property name="name">col1</property>
<text-property name="displayName">col1</text-property>
<expression
name="expression">dataSetRow["col1"]</expression>
<property name="dataType">integer</property>
</structure>
<structure>
<property name="name">col2</property>
<text-property name="displayName">col2</text-property>
<expression
name="expression">dataSetRow["col2"]</expression>
<property name="dataType">string</property>
</structure>
<structure>
<property name="name">col3</property>
<expression name="expression"
type="javascript">dataSetRow["A_Column"]</expression>
<property name="dataType">string</property>
</structure>
<structure>
<property name="name">Column Binding</property>
<expression name="expression"
type="javascript">dataSetRow[3]</expression>
<property name="dataType">string</property>
</structure>
</list-property>
<column id="34"/>
<column id="23"/>
<column id="24"/>
<column id="28"/>
<header>
<row id="10">
<cell id="31"/>
<cell id="11">
<label id="12">
<text-property name="text">col1</text-property>
</label>
</cell>
<cell id="13">
<label id="14">
<text-property name="text">col2</text-property>
</label>
</cell>
<cell id="25">
<label id="30">
<text-property
name="text">A_Column</text-property>
</label>
</cell>
</row>
</header>
<detail>
<row id="15">
<cell id="32">
<data id="35">
<property name="resultSetColumn">Column
Binding</property>
</data>
</cell>
<cell id="16">
<data id="17">
<property
name="resultSetColumn">col1</property>
</data>
</cell>
<cell id="18">
<data id="19">
<property
name="resultSetColumn">col2</property>
</data>
</cell>
<cell id="26">
<data id="29">
<property
name="resultSetColumn">col3</property>
</data>
</cell>
</row>
</detail>
<footer>
<row id="20">
<cell id="33"/>
<cell id="21"/>
<cell id="22"/>
<cell id="27"/>
</row>
</footer>
</table>
</body>
</report>


Csaba wrote:
> Hello Users,
>
> How can I reference a data column in a data binding from a dynamic
> scripted data-set?
>
> By dynamic scripted data-set I mean a data-set element without any
> statically defined columns but with a scripted data-set event handler
> class defining the dynamic columns like this:
> public boolean describe(IDataSetInstance dataSet,
> IScriptedDataSetMetaData metaData) {
> metaData.addColumn("A_Column", String.class);
> return true;
> }
>
> row[1] /row[0] is the RowNo, I guess/ or row["A_Column"] does not work
> in a data binding in a report design file, it throws an exception.
>
> Many thanks!
> Csaba
Re: how to access dynamically defined data-set columns [message #492645 is a reply to message #492448] Wed, 21 October 2009 08:51 Go to previous messageGo to next message
Csaba  is currently offline Csaba Friend
Messages: 24
Registered: July 2009
Junior Member
Jason,

thanks a lot!

It was my mistake, I used row instead of dataSetRow!

Now it works fine!

Csaba
Re: how to access dynamically defined data-set columns [message #531335 is a reply to message #492448] Tue, 04 May 2010 14:49 Go to previous messageGo to next message
Diego Ocampo is currently offline Diego OcampoFriend
Messages: 2
Registered: May 2010
Junior Member
Hi all,

I have an issue related to dynamic data sets and tables.

I have a dataset with no defined columns, at first. This is because the number of columns may vary, and there is no static columns in it.

Therefore, i have an event handler as you presented here, where i create the columns:

	public boolean describe(IDataSetInstance paramIDataSetInstance,
			IScriptedDataSetMetaData paramIScriptedDataSetMetaData) throws ScriptException {
		paramIScriptedDataSetMetaData.addColumn("id", String.class);
		paramIScriptedDataSetMetaData.addColumn("name", String.class);
		paramIScriptedDataSetMetaData.addColumn("description", String.class);
		return true;
	}


Then , i fill up the data set, with the fetch method, like this:

public boolean fetch(IDataSetInstance paramIDataSetInstance, IUpdatableDataSetRow paramIUpdatableDataSetRow)
			throws ScriptException {
		paramIUpdatableDataSetRow.setColumnValue("id", "test id");
		paramIUpdatableDataSetRow.setColumnValue("name", "test name");
		paramIUpdatableDataSetRow.setColumnValue("description", "test description");
		return false;
	}


I debugged it and seems to work fine, no problems during execution.
Now, my problem is, once this data set if filled, display all this data in a table, on the report.
I tried adding an empty table and setting "Edit Data Binding/ Select Data Binding/Data Set/ myDataSet" on the table's contextual menu, but nothing is shown on the report.
I also have the problem that the size of the table isnt predefined either, it is as the data set, dynamic.

The business idea behind all this is having a common report that could display listings of different objects like users, barcodes, etc.
The EventHandler would receive an array with the columns, and a list or something with the detail data, and with those parameters it must create the dataset (already done) and display it on the report (not done).

I hope you can help, thanks in advance
Diego Ocampo
Re: how to access dynamically defined data-set columns [message #531426 is a reply to message #531335] Tue, 04 May 2010 20:27 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Diego,

You could hard code one column that is used in all tables and bind that
to the dataset in the design and then use the method you have below to
add extra columns and then in the prepare of the table add columns to
the table and the bindings. For example look at this event handler:
package my.event.handlers;

import org.eclipse.birt.report.engine.api.script.IScriptedDataSetMe taData;
import org.eclipse.birt.report.engine.api.script.IUpdatableDataSetR ow;
import
org.eclipse.birt.report.engine.api.script.eventadapter.Scrip tedDataSetEventAdapter;
import org.eclipse.birt.report.engine.api.script.instance.IDataSetI nstance;

public class MyScriptedDataSetMeta extends ScriptedDataSetEventAdapter {

private Integer cnt=0;
private Integer cntT=3;

@Override
public boolean fetch(IDataSetInstance dataSet, IUpdatableDataSetRow row) {

if( cnt < cntT){
try{
row.setColumnValue("col1", cnt);
row.setColumnValue("col2", "hello");
row.setColumnValue("A_Column", "jjj");

cnt++;
return true;
}catch (Exception e){

}
}
return false;
}

@Override
public boolean describe(IDataSetInstance dataSet,
IScriptedDataSetMetaData metaData) {
// TODO Auto-generated method stub
metaData.addColumn("col1", Integer.class);
metaData.addColumn("col2", String.class);
metaData.addColumn("A_Column", String.class);

return true;
//return super.describe(dataSet, metaData);
}

}

Next look at this report. Specifically look at the prepare method of
the table.

<?xml version="1.0" encoding="UTF-8"?>
<report xmlns="http://www.eclipse.org/birt/2005/design" version="3.2.21"
id="1">
<property name="createdBy">Eclipse BIRT Designer Version
2.5.2.v20100208 Build &lt;2.5.2.v20100210-0630></property>
<property name="units">in</property>
<property name="iconFile">/templates/blank_report.gif</property>
<property name="bidiLayoutOrientation">ltr</property>
<data-sources>
<script-data-source name="Data Source" id="7"/>
</data-sources>
<data-sets>
<script-data-set name="Data Set" id="8">
<property
name="eventHandlerClass">my.event.handlers.MyScriptedDataSetMeta </property>
<list-property name="resultSetHints">
<structure>
<property name="position">1</property>
<property name="name">col1</property>
<property name="dataType">integer</property>
</structure>
<structure>
<property name="position">2</property>
<property name="name">col2</property>
<property name="dataType">string</property>
</structure>
</list-property>
<list-property name="columnHints">
<structure>
<property name="columnName">col1</property>
</structure>
<structure>
<property name="columnName">col2</property>
</structure>
</list-property>
<structure name="cachedMetaData">
<list-property name="resultSet">
<structure>
<property name="position">1</property>
<property name="name">col1</property>
<property name="dataType">integer</property>
</structure>
<structure>
<property name="position">2</property>
<property name="name">col2</property>
<property name="dataType">string</property>
</structure>
</list-property>
</structure>
<property name="dataSource">Data Source</property>
</script-data-set>
</data-sets>
<styles>
<style name="report" id="4">
<property name="fontFamily">sans-serif</property>
<property name="fontSize">10pt</property>
</style>
<style name="crosstab" 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-cell" 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>
<table name="mytable" id="9">
<property name="dataSet">Data Set</property>
<list-property name="boundDataColumns">
<structure>
<property name="name">col1</property>
<text-property name="displayName">col1</text-property>
<expression
name="expression">dataSetRow["col1"]</expression>
<property name="dataType">integer</property>
</structure>
<structure>
<property name="name">col2</property>
<text-property name="displayName">col2</text-property>
<expression
name="expression">dataSetRow["col2"]</expression>
<property name="dataType">string</property>
</structure>
<structure>
<property name="name">col3</property>
<expression name="expression"
type="javascript">dataSetRow["A_Column"]</expression>
<property name="dataType">string</property>
</structure>
<structure>
<property name="name">Column Binding</property>
<expression name="expression"
type="javascript">dataSetRow[3]</expression>
<property name="dataType">string</property>
</structure>
</list-property>
<method name="onPrepare"><![CDATA[importPackage(
Packages.org.eclipse.birt.report.model.api );
elementFactory = reportContext.getDesignHandle().getElementFactory()
var mytable = reportContext.getDesignHandle().findElement("mytable");
var colbinds = mytable.getColumnBindings( );
var cs1 = StructureFactory.createComputedColumn( );
cs1.setName("tst");
cs1.setExpression( "dataSetRow[\"A_Column\"]" );
colbinds.addItem( cs1 );

//second parameter is before(-1) or after(1)
mytable.insertColumn(2,-1);
//get first detail row
mydetail = mytable.getDetail( ).get( 0 );
//get first column and add detail data item
var tcell = mydetail.getCells( ).get( 1 );
var mydata = elementFactory.newDataItem( null );
mydata.setResultSetColumn( "tst");
tcell.getContent( ).add( mydata );
//get header and add label
var myheader = mytable.getHeader( ).get( 0 );
tcell = myheader.getCells( ).get( 1 );
var mylabel = elementFactory.newLabel( null );
mylabel.setText( "ScriptColumn" );//$NON-NLS-1$
tcell.getContent( ).add( mylabel );

]]></method>
<column id="34"/>
<column id="23"/>
<column id="24"/>
<column id="28"/>
<header>
<row id="10">
<cell id="31"/>
<cell id="11">
<label id="12">
<text-property name="text">col1</text-property>
</label>
</cell>
<cell id="13">
<label id="14">
<text-property name="text">col2</text-property>
</label>
</cell>
<cell id="25">
<label id="30">
<text-property
name="text">A_Column</text-property>
</label>
</cell>
</row>
</header>
<detail>
<row id="15">
<cell id="32">
<data id="35">
<property name="resultSetColumn">Column
Binding</property>
</data>
</cell>
<cell id="16">
<data id="17">
<property
name="resultSetColumn">col1</property>
</data>
</cell>
<cell id="18">
<data id="19">
<property
name="resultSetColumn">col2</property>
</data>
</cell>
<cell id="26">
<data id="29">
<property
name="resultSetColumn">col3</property>
</data>
</cell>
</row>
</detail>
<footer>
<row id="20">
<cell id="33"/>
<cell id="21"/>
<cell id="22"/>
<cell id="27"/>
</row>
</footer>
</table>
</body>
</report>






On 5/4/2010 10:49 AM, Diego Ocampo wrote:
> Hi all,
>
> I have an issue related to dynamic data sets and tables.
>
> I have a dataset with no defined columns, at first. This is because the
> number of columns may vary, and there is no static columns in it.
>
> Therefore, i have an event handler as you presented here, where i create
> the columns:
>
>
> public boolean describe(IDataSetInstance paramIDataSetInstance,
> IScriptedDataSetMetaData paramIScriptedDataSetMetaData) throws
> ScriptException {
> paramIScriptedDataSetMetaData.addColumn("id", String.class);
> paramIScriptedDataSetMetaData.addColumn("name", String.class);
> paramIScriptedDataSetMetaData.addColumn("description", String.class);
> return true;
> }
>
>
> Then , i fill up the data set, with the fetch method, like this:
>
>
> public boolean fetch(IDataSetInstance paramIDataSetInstance,
> IUpdatableDataSetRow paramIUpdatableDataSetRow)
> throws ScriptException {
> paramIUpdatableDataSetRow.setColumnValue("id", "test id");
> paramIUpdatableDataSetRow.setColumnValue("name", "test name");
> paramIUpdatableDataSetRow.setColumnValue("description", "test
> description");
> return false;
> }
>
>
> I debugged it and seems to work fine, no problems during execution. Now,
> my problem is, once this data set if filled, display all this data in a
> table, on the report.
> I tried adding an empty table and setting "Edit Data Binding/ Select
> Data Binding/Data Set/ myDataSet" on the table's contextual menu, but
> nothing is shown on the report.
> I also have the problem that the size of the table isnt predefined
> either, it is as the data set, dynamic.
>
> The business idea behind all this is having a common report that could
> display listings of different objects like users, barcodes, etc.
> The EventHandler would receive an array with the columns, and a list or
> something with the detail data, and with those parameters it must create
> the dataset (already done) and display it on the report (not done).
>
> I hope you can help, thanks in advance
> Diego Ocampo
Re: how to access dynamically defined data-set columns [message #531866 is a reply to message #531426] Thu, 06 May 2010 12:27 Go to previous message
Diego Ocampo is currently offline Diego OcampoFriend
Messages: 2
Registered: May 2010
Junior Member
Thanks Jason,
We used an EventHandler to dynamically modify the Table in the report design, using the DE API, then using another EventHandler, we populate the dataset and thats it

Thanks again,
Diego
Previous Topic:BIRT Servlet Stress Test Problem
Next Topic:chartBuilder example lauches wizard with error
Goto Forum:
  


Current Time: Fri Mar 29 05:31:01 GMT 2024

Powered by FUDForum. Page generated in 0.03767 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top