Author: adrianc
Date: Tue Mar 23 22:34:54 2010
New Revision: 926819
URL: http://svn.apache.org/viewvc?rev=926819&view=rev
Log:
Added the ability for screen widgets to invoke Groovy script methods. Groovy
script methods do not have parameters; parameters are passed in the context -
just like other screen widget actions. To use:
<actions>
<script location="groovyLocation#methodName"/>
</actions>
Modified:
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/WidgetWorker.java
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ModelScreenAction.java
Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/WidgetWorker.java
URL:
http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/WidgetWorker.java?rev=926819&r1=926818&r2=926819&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/WidgetWorker.java
(original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/WidgetWorker.java Tue Mar
23 22:34:54 2010
@@ -378,4 +378,34 @@ public class WidgetWorker {
return linkType;
}
}
+
+ /** Returns the script location based on a script combined name:
+ * <code>location#methodName</code>.
+ *
+ * @param combinedName The combined location/method name
+ * @return The script location
+ */
+ public static String getScriptLocation(String combinedName) {
+ int pos = combinedName.lastIndexOf("#");
+ if (pos == -1) {
+ return combinedName;
+ }
+ return combinedName.substring(0, pos);
+ }
+
+ /** Returns the script method name based on a script combined name:
+ * <code>location#methodName</code>. Returns <code>null</code> if
+ * no method name is found.
+ *
+ * @param combinedName The combined location/method name
+ * @return The method name or <code>null</code>
+ */
+ public static String getScriptMethodName(String combinedName) {
+ int pos = combinedName.lastIndexOf("#");
+ if (pos == -1) {
+ return null;
+ }
+ return combinedName.substring(pos + 1);
+ }
+
}
Modified:
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java
URL:
http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java?rev=926819&r1=926818&r2=926819&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java
(original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java
Tue Mar 23 22:34:54 2010
@@ -31,6 +31,9 @@ import java.util.regex.PatternSyntaxExce
import javolution.util.FastList;
import javolution.util.FastMap;
+import groovy.lang.Binding;
+
+import org.codehaus.groovy.runtime.InvokerHelper;
import org.ofbiz.base.util.BshUtil;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
@@ -49,6 +52,7 @@ import org.ofbiz.entity.finder.PrimaryKe
import org.ofbiz.entity.util.EntityListIterator;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.ModelService;
+import org.ofbiz.widget.WidgetWorker;
import org.w3c.dom.Element;
@@ -267,11 +271,15 @@ public abstract class ModelFormAction {
}
public static class Script extends ModelFormAction {
+ protected static final Object[] EMPTY_ARGS = {};
protected String location;
+ protected String method;
public Script(ModelForm modelForm, Element scriptElement) {
super (modelForm, scriptElement);
- this.location = scriptElement.getAttribute("location");
+ String scriptLocation = scriptElement.getAttribute("location");
+ this.location = WidgetWorker.getScriptLocation(scriptLocation);
+ this.method = WidgetWorker.getScriptMethodName(scriptLocation);
}
@Override
@@ -284,9 +292,14 @@ public abstract class ModelFormAction {
Debug.logError(e, errMsg, module);
throw new IllegalArgumentException(errMsg);
}
- } else if (location.endsWith(".groovy")) {
+ } else if (location.contains(".groovy")) {
try {
- GroovyUtil.runScriptAtLocation(location, context);
+ groovy.lang.Script script =
InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), new
Binding(context));
+ if (UtilValidate.isEmpty(method)) {
+ script.run();
+ } else {
+ script.invokeMethod(method, EMPTY_ARGS);
+ }
} catch (GeneralException e) {
String errMsg = "Error running Groovy script at location
[" + location + "]: " + e.toString();
Debug.logError(e, errMsg, module);
Modified:
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java
URL:
http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java?rev=926819&r1=926818&r2=926819&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java
(original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java
Tue Mar 23 22:34:54 2010
@@ -25,16 +25,22 @@ import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSession;
+
import javolution.util.FastList;
import javolution.util.FastMap;
+import groovy.lang.Binding;
+import org.codehaus.groovy.runtime.InvokerHelper;
+
import org.ofbiz.base.util.BshUtil;
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.UtilGenerics;
import org.ofbiz.base.util.UtilFormatOut;
+import org.ofbiz.base.util.UtilGenerics;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.base.util.UtilXml;
@@ -45,10 +51,8 @@ import org.ofbiz.entity.finder.ByConditi
import org.ofbiz.entity.finder.PrimaryKeyFinder;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.ModelService;
-
+import org.ofbiz.widget.WidgetWorker;
import org.w3c.dom.Element;
-import javax.servlet.ServletContext;
-import javax.servlet.http.HttpSession;
/**
@@ -324,11 +328,15 @@ public abstract class ModelMenuAction {
}
public static class Script extends ModelMenuAction {
+ protected static final Object[] EMPTY_ARGS = {};
protected String location;
+ protected String method;
public Script(ModelMenu modelMenu, Element scriptElement) {
super (modelMenu, scriptElement);
- this.location = scriptElement.getAttribute("location");
+ String scriptLocation = scriptElement.getAttribute("location");
+ this.location = WidgetWorker.getScriptLocation(scriptLocation);
+ this.method = WidgetWorker.getScriptMethodName(scriptLocation);
}
@Override
@@ -343,7 +351,12 @@ public abstract class ModelMenuAction {
}
} else if (location.endsWith(".groovy")) {
try {
- GroovyUtil.runScriptAtLocation(location, context);
+ groovy.lang.Script script =
InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), new
Binding(context));
+ if (UtilValidate.isEmpty(method)) {
+ script.run();
+ } else {
+ script.invokeMethod(method, EMPTY_ARGS);
+ }
} catch (GeneralException e) {
String errMsg = "Error running Groovy script at location
[" + location + "]: " + e.toString();
Debug.logError(e, errMsg, module);
Modified:
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ModelScreenAction.java
URL:
http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ModelScreenAction.java?rev=926819&r1=926818&r2=926819&view=diff
==============================================================================
---
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ModelScreenAction.java
(original)
+++
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ModelScreenAction.java
Tue Mar 23 22:34:54 2010
@@ -33,6 +33,9 @@ import javax.servlet.http.HttpSession;
import javolution.util.FastList;
import javolution.util.FastMap;
+import groovy.lang.Binding;
+import org.codehaus.groovy.runtime.InvokerHelper;
+
import org.ofbiz.base.util.BshUtil;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
@@ -58,6 +61,7 @@ import org.ofbiz.minilang.method.MethodC
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.ModelService;
+import org.ofbiz.widget.WidgetWorker;
import org.w3c.dom.Element;
@@ -392,11 +396,15 @@ public abstract class ModelScreenAction
}
public static class Script extends ModelScreenAction {
+ protected static final Object[] EMPTY_ARGS = {};
protected String location;
+ protected String method;
public Script(ModelScreen modelScreen, Element scriptElement) {
super (modelScreen, scriptElement);
- this.location = scriptElement.getAttribute("location");
+ String scriptLocation = scriptElement.getAttribute("location");
+ this.location = WidgetWorker.getScriptLocation(scriptLocation);
+ this.method = WidgetWorker.getScriptMethodName(scriptLocation);
}
@Override
@@ -409,19 +417,22 @@ public abstract class ModelScreenAction
}
} else if (location.endsWith(".groovy")) {
try {
- GroovyUtil.runScriptAtLocation(location, context);
+ groovy.lang.Script script =
InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), new
Binding(context));
+ if (UtilValidate.isEmpty(method)) {
+ script.run();
+ } else {
+ script.invokeMethod(method, EMPTY_ARGS);
+ }
} catch (GeneralException e) {
throw new GeneralException("Error running Groovy script at
location [" + location + "]", e);
}
- } else if (location.contains(".xml#")) {
- String xmlResource =
ScreenFactory.getResourceNameFromCombined(location);
- String methodName =
ScreenFactory.getScreenNameFromCombined(location);
+ } else if (location.endsWith(".xml")) {
Map<String, Object> localContext = FastMap.newInstance();
localContext.putAll(context);
DispatchContext ctx =
this.modelScreen.getDispatcher(context).getDispatchContext();
MethodContext methodContext = new MethodContext(ctx,
localContext, null);
try {
- SimpleMethod.runSimpleMethod(xmlResource, methodName,
methodContext);
+ SimpleMethod.runSimpleMethod(location, method,
methodContext);
context.putAll(methodContext.getResults());
} catch (MiniLangException e) {
throw new GeneralException("Error running simple method at
location [" + location + "]", e);