Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » BIRT » Problem accessing 'params' in scripted data source
Problem accessing 'params' in scripted data source [message #922797] Tue, 25 September 2012 10:51 Go to next message
Paul Ramsden is currently offline Paul RamsdenFriend
Messages: 79
Registered: February 2011
Location: BW, Germany
Member
I have a data source DS1 which has the following Property Binding:

Connection Profile Name: params["__runtime"].value.toLowerCase() == 'prod' ? 'EMOS SQL Prod' : 'EMOS SQL Test'


This has worked fine so far for normal reports. I just added '__runtime' as a report parameter and it works.

I am now trying to make a script which executes a number of data sets and I get the following error message:

Wrapped org.eclipse.birt.core.exception.CoreException: Error evaluating Javascript expression. Script engine error: ReferenceError: "params" is not defined.
 Script source: <inline>, line: 0, text:
params["__runtime"].value.toLowerCase() == 'prod' ? 'EMOS SQL Prod' : 'EMOS SQL Test' (dsKontingentQueries.js#29) (Element ID:1) 


This is my script:
function getDataSession(dataSourceName, dataSetName) {
	debug("in getDataSession");
	var myconfig = reportContext.getReportRunnable().getReportEngine().getConfig();
	debug("in getDataSession2");
	var des = DataRequestSession.newSession(myconfig, new DataSessionContext(3));
	debug("in getDataSession3");
	var dsrc = reportContext.getDesignHandle().findDataSource(dataSourceName);
	debug("in getDataSession4");
	var dset = reportContext.getDesignHandle().findDataSet(dataSetName);
	debug("in getDataSession5");  // <=== last message, next line causes error
	des.defineDataSource(des.getModelAdaptor().adaptDataSource(dsrc));
	debug("in getDataSession6");
	des.defineDataSet(des.getModelAdaptor().adaptDataSet(dset));

	debug("leaving getDataSession");
	return des;
}


Do I need to do some kind of input parameter binding here? If so, how?

[Updated on: Tue, 25 September 2012 12:51]

Report message to a moderator

Re: Problem accessing 'params' in scripted data source [message #923230 is a reply to message #922797] Tue, 25 September 2012 18:29 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Can you try something like the following with calling the data engine?
importPackage( Packages.org.eclipse.birt.report.model.api );
importPackage(Packages.java.lang);
importPackage(Packages.java.util);
importPackage(Packages.org.eclipse.birt.data.engine.api);
importPackage(Packages.org.eclipse.birt.report.model.api);
importPackage(Packages.org.eclipse.birt.data.engine.api.querydefn);
importPackage(Packages.org.eclipse.birt.data.engine.core);


var myconfig = reportContext.getReportRunnable().getReportEngine().getConfig();
var de = DataEngine.newDataEngine( myconfig, null );

var dsrc = reportContext.getDesignHandle().findDataSource("Data Source");
var dset = reportContext.getDesignHandle().findDataSet("Data Set");

var odaDataSource = new OdaDataSourceDesign( "Test Data Source" );
odaDataSource.setExtensionID( "org.eclipse.birt.report.data.oda.jdbc" );
odaDataSource.addPublicProperty( "odaURL", dsrc.getProperty("odaURL").toString() );
odaDataSource.addPublicProperty( "odaDriverClass", dsrc.getProperty("odaDriverClass").toString());
odaDataSource.addPublicProperty( "odaUser", dsrc.getProperty("odaUser").toString() );
odaDataSource.addPublicProperty( "odaPassword", "" );


var odaDataSet = new OdaDataSetDesign( "Test Data Set" );
odaDataSet.setDataSource( odaDataSource.getName( ) );
odaDataSet.setExtensionID( "org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );
odaDataSet.setQueryText( dset.getQueryText() );


de.defineDataSource( odaDataSource );
de.defineDataSet( odaDataSet );

queryDefinition = new QueryDefinition( );
queryDefinition.setDataSetName( odaDataSet.getName() );
queryDefinition.setAutoBinding(true);


var pq = de.prepare( queryDefinition );

var qr = pq.execute( null );

rowcount=0;
var ri = qr.getResultIterator( );
while ( ri.next( ) )
{
rowcount++
}

ri.close( );
qr.close( );
de.shutdown( );

Jason
Re: Problem accessing 'params' in scripted data source [message #923681 is a reply to message #922797] Wed, 26 September 2012 05:23 Go to previous messageGo to next message
Paul Ramsden is currently offline Paul RamsdenFriend
Messages: 79
Registered: February 2011
Location: BW, Germany
Member
Jason

I tried the code you sent and it works though I'm not sure what it tells me. The Connection Profile Name property of the data source is a script which selects the connection based on a report parameter. It seems I'm just getting the default settings for my data source and the Connection Profile is not being used at all!

How could I modify your example script to set the connection profile property and ensure that profile selection script functions?

(I'm using BIRT 3.2.7)

Paul

[Updated on: Wed, 26 September 2012 05:53]

Report message to a moderator

Re: Problem accessing 'params' in scripted data source [message #924403 is a reply to message #923681] Wed, 26 September 2012 20:26 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

I am confused. Are you wanting to create a data source dynamically and set it to use a specific connection profile name? Do you store the connection profiles in the resource folder?

Jason
Re: Problem accessing 'params' in scripted data source [message #924736 is a reply to message #924403] Thu, 27 September 2012 04:54 Go to previous messageGo to next message
Paul Ramsden is currently offline Paul RamsdenFriend
Messages: 79
Registered: February 2011
Location: BW, Germany
Member
Sorry for the confusion!

I have a connection profile which contains connection details for both the test and production environments. The data source is defined and parameter binding used so that the connection name for the appropriate environment is chosen using a report parameter.

Connection Profile Name: params["__runtime"].value.toLowerCase() == 'prod' ? 'EMOS SQL Prod' : 'EMOS SQL Test'


I have a couple of data sets which I want to execute from a script and so I need to set up the data source which in turn needs the report parameter in order to initialise itself correctly.

Quite likely I'm doing a few things wrong here...

(I see that you answered a question of mine on StackOverflow yesterday which relates to this whole problem. http://stackoverflow.com/questions/12577027/how-to-write-a-new-scripted-data-set-which-uses-results-from-other-data-sets-in/12613644#12613644. I'll check that out now. Perhaps that will help)

Thanks

Paul
Re: Problem accessing 'params' in scripted data source [message #925390 is a reply to message #924736] Thu, 27 September 2012 17:14 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Paul

Let me know. You should be able to call any other data set as well using the Data Engine API within a BIRT script like I posted earlier. You may have to remove the property binding from the data source and instead use a beforeOpen script though.

Jason
Re: Problem accessing 'params' in scripted data source [message #929245 is a reply to message #925390] Mon, 01 October 2012 10:06 Go to previous messageGo to next message
Paul Ramsden is currently offline Paul RamsdenFriend
Messages: 79
Registered: February 2011
Location: BW, Germany
Member
I tried this (in the DataSource beforeOpen event)

var profilePath = "connectionStore.xml";

if (reportContext.getAppContext().containsKey('BIRT_VIEWER_HTTPSERVET_REQUEST')) {
	if(reportContext.getHttpServletRequest() != null) {
		var rp = reportContext.getHttpServletRequest().getSession().getServletContext().getRealPath("birt/connectionStore.xml");
		if( rp != null ){
			profilePath =  rp;
		}
	}
}


this.setExtensionProperty("OdaConnProfileStorePath", getProfilePath());
debug(getProfilePath());

var profileName = params["__runtime"].value.toLowerCase() == 'prod' ? 'EMOS SQL Prod' : 'EMOS SQL Test';
debug(profileName);
this.setExtensionProperty("OdaConnProfileName", profileName);



but I get the following errors

Mon Oct 01 12:02:21 CEST 2012:beforeFactory
Mon Oct 01 12:02:21 CEST 2012:queryDateRange
Mon Oct 01 12:02:21 CEST 2012:in getDataSession
Mon Oct 01 12:02:21 CEST 2012:leaving getDataSession
Mon Oct 01 12:02:21 CEST 2012:before exec
Mon Oct 01 12:02:21 CEST 2012:JavaException: org.eclipse.birt.data.engine.core.DataException: Fail to execute script in function __bm_beforeOpen(). Source:
------
" + var profilePath = "connectionStore.xml";

if (reportContext.getAppContext().containsKey('BIRT_VIEWER_HTTPSERVET_REQUEST')) {
	if(reportContext.getHttpServletRequest() != null) {
		var rp = reportContext.getHttpServletRequest().getSession().getServletContext().getRealPath("birt/connectionStore.xml");
		if( rp != null ){
			profilePath =  rp;
		}
	}
}


this.setExtensionProperty("OdaConnProfileStorePath", getProfilePath());
debug(getProfilePath());

var profileName = params["__runtime"].value.toLowerCase() == 'prod' ? 'EMOS SQL Prod' : 'EMOS SQL Test';
debug(profileName);
this.setExtensionProperty("OdaConnProfileName", profileName);

 + "
-----
A BIRT exception occurred. See next exception for more information.
Error evaluating Javascript expression. Script engine error: Wrapped org.eclipse.birt.report.data.adapter.api.AdapterException: ReportContext method [getAppContext] can only be used in runtime. (<inline>#3)
 Script source: <inline>, line: 0, text:
__bm_beforeOpen()
Mon Oct 01 12:02:21 CEST 2012:after queryDateRange
Mon Oct 01 12:02:21 CEST 2012:undefined
Mon Oct 01 12:02:21 CEST 2012:undefined
Mon Oct 01 12:02:21 CEST 2012:queryDateRange
Mon Oct 01 12:02:21 CEST 2012:in getDataSession
Mon Oct 01 12:02:21 CEST 2012:leaving getDataSession
Mon Oct 01 12:02:21 CEST 2012:before exec
Mon Oct 01 12:02:21 CEST 2012:JavaException: org.eclipse.birt.data.engine.core.DataException: Fail to execute script in function __bm_beforeOpen(). Source:
------
" + var profilePath = "connectionStore.xml";

if (reportContext.getAppContext().containsKey('BIRT_VIEWER_HTTPSERVET_REQUEST')) {
	if(reportContext.getHttpServletRequest() != null) {
		var rp = reportContext.getHttpServletRequest().getSession().getServletContext().getRealPath("birt/connectionStore.xml");
		if( rp != null ){
			profilePath =  rp;
		}
	}
}


this.setExtensionProperty("OdaConnProfileStorePath", getProfilePath());
debug(getProfilePath());

var profileName = params["__runtime"].value.toLowerCase() == 'prod' ? 'EMOS SQL Prod' : 'EMOS SQL Test';
debug(profileName);
this.setExtensionProperty("OdaConnProfileName", profileName);

 + "
-----
A BIRT exception occurred. See next exception for more information.
Error evaluating Javascript expression. Script engine error: Wrapped org.eclipse.birt.report.data.adapter.api.AdapterException: ReportContext method [getAppContext] can only be used in runtime. (<inline>#3)
 Script source: <inline>, line: 0, text:
__bm_beforeOpen()



What does 'ReportContext method [getAppContext] can only be used in runtime.' actually mean and what can I do to correct it?

Thanks

Paul

[Updated on: Mon, 01 October 2012 10:07]

Report message to a moderator

Re: Problem accessing 'params' in scripted data source [message #929651 is a reply to message #929245] Mon, 01 October 2012 16:30 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

I assume you are getting this error when calling the data source with the data engine api. This is because you have not setup the runtime env for that particular data engine call. If you can give some more detail I may be able to help you come up with another approach. Why are your calling the de api to begin with?

Jason
Re: Problem accessing 'params' in scripted data source [message #930355 is a reply to message #929651] Tue, 02 October 2012 08:33 Go to previous messageGo to next message
Paul Ramsden is currently offline Paul RamsdenFriend
Messages: 79
Registered: February 2011
Location: BW, Germany
Member
Jason

thanks for your help. I am quite surprised to find myself poking around at this level. I guess one thing led to another...

I don't quite know where to begin so I'll go right back to the start.

o connection profiles in a profile store. Profiles named like MSSQL_PRod, MSSQL_Test, DB2_Prod, DB2_Test etc.
o DataSources configured at runtime depending on a parameter (PropertyBinding)
o Most queries can be used as-is
o Some queries contain compacted data. Simplified example: Hotel bookings at trade fair: only days with actual bookings are in the result. I need to expand the result set to include days with no bookings so that I can display a table of occupation for all hotels and all days.
o Started to write scripted data set to bring all needed queries together and then manipulate and expand results
o Need to setup and execute Db2 and MSSQL queries to fetch all data needed. This is why I started to mess with the DE API

I hope this gives you an idea of where I'm coming from. Perhaps there is an easy solution to this especially as BIRT seems to be so flexible. I just feel I'm stumbling in the dark though.

Unfortunately, I'm rapidly running out of time on this and I might have to resort to using a web service to perform the functions I need. The web services add extra complexity to the whole project and I was hoping to avoid adding more...

Thanks again for your help!

Paul
Re: Problem accessing 'params' in scripted data source [message #930801 is a reply to message #930355] Tue, 02 October 2012 16:55 Go to previous message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Paul

You should be able to use a scripted data set combined with a Joint data set to add to the data. The point I am confused about is why you need the DE API.
Need to setup and execute Db2 and MSSQL queries to fetch all data needed. This is why I started to mess with the DE API -- What are you doing with this data.

Jason
Previous Topic:crosstab multiple grand totals / summary
Next Topic:Arabic
Goto Forum:
  


Current Time: Thu Nov 20 21:06:07 GMT 2014

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

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