To illustrate the problem this commit solves, let's use this example:
<set field="someList"
value="${groovy:someObject.methodThatReturnsAList();}" type="List" />
You would assume the someList field contains the List that
someObject.methodThatReturnsAList() returned. It doesn't. Instead, a new
List is created, the list returned from
someObject.methodThatReturnsAList() is converted to a String, the String
is added to the new List, and the new List is returned.
-Adrian
On 9/3/2011 2:25 PM, Adrian Crum wrote:
On 9/3/2011 2:16 PM, Jacques Le Roux wrote:
Comments inline
From: "Adrian Crum" <[email protected]>
I agree it is confusing. A while ago I suggested having another
attribute like from-expression, but there wasn't much interest in it.
Would be more clear than from-field IMO, but I still wonder if any of
those is needed...
Yes, an attribute is needed that returns something other than a
String. The from-field attribute returns an Object retrieved from the
context Map. The value and default-value attributes return a String.
You can try to convert those Strings to the desired type, but some
types can not be converted (like the entity models I was trying to use).
The value attribute returns a String, while from-field attribute
returns an Object.
Are you sure? Actually I have not much used groovy: in minilang. I
have used it in screen actions and there you can also specify the
type using type=
Yes, I am sure. I spent several hours trying to find a way to call an
Object's method and get the correct type returned - no luck.
And looking in simple-method services in found many cases where type=
is used also
Look at the WorkEffort services I just committed. See if you can get
them to work without this commit.
Jacques
-Adrian
On 9/3/2011 12:16 PM, Jacques Le Roux wrote:
Is it not a bit confusing (it's not a field at all), was not value
enough?
Jacques
From: <[email protected]>
Author: adrianc
Date: Fri Sep 2 20:55:15 2011
New Revision: 1164712
URL: http://svn.apache.org/viewvc?rev=1164712&view=rev
Log:
Improved mini-language <set> operation: now supports groovy:
syntax in the from-field attribute.
Modified:
ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/SetOperation.java
Modified:
ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/SetOperation.java
URL:
http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/SetOperation.java?rev=1164712&r1=1164711&r2=1164712&view=diff
==============================================================================
---
ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/SetOperation.java
(original)
+++
ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/SetOperation.java
Fri Sep 2 20:55:15 2011
@@ -21,8 +21,10 @@ package org.ofbiz.minilang.method.envops
import javolution.util.FastList;
import javolution.util.FastMap;
+import org.codehaus.groovy.runtime.InvokerHelper;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
+import org.ofbiz.base.util.GroovyUtil;
import org.ofbiz.base.util.ObjectType;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.base.util.string.FlexibleStringExpander;
@@ -54,11 +56,16 @@ public class SetOperation extends Method
protected String type;
protected boolean setIfNull; // default to false
protected boolean setIfEmpty; // default to true
+ protected Class<?> parsedGroovyScript = null;
public SetOperation(Element element, SimpleMethod simpleMethod) {
super(element, simpleMethod);
this.field = new
ContextAccessor<Object>(element.getAttribute("field"));
- this.fromField = new
ContextAccessor<Object>(element.getAttribute("from-field"));
+ String fromFieldStr = element.getAttribute("from-field");
+ if (fromFieldStr != null &&
fromFieldStr.startsWith("groovy:")) {
+ this.parsedGroovyScript =
GroovyUtil.parseClass(fromFieldStr.replace("groovy:", ""));
+ }
+ this.fromField = new ContextAccessor<Object>(fromFieldStr);
this.valueExdr =
FlexibleStringExpander.getInstance(element.getAttribute("value"));
this.defaultExdr =
FlexibleStringExpander.getInstance(element.getAttribute("default-value"));
this.type = element.getAttribute("type");
@@ -75,7 +82,9 @@ public class SetOperation extends Method
@Override
public boolean exec(MethodContext methodContext) {
Object newValue = null;
- if (!this.fromField.isEmpty()) {
+ if (this.parsedGroovyScript != null) {
+ newValue =
InvokerHelper.createScript(this.parsedGroovyScript,
GroovyUtil.getBinding(methodContext.getEnvMap())).run();
+ } else if (!this.fromField.isEmpty()) {
newValue = this.fromField.get(methodContext);
if (Debug.verboseOn()) Debug.logVerbose("In screen
getting value for field from [" + this.fromField.toString() + "]:
" + newValue, module);
} else if (!this.valueExdr.isEmpty()) {