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)
Using a Java event handler to compute data
R2D4
Hello everybody,<br />
<br />
I'm using Birt to access an XML datasource and I need to make some computations on data using Java. After some reading I kind of concluded I need to implement a Java event handler to perform the needed computations. I can't get it to work however.<br />
<br />
What I've done so far:<br />
- create a Java class in my Birt project for the event handler<br />
- add the event handler to the event handler field of my data set<br />
<br />
My event handler looks like this: (I know this very very basic event handler will not do anything at all)<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
import org.eclipse.birt.chart.datafeed.DataSetAdapter;
import org.eclipse.birt.report.engine.api.script.IDataSetRow;
import org.eclipse.birt.report.engine.api.script.IReportContext;
import org.eclipse.birt.report.engine.api.script.ScriptException;
import org.eclipse.birt.report.engine.api.script.instance.IDataSetInstance;
public class DataSetFunctions extends DataSetAdapter {
public void onFetch (IDataSetInstance dataSet, IDataSetRow row, IReportContext reportContext) {
try {
String DateTimeValue = (String) row.getColumnValue("datetimerow");
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
</pre>
<br />
When I execute the report (from a Java application which integrates the report) I get this error: <strong class='bbc'>WARNING: DataSetFunctions cannot be cast to org.eclipse.birt.report.engine.api.script.eventhandler.IDataItemEventHandler</strong>.<br />
<br />
I don't really get what I am doing wrong... is there something wrong with my code? Or did I use the wrong place for the event handler?<br />
<br />
Thanks, R2
Find more posts tagged with
Comments
Virgil Dodson
Hi R2,
You should be able to see your Java event handler running in the Eclipse IDE, but creating a seperate Java Project... putting that .java file in the src... and then open your Project properties for your BIRT project and check under Project References that your new Java project is referenced.
Does it work then?
R2D4
<blockquote class='ipsBlockquote' data-author="vdodson"><p>Hi R2,<br />
<br />
You should be able to see your Java event handler running in the Eclipse IDE, but creating a seperate Java Project... putting that .java file in the src... and then open your Project properties for your BIRT project and check under Project References that your new Java project is referenced.<br />
<br />
Does it work then?</p></blockquote>
<br />
Hi Virgil,<br />
<br />
Thanks for the suggestion. After moving the .java to a separate project and making it visible from the Birt project, I gave it another try... same error. <br />
<br />
When I remove the project reference between the two I get some other fancy
errors on my class not being found, so I presume the relation between the projects is okay.<br />
<br />
Thanks, R2
alex520
you should extends DataSetEventAdapter not DataSetAdapter <br />
<br />
code below is from wjax conference , yon can get more script example from<br />
<a class='bbc_url' href='
http://www.birt-exchange.com/devshare/designing-birt-reports/686-birt-scripting-examples-from-wjax/#description'>BIRT
Scripting Examples from WJAX - Designs & Code - BIRT Exchange</a><br />
<br />
<br />
package my.event.handlers;<br />
<br />
import org.eclipse.birt.report.engine.api.script.IDataSetRow;<br />
import org.eclipse.birt.report.engine.api.script.IReportContext;<br />
import org.eclipse.birt.report.engine.api.script.eventadapter.DataSetEventAdapter;<br />
import org.eclipse.birt.report.engine.api.script.instance.IDataSetInstance;<br />
<br />
public class MyDataSet extends DataSetEventAdapter {<br />
<br />
@Override<
;br />
public void afterClose(IReportContext reportContext) {<br />
// TODO Auto-generated method stub<br />
super.afterClose(reportContext);<br />
}<br />
<br />
@Override<
;br />
public void afterOpen(IDataSetInstance dataSet, IReportContext reportContext) {<br />
// TODO Auto-generated method stub<br />
super.afterOpen(dataSet, reportContext);<br />
}<br />
<br />
@Override<
;br />
public void beforeClose(IDataSetInstance dataSet,<br />
IReportContext reportContext) {<br />
// TODO Auto-generated method stub<br />
super.beforeClose(dataSet, reportContext);<br />
}<br />
<br />
@Override<
;br />
public void beforeOpen(IDataSetInstance dataSet,<br />
IReportContext reportContext) {<br />
try{<br />
int chk = dataSet.getColumnMetaData().getColumnCount();<br />
for( int i =0; i < chk; i++){<br />
System.out.println(dataSet.getColumnMetaData().getColumnName(i));<br />
<br />
}<br />
}catch(Exception e){<br />
e.printStackTrace();<br />
}<br />
<br />
<br />
// TODO Auto-generated method stub<br />
super.beforeOpen(dataSet, reportContext);<br />
}<br />
<br />
@Override<
;br />
public void onFetch(IDataSetInstance dataSet, IDataSetRow row,<br />
IReportContext reportContext) {<br />
// TODO Auto-generated method stub<br />
super.onFetch(dataSet, row, reportContext);<br />
}<br />
<br />
}
R2D4
<blockquote class='ipsBlockquote' data-author="alex520"><p>you should extends DataSetEventAdapter not DataSetAdapter <br />
<br />
code below is from wjax conference , yon can get more script example from<br />
<a class='bbc_url' href='
http://www.birt-exchange.com/devshare/designing-birt-reports/686-birt-scripting-examples-from-wjax/#description'>BIRT
Scripting Examples from WJAX - Designs & Code - BIRT Exchange</a><br />
<br />
<br />
package my.event.handlers;<br />
<br />
import org.eclipse.birt.report.engine.api.script.IDataSetRow;<br />
import org.eclipse.birt.report.engine.api.script.IReportContext;<br />
import org.eclipse.birt.report.engine.api.script.eventadapter.DataSetEventAdapter;<br />
import org.eclipse.birt.report.engine.api.script.instance.IDataSetInstance;<br />
<br />
public class MyDataSet extends DataSetEventAdapter {<br />
<br />
@Override<
;br />
public void afterClose(IReportContext reportContext) {<br />
// TODO Auto-generated method stub<br />
super.afterClose(reportContext);<br />
}<br />
<br />
@Override<
;br />
public void afterOpen(IDataSetInstance dataSet, IReportContext reportContext) {<br />
// TODO Auto-generated method stub<br />
super.afterOpen(dataSet, reportContext);<br />
}<br />
<br />
@Override<
;br />
public void beforeClose(IDataSetInstance dataSet,<br />
IReportContext reportContext) {<br />
// TODO Auto-generated method stub<br />
super.beforeClose(dataSet, reportContext);<br />
}<br />
<br />
@Override<
;br />
public void beforeOpen(IDataSetInstance dataSet,<br />
IReportContext reportContext) {<br />
try{<br />
int chk = dataSet.getColumnMetaData().getColumnCount();<br />
for( int i =0; i < chk; i++){<br />
System.out.println(dataSet.getColumnMetaData().getColumnName(i));<br />
<br />
}<br />
}catch(Exception e){<br />
e.printStackTrace();<br />
}<br />
<br />
<br />
// TODO Auto-generated method stub<br />
super.beforeOpen(dataSet, reportContext);<br />
}<br />
<br />
@Override<
;br />
public void onFetch(IDataSetInstance dataSet, IDataSetRow row,<br />
IReportContext reportContext) {<br />
// TODO Auto-generated method stub<br />
super.onFetch(dataSet, row, reportContext);<br />
}<br />
<br />
}</p></blockquote>
<br />
Thanks Alex,<br />
<br />
That did the trick; my documentation was wrong (I'm using Addison Wesley's Integrating and extending Birt 2nd edition, which dates from july 2008) on this one!<br />
<br />
Thanks!<br />
<br />
Btw: I have a totally new problem; although my code is alright the onFetch handler doesn't do a thing. As this is a separate problem I have openened a new thread: <a class='bbc_url' href='
http://www.birt-exchange.com/forum/deploying-integrating-birt-report-engine-applications/14222-onfetch-methode-doesnt-fire-overridden-eventhandler.html'>http://www.birt-exchange.com/forum/deploying-integrating-birt-report-engine-applications/14222-onfetch-methode-doesnt-fire-overridden-eventhandler.html</a>
;
rajasrini
<p>Hi....</p><p>how to call the birt report design to java file? please attach one report design.....plz help me....</p><p> </p><p> </p><p> </p><p> </p><p>Thanks,</p><p>Raja</p>