Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » BIRT » How to use scriptContext class in Birt engine 2.6.1
How to use scriptContext class in Birt engine 2.6.1 [message #685268] Fri, 17 June 2011 06:57 Go to next message
Ouke  is currently offline Ouke
Messages: 2
Registered: June 2011
Junior Member
Hi folks:

I use the birt api just want to get the DataSet query result from birt report.
This birt report define several parameters.
So I need to create context to include all of them.
I create script context like below:

org.mozilla.javascript.ScriptableObject scriptScope = (ScriptableObject)i_engine.getRootScope();
org.mozilla.javascript.ScriptContext scriptContext = new ScriptContext(scriptScope);
scriptContext.getContext().setWrapFactory( new WrapFactory());
new CoreJavaScriptInitializer().initialize(scriptContext.getContext(), scriptScope);
Map rptParams; // This Map save all parameter name and value which define in report
Object obj = scriptContext.javaToJs(rptParams);
scriptScope.put("params",scriptScope,obj);
......

But now, I need to upgrade the birt engine version from 2.3.1 to 2.6.1.
The interface of ScriptContext has been changed(exp, getContext(),javaToJs() method remove),
the code above can not be complied successful.

I do not know the new ScriptContext how to use. And can have the same action before.
Could someone can help me?

[Updated on: Fri, 17 June 2011 07:33]

Report message to a moderator

Re: How to use scriptContext class in Birt engine 2.6.1 [message #685449 is a reply to message #685268] Fri, 17 June 2011 14:25 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason Weathersby
Messages: 9167
Registered: July 2009
Senior Member

I am not certain this will help but here is the ScriptContext test class.

/*******************************************************************************
* Copyright (c) 2004 Actuate Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Actuate Corporation - initial API and implementation

*******************************************************************************/

package org.eclipse.birt.core.script;

import java.text.DateFormat;
import java.util.ArrayList;

import junit.framework.TestCase;

import org.eclipse.birt.core.exception.BirtException;

/**
*
*/
public class ScriptContextTest extends TestCase
{

ScriptContext context;

public void setUp( )
{
context = new ScriptContext( );
}

public void tearDown( )
{
context.close( );
}

/**
* test if the enterScope & exitScope is correct.
*/
public void testScope( ) throws BirtException
{
//register A in root
context.setAttribute( "A", new Integer( 10 ) );
//register B in root
ScriptContext context1 = context.newContext( null );
context.setAttribute( "B", new Integer( 20 ) );
Object result = eval( context1, "A + B" );
assertEquals( ( (Number) result ).doubleValue( ), 30.0,
Double.MIN_VALUE );
//B is valid now
boolean hasException = false;
try
{
result = eval( context, "A + B" );
}
catch ( Exception ex )
{
hasException = true;
}
assertTrue( !hasException );
//A is still valid
result = eval( context, "A" );
assertEquals( ( (Number) result ).doubleValue( ), 10.0,
Double.MIN_VALUE );
}

private Object eval( ScriptContext scriptContext, String script )
throws BirtException
{
Object result = scriptContext.evaluate( context.compile( "javascript",
"<inline>", 1, script ) );
return result;
}

/**
* Test if we can use NativeJavaObject as scope.
*/
public void testJavaScope() throws BirtException
{
StringBuffer buffer = new StringBuffer();
//define a function in the root
eval(context, "function getText() { return 'TEXT'};");

ScriptContext context1 = context.newContext( buffer );
//enter java-based scope
eval(context1, "append(getText());");
eval(context1, "append('TEXT2');");

assertEquals("TEXTTEXT2", buffer.toString());
Object result = eval(context, "getText()");
assertEquals("TEXT", result);
}

/**
* compile a script and running it in different scope
* to see if it returns differnt values.
*
* Expected:
*
* the same code running in different scope reutrns different values.
*/
public void testCompiledScript() throws BirtException
{
ScriptContext context1 = context.newContext( null );
eval(context1, "function getText() { return 'A'}");
assertEquals("A", eval(context1, "getText()"));
ScriptContext context2 = context.newContext( null );
eval(context2, "function getText() { return 'B'}");
assertEquals("B", eval(context2, "getText()"));
boolean hasException = false;
try
{
eval(context, "getText()");
}
catch(Exception ex)
{
hasException = true;
}
assertTrue(hasException);

}

/**
* Test if the defineClass/definePackage is supported by script.
*/
public void testGlobal() throws BirtException
{
eval(context, "importPackage(java.util)");
eval(context, "importClass(java.text.DateFormat)");
Object list = eval(context, "new ArrayList()");
Object fmt = eval(context, "DateFormat.getInstance()");
assertTrue(list instanceof ArrayList);
assertTrue(fmt instanceof DateFormat);
}


/**
* context shares the object in the root scope
*/
public void testRootScope( ) throws BirtException
{
context.setAttribute( "share", "ABCDEFG" );
Object result = eval( context, "share + 'c'" );
assertEquals( "ABCDEFGc", result.toString( ) );
context.close( );
}

/**
* In javascript, the "this" always point to the
* current scope.
*/
public void testThisObject() throws BirtException
{
context.setAttribute("A", "ABCDE");

ScriptContext context1 = context.newContext( null );
context1.setAttribute( "a", "VALUE");
Object result = eval(context1, "a");
assertEquals("VALUE", result);

//it can use this to access the member of scope
result = eval(context1, "this.a");
assertEquals("VALUE", result);

//it can access the member of parent
result = eval(context1, "A");
assertEquals("ABCDE", result);

//it can not use this to access the member of parent.
result = eval(context1, "this.A");
assertEquals(null, result);

context.close();
}
}

Jason

On 6/17/2011 2:57 AM, Ouke wrote:
> Hi folks:
>
> I have a birt report which have several input parameters.
> The value of input parameters are set in java code.
> I use birt api to create script context to warp the parameter object to
> javascript object:
>
> org.mozilla.javascript.ScriptableObject scriptScope =
> (ScriptableObject)i_engine.getRootScope();
> org.mozilla.javascript.ScriptContext scriptContext = new
> ScriptContext(scriptScope);
> scriptContext.getContext().setWrapFactory( new WrapFactory());
> new CoreJavaScriptInitializer().initialize(scriptContext.getContext(),
> scriptScope);
>
> But now, I need to upgrade the birt engine version from 2.3.1 to 2.6.1.
> The interface of ScriptContext has been changed(exp, getContext(),
> getRootScope()method remove),
> the code above can not be complied successful.
>
> I do not know the new ScriptContext how to use. And can have the same
> action before.
> Could someone can help me?
Re: How to use scriptContext class in Birt engine 2.6.1 [message #693808 is a reply to message #685449] Thu, 07 July 2011 08:38 Go to previous messageGo to next message
Ouke  is currently offline Ouke
Messages: 2
Registered: June 2011
Junior Member
Hi Jason

I am sorry to update so late.
Because some of the interface for scriptContext do not support by 2.3.1 & 2.6.1.
So I try to use the common interface in order to be supported by both version.
I design a birt report which has one dataSet and one parameter named "IndexTooltip".
This dataSet has a computed Column which named "localizedindex" and it link to "IndexTooltip" parameter.
I just want to run the DataSet query and then get the result to other use.
Below is my code.
==================================
Context scontext = Context.enter();
ImporterTopLevel importerTopLevel = new ImporterTopLevel();
importerTopLevel.initStandardObjects(scontext, true);

// Gets the runtime parameter value for parameter "IndexTooltip"
IGetParameterDefinitionTask parameterTask = reportEngine.createGetParameterDefinitionTask(report);
Collection definitionParams = parameterTask.getParameterDefns(true);
Iterator reportparamIter = definitionParams.iterator();
Map reportParams = HashMap();

while (reportparamIter .hasNext())
{
IParameterDefnBase definitionParam = (IParameterDefnBase) reportparamIter.next();
getReportParms((IScalarParameterDefn) definitionParam,rptParams);

String paramName = definitionParam.getName();
int paramType = definitionParam.getDataType();
String paramFormat = definitionParam.getDisplayFormat();

Object param = getParameterValue(paramName); //get the runtime value
reportParams.put(paramName, param);
}

// Here is the subclass for BaseScriptable and rewritten the get() and put() method.
RptScriptableParms scriptParams = new RptScriptableParms (reportParams, importerTopLevel);
Object objs = scontext.javaToJS(scriptParams, importerTopLevel);

// Here i put the runtime parameters into the script Top Scope
importerTopLevel.put("params", importerTopLevel, objs);

Then I run the query under the top Scope
IQueryResults results = query.execute( importerTopLevel );

But when I get the resultIterator like below:
IResultIterator resultItr = resultSet.getResultIterator();

It occur the exception which told to me the parameter do not define.

ERROR [com.spss.reporting.ws.ReportingImpl]
org.eclipse.birt.data.engine.core.DataException: Fail to compute value for computed column "localizedindex".
A BIRT exception occurred: There are errors evaluating script "params["IndexTooltip"]":
ReferenceError: "params" is not defined.. See next exception for more information.
There are errors evaluating script "params["IndexTooltip"]":
ReferenceError: "params" is not defined.
at org.eclipse.birt.data.engine.impl.ComputedColumnHelperInstance.process(ComputedColumnHelper.java:511)
at org.eclipse.birt.data.engine.impl.ComputedColumnHelper.process(ComputedColumnHelper.java:119)
at org.eclipse.birt.data.engine.executor.cache.RowResultSet.processFetchEvent(RowResultSet.java:152)
at org.eclipse.birt.data.engine.executor.cache.RowResultSet.next(RowResultSet.java:113)
at org.eclipse.birt.data.engine.executor.transform.SimpleResultSet.<init>(SimpleResultSet.java:85)
at org.eclipse.birt.data.engine.executor.DataSourceQuery.execute(DataSourceQuery.java:874)
at org.eclipse.birt.data.engine.impl.PreparedOdaDSQuery$OdaDSQueryExecutor.executeOdiQuery(PreparedOdaDSQuery.java:427)
at org.eclipse.birt.data.engine.impl.QueryExecutor.execute(QueryExecutor.java:1094)
at org.eclipse.birt.data.engine.impl.ServiceForQueryResults.executeQuery(ServiceForQueryResults.java:232)
at org.eclipse.birt.data.engine.impl.QueryResults.getResultIterator(QueryResults.java:172)

I do not know where is wrong. Could you help me?
Thank you very much.

[Updated on: Fri, 08 July 2011 01:56]

Report message to a moderator

Re: How to use scriptContext class in Birt engine 2.6.1 [message #694457 is a reply to message #693808] Fri, 08 July 2011 16:25 Go to previous message
Jason Weathersby is currently offline Jason Weathersby
Messages: 9167
Registered: July 2009
Senior Member

I am not exactly sure what you are doing here but the params object is
placed in the script context by the ExecutionContext.java class. You
may want to check it out and have a look. It is in the
org.eclipse.birt.report.engine plugin.

Jason

On 7/7/2011 4:38 AM, Ouke wrote:
> Hi Jason
>
> I am sorry to update so late.
> Because some of the interface for scriptContext do not both support by
> 2.3.1 & 2.6.1.
> So I try to use the common interface to do it.
> I design a birt report which has one dataSet and one parameter named
> "IndexTooltip".
> This dataSet has a computed Column which named "localizedindex" and it
> link to "IndexTooltip" parameter.
> Below is my rewrite code.
> ==================================
> Context scontext = Context.enter();
> ImporterTopLevel importerTopLevel = new ImporterTopLevel();
> importerTopLevel.initStandardObjects(scontext, true);
>
> // Gets the runtime parameter value for parameter "IndexTooltip"
> Map reportParams = getReportParameters();
>
> // Here is the subclass for BaseScriptable.
> RptScriptableParms scriptParams = new RptScriptableParms (reportParams,
> importerTopLevel);
> Object objs = scontext.javaToJS(scriptParams, importerTopLevel);
>
> // Here i put the runtime parameters into the script Top Scope
> importerTopLevel.put("parameters", importerTopLevel, objs);
>
> Then I run the query under the top Scope
> IQueryResults results = query.execute( importerTopLevel );
>
> But when I get the resultIterator like below:
> IResultIterator resultItr = resultSet.getResultIterator();
>
> It occur the exception which told to me the parameter do not define.
>
> ERROR [com.spss.reporting.ws.ReportingImpl]
> org.eclipse.birt.data.engine.core.DataException: Fail to compute value
> for computed column "localizedindex".
> A BIRT exception occurred: There are errors evaluating script
> "params["IndexTooltip"]":
> ReferenceError: "params" is not defined.. See next exception for more
> information.
> There are errors evaluating script "params["IndexTooltip"]":
> ReferenceError: "params" is not defined.
> at
> org.eclipse.birt.data.engine.impl.ComputedColumnHelperInstance.process(ComputedColumnHelper.java:511)
>
> at
> org.eclipse.birt.data.engine.impl.ComputedColumnHelper.process(ComputedColumnHelper.java:119)
>
> at
> org.eclipse.birt.data.engine.executor.cache.RowResultSet.processFetchEvent(RowResultSet.java:152)
>
> at
> org.eclipse.birt.data.engine.executor.cache.RowResultSet.next(RowResultSet.java:113)
>
> at
> org.eclipse.birt.data.engine.executor.transform.SimpleResultSet.<init>(SimpleResultSet.java:85)
>
> at
> org.eclipse.birt.data.engine.executor.DataSourceQuery.execute(DataSourceQuery.java:874)
>
> at
> org.eclipse.birt.data.engine.impl.PreparedOdaDSQuery$OdaDSQueryExecutor.executeOdiQuery(PreparedOdaDSQuery.java:427)
>
> at
> org.eclipse.birt.data.engine.impl.QueryExecutor.execute(QueryExecutor.java:1094)
>
> at
> org.eclipse.birt.data.engine.impl.ServiceForQueryResults.executeQuery(ServiceForQueryResults.java:232)
>
> at
> org.eclipse.birt.data.engine.impl.QueryResults.getResultIterator(QueryResults.java:172)
>
>
> I do not know where is wrong. Could you help me again?
> Thank you very much.
Previous Topic:Dynamic Sorting on Derived Columns of Crosstabs
Next Topic:params["dt1"].setHours(23,59,59,999); return error?!!
Goto Forum:
  


Current Time: Mon Oct 20 09:53:40 GMT 2014

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

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