use of org.apache.ofbiz.minilang.method.MethodContext in project ofbiz-framework by apache.
the class SimpleMethod method runSimpleService.
public static Map<String, Object> runSimpleService(String xmlResource, String methodName, DispatchContext ctx, Map<String, ? extends Object> context) throws MiniLangException {
MethodContext methodContext = new MethodContext(ctx, context, null);
runSimpleMethod(xmlResource, methodName, methodContext);
return methodContext.getResults();
}
use of org.apache.ofbiz.minilang.method.MethodContext in project ofbiz-framework by apache.
the class SimpleMethod method runSimpleService.
public static Map<String, Object> runSimpleService(URL xmlURL, String methodName, DispatchContext ctx, Map<String, ? extends Object> context, ClassLoader loader) throws MiniLangException {
MethodContext methodContext = new MethodContext(ctx, context, loader);
runSimpleMethod(xmlURL, methodName, methodContext);
return methodContext.getResults();
}
use of org.apache.ofbiz.minilang.method.MethodContext in project ofbiz-framework by apache.
the class CallSimpleMethod method exec.
@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
if (UtilValidate.isEmpty(this.methodName)) {
throw new MiniLangRuntimeException("method-name attribute is empty", this);
}
SimpleMethod simpleMethodToCall = SimpleMethod.getSimpleMethod(this.xmlURL, this.methodName);
if (simpleMethodToCall == null) {
throw new MiniLangRuntimeException("Could not find <simple-method name=\"" + this.methodName + "\"> in XML document " + this.xmlResource, this);
}
MethodContext localContext = methodContext;
if ("function".equals(this.scope)) {
Map<String, Object> localEnv = new HashMap<String, Object>();
localEnv.putAll(methodContext.getEnvMap());
localEnv.remove(this.simpleMethod.getEventResponseCodeName());
localEnv.remove(this.simpleMethod.getServiceResponseMessageName());
localContext = new MethodContext(localEnv, methodContext.getLoader(), methodContext.getMethodType());
}
String returnVal = simpleMethodToCall.exec(localContext);
if (Debug.verboseOn()) {
Debug.logVerbose("Called simple-method named [" + this.methodName + "] in resource [" + this.xmlResource + "], returnVal is [" + returnVal + "]", module);
}
if (simpleMethodToCall.getDefaultErrorCode().equals(returnVal)) {
if (methodContext.getMethodType() == MethodContext.EVENT) {
methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
} else if (methodContext.getMethodType() == MethodContext.SERVICE) {
methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
}
return false;
}
if (methodContext.getMethodType() == MethodContext.EVENT) {
// FIXME: This doesn't make sense. We are comparing the called method's response code with this method's
// response code. Since response codes are configurable per method, this code will fail.
String responseCode = (String) localContext.getEnv(this.simpleMethod.getEventResponseCodeName());
if (this.simpleMethod.getDefaultErrorCode().equals(responseCode)) {
Debug.logWarning("Got error [" + responseCode + "] calling inline simple-method named [" + this.methodName + "] in resource [" + this.xmlResource + "], message is " + methodContext.getEnv(this.simpleMethod.getEventErrorMessageName()), module);
return false;
}
} else if (methodContext.getMethodType() == MethodContext.SERVICE) {
// FIXME: This doesn't make sense. We are comparing the called method's response message with this method's
// response message. Since response messages are configurable per method, this code will fail.
String responseMessage = (String) localContext.getEnv(this.simpleMethod.getServiceResponseMessageName());
if (this.simpleMethod.getDefaultErrorCode().equals(responseMessage)) {
Debug.logWarning("Got error [" + responseMessage + "] calling inline simple-method named [" + this.methodName + "] in resource [" + this.xmlResource + "], message is " + methodContext.getEnv(this.simpleMethod.getServiceErrorMessageName()) + ", and the error message list is: " + methodContext.getEnv(this.simpleMethod.getServiceErrorMessageListName()), module);
return false;
}
}
if ("function".equals(this.scope) && this.resultToFieldList != null) {
Map<String, Object> results = localContext.getResults();
if (results != null) {
for (ResultToField resultToField : this.resultToFieldList) {
resultToField.exec(methodContext.getEnvMap(), results);
}
}
}
return true;
}
use of org.apache.ofbiz.minilang.method.MethodContext in project ofbiz-framework by apache.
the class MiniLangTests method createServiceMethodContext.
private MethodContext createServiceMethodContext() {
MethodContext context = new MethodContext(dispatcher.getDispatchContext(), createContext(), null);
context.setUserLogin(dispatcher.getDelegator().makeValidValue("UserLogin", UtilMisc.toMap("userLoginId", "system")), "userLogin");
if (traceEnabled) {
context.setTraceOn(Debug.INFO);
}
return context;
}
use of org.apache.ofbiz.minilang.method.MethodContext in project ofbiz-framework by apache.
the class MiniLangTests method testAssignmentOperators.
public void testAssignmentOperators() throws Exception {
// <check-errors> and <add-error> tests
SimpleMethod methodToTest = createSimpleMethod("<simple-method name=\"testCheckErrors\"><check-errors/></simple-method>");
MethodContext context = createServiceMethodContext();
String result = methodToTest.exec(context);
assertEquals("<check-errors> success result", methodToTest.getDefaultSuccessCode(), result);
List<String> messages = context.getEnv(methodToTest.getServiceErrorMessageListName());
assertNull("<check-errors> null error message list", messages);
methodToTest = createSimpleMethod("<simple-method name=\"testCheckErrors\"><add-error><fail-message message=\"This should fail\"/></add-error><check-errors/></simple-method>");
context = createServiceMethodContext();
result = methodToTest.exec(context);
assertEquals("<check-errors> error result", methodToTest.getDefaultErrorCode(), result);
messages = context.getEnv(methodToTest.getServiceErrorMessageListName());
assertNotNull("<check-errors> error message list", messages);
assertTrue("<check-errors> error message text", messages.contains("This should fail"));
// <assert>, <not>, and <if-empty> tests
methodToTest = createSimpleMethod("<simple-method name=\"testAssert\"><assert><not><if-empty field=\"locale\"/></not></assert><check-errors/></simple-method>");
context = createServiceMethodContext();
result = methodToTest.exec(context);
assertEquals("<assert> success result", methodToTest.getDefaultSuccessCode(), result);
messages = context.getEnv(methodToTest.getServiceErrorMessageListName());
assertNull("<assert> null error message list", messages);
methodToTest = createSimpleMethod("<simple-method name=\"testAssert\"><assert><if-empty field=\"locale\"/></assert><check-errors/></simple-method>");
context = createServiceMethodContext();
result = methodToTest.exec(context);
assertEquals("<assert> error result", methodToTest.getDefaultErrorCode(), result);
messages = context.getEnv(methodToTest.getServiceErrorMessageListName());
assertNotNull("<assert> error message list", messages);
String errorMessage = messages.get(0);
assertTrue("<assert> error message text", errorMessage.startsWith("Assertion failed:"));
}
Aggregations