The context Map needs to be passed in the ScriptUtil.executeScript
method args argument. I originally set up the events and services to do
that, but it was removed. See rev 1300473.
-Adrian
On 4/13/2012 1:33 PM, Jacopo Cappellato wrote:
Thank you Adrian,
but I am not sure I understand what you are proposing; could you add some
details?
If I use the declaration:
def testMethod1(Map context) {
...
}
I get the error:
javax.script.ScriptException: java.lang.NullPointerException: Cannot set
property 'orderId' on null object
Thanks,
Jacopo
On Apr 13, 2012, at 12:57 PM, Adrian Crum wrote:
Functions/methods should return a value. Also, according to the JSR if a
scripting language doesn't support returning a value, then the last value
assigned to a variable is returned.
So, trying to return values from a function via the context Map will not work.
That's why I originally supplied the context Map as a function/method argument
- so you could do something like:
def testMethod1(Map context) {
orderId = parameters.orderId;
context.orderId = orderId;
}
-Adrian
On 4/13/2012 11:37 AM, Jacopo Cappellato wrote:
Hi Adrian,
I am trying to test the ability to specify a method name from from a<script>
element in a screen widget but I am getting an error and I am wondering if you could
help in figuring out what is wrong.
If you apply this patch:
Index: applications/order/widget/ordermgr/OrderViewScreens.xml
===================================================================
--- applications/order/widget/ordermgr/OrderViewScreens.xml (revision
1325649)
+++ applications/order/widget/ordermgr/OrderViewScreens.xml (working copy)
@@ -55,7 +55,7 @@
<set field="layoutSettings.javaScripts[]"
value="/ordermgr/images/js/order.js" global="true"/>
<set field="layoutSettings.javaScripts[]"
value="/ordermgr/images/js/OrderShippingInfo.js" global="true"/>
<set field="layoutSettings.javaScripts[]"
value="/ordermgr/images/js/geoAutoCompleter.js" global="true"/>
-<script
location="component://order/webapp/ordermgr/WEB-INF/actions/order/OrderView.groovy"/>
+<script
location="component://order/webapp/ordermgr/WEB-INF/actions/order/OrderView.groovy#testMethod1"/>
<script
location="component://order/webapp/ordermgr/WEB-INF/actions/order/OrderViewWebSecure.groovy"/>
</actions>
<widgets>
Index: applications/order/webapp/ordermgr/WEB-INF/actions/order/OrderView.groovy
===================================================================
--- applications/order/webapp/ordermgr/WEB-INF/actions/order/OrderView.groovy
(revision 1325649)
+++ applications/order/webapp/ordermgr/WEB-INF/actions/order/OrderView.groovy
(working copy)
@@ -33,6 +33,8 @@
import javolution.util.FastMap;
+def testMethod1() {
+
orderId = parameters.orderId;
context.orderId = orderId;
@@ -494,3 +496,5 @@
}
}
context.orderAdjustmentId = orderAdjustmentId;
+
+}
and then visit this url:
https://localhost:8443/ordermgr/control/orderview?orderId=Demo1002
you will get the following error:
[java] Error running script at location
[component://order/webapp/ordermgr/WEB-INF/actions/order/OrderView.groovy]:
javax.script.ScriptException: groovy.lang.MissingPropertyException: No such
property: context for class: Script1
[java] Exception: javax.script.ScriptException
[java] Message: groovy.lang.MissingPropertyException: No such property:
context for class: Script1
[java] ---- cause
---------------------------------------------------------------------
[java] Exception: groovy.lang.MissingPropertyException
[java] Message: No such property: context for class: Script1
[java] ---- stack trace
---------------------------------------------------------------
[java] groovy.lang.MissingPropertyException: No such property: context
for class: Script1
[java]
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
[java]
org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
[java]
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
[java] Script1.testMethod1(Script1.groovy:39)
[java] sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java]
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java]
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[java] java.lang.reflect.Method.invoke(Method.java:597)
[java]
org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
[java] groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
[java] groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1047)
[java] groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:914)
[java] groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:877)
[java] groovy.lang.Closure.call(Closure.java:412)
[java]
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.callGlobal(GroovyScriptEngineImpl.java:385)
[java]
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.callGlobal(GroovyScriptEngineImpl.java:379)
[java]
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.invokeImpl(GroovyScriptEngineImpl.java:368)
[java]
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.invokeFunction(GroovyScriptEngineImpl.java:163)
[java] org.ofbiz.base.util.ScriptUtil.executeScript(ScriptUtil.java:387)
[java] org.ofbiz.base.util.ScriptUtil.executeScript(ScriptUtil.java:339)
[java] org.ofbiz.base.util.ScriptUtil.executeScript(ScriptUtil.java:324)
[java]
org.ofbiz.widget.ModelWidgetAction$Script.runAction(ModelWidgetAction.java:416)
...
So the error is caused by line 387 of ScriptUtil.java; here is the relevant
code:
if (UtilValidate.isNotEmpty(functionName)) {
try {
Invocable invocableEngine = (Invocable) engine;
result = invocableEngine.invokeFunction(functionName, args ==
null ? EMPTY_ARGS : args); // this is where the error is thrown
} catch (ClassCastException e) {
throw new ScriptException("Script engine " + engine.getClass().getName()
+ " does not support function/method invocations");
}
}
Any hint on how I could solve this?
Thanks!
Jacopo