|
Author: adrianc
Date: Sat May 5 09:25:03 2012 New Revision: 1334358 URL: http://svn.apache.org/viewvc?rev=1334358&view=rev Log: More work on the Mini-language <simple-method> element: moved convenience methods from MethodOperation.java to SimpleMethod.java - so all sub-elements can use them (not just operations elements), simplified some code. Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallBsh.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/SetOperation.java Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java?rev=1334358&r1=1334357&r2=1334358&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java Sat May 5 09:25:03 2012 @@ -324,8 +324,8 @@ public final class SimpleMethod { } this.parentSimpleMethodsMap = parentSimpleMethodsMap; this.fromLocation = fromLocation; - this.methodName = simpleMethodElement.getAttribute("method-name"); - this.shortDescription = simpleMethodElement.getAttribute("short-description"); + methodName = simpleMethodElement.getAttribute("method-name"); + shortDescription = simpleMethodElement.getAttribute("short-description"); defaultErrorCode = UtilXml.elementAttribute(simpleMethodElement, "default-error-code", "error"); defaultSuccessCode = UtilXml.elementAttribute(simpleMethodElement, "default-success-code", "success"); parameterMapName = UtilXml.elementAttribute(simpleMethodElement, "parameter-map-name", "parameters"); @@ -350,7 +350,26 @@ public final class SimpleMethod { securityName = UtilXml.elementAttribute(simpleMethodElement, "security-name", "security"); dispatcherName = UtilXml.elementAttribute(simpleMethodElement, "dispatcher-name", "dispatcher"); userLoginName = UtilXml.elementAttribute(simpleMethodElement, "user-login-name", "userLogin"); - this.methodOperations = Collections.unmodifiableList(readOperations(simpleMethodElement, this)); + methodOperations = Collections.unmodifiableList(readOperations(simpleMethodElement, this)); + } + + public void addErrorMessage(MethodContext methodContext, String message) { + String messageListName = methodContext.getMethodType() == MethodContext.EVENT ? getEventErrorMessageListName() : getServiceErrorMessageListName(); + addMessage(methodContext, messageListName, message); + } + + public void addMessage(MethodContext methodContext, String message) { + String messageListName = methodContext.getMethodType() == MethodContext.EVENT ? getEventEventMessageListName() : getServiceSuccessMessageListName(); + addMessage(methodContext, messageListName, message); + } + + private void addMessage(MethodContext methodContext, String messageListName, String message) { + List<String> messages = methodContext.getEnv(messageListName); + if (messages == null) { + messages = FastList.newInstance(); + methodContext.putEnv(messageListName, messages); + } + messages.add(message); } /** Execute the Simple Method operations */ @@ -379,15 +398,7 @@ public final class SimpleMethod { if (userLogin == null) { Map<String, Object> messageMap = UtilMisc.<String, Object> toMap("shortDescription", shortDescription); String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.must_logged_process", messageMap, locale) + "."; - - if (methodContext.getMethodType() == MethodContext.EVENT) { - methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errMsg); - return defaultErrorCode; - } else if (methodContext.getMethodType() == MethodContext.SERVICE) { - methodContext.putResult(ModelService.ERROR_MESSAGE, errMsg); - methodContext.putResult(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); - return null; - } + return returnError(methodContext, errMsg); } } // if using transaction, try to start here @@ -397,16 +408,8 @@ public final class SimpleMethod { beganTransaction = TransactionUtil.begin(); } catch (GenericTransactionException e) { String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.error_begin_transaction", locale) + ": " + e.getMessage(); - Debug.logWarning(errMsg, module); - Debug.logWarning(e, module); - if (methodContext.getMethodType() == MethodContext.EVENT) { - methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errMsg); - return defaultErrorCode; - } else if (methodContext.getMethodType() == MethodContext.SERVICE) { - methodContext.putResult(ModelService.ERROR_MESSAGE, errMsg); - methodContext.putResult(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); - return null; - } + Debug.logWarning(e, errMsg, module); + return returnError(methodContext, errMsg); } } // declare errorMsg here just in case transaction ops fail @@ -417,7 +420,7 @@ public final class SimpleMethod { } catch (Throwable t) { // make SURE nothing gets thrown through String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.error_running", locale) + ": " + t.getMessage(); - Debug.logError(errMsg, module); + Debug.logWarning(t, errMsg, module); finished = false; errorMsg += errMsg; } @@ -462,7 +465,7 @@ public final class SimpleMethod { response = null; } returnValue = response; - } else if (methodContext.getMethodType() == MethodContext.SERVICE) { + } else { boolean forceError = false; String tempErrorMsg = (String) methodContext.getEnv(serviceErrorMessageName); if (errorMsg.length() > 0 || UtilValidate.isNotEmpty(tempErrorMsg)) { @@ -506,9 +509,6 @@ public final class SimpleMethod { } methodContext.putResult(ModelService.RESPONSE_MESSAGE, response); returnValue = null; - } else { - response = defaultSuccessCode; - returnValue = defaultSuccessCode; } // decide whether or not to commit based on the response message, ie only rollback if error is returned and not finished boolean doCommit = true; @@ -665,4 +665,14 @@ public final class SimpleMethod { public boolean getUseTransaction() { return this.useTransaction; } + + private String returnError(MethodContext methodContext, String errorMsg) { + if (methodContext.getMethodType() == MethodContext.EVENT) { + methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errorMsg); + } else { + methodContext.putResult(ModelService.ERROR_MESSAGE, errorMsg); + methodContext.putResult(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); + } + return defaultErrorCode; + } } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java?rev=1334358&r1=1334357&r2=1334358&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java Sat May 5 09:25:03 2012 @@ -22,9 +22,6 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import java.util.List; - -import javolution.util.FastList; import org.ofbiz.minilang.MiniLangElement; import org.ofbiz.minilang.MiniLangException; @@ -41,25 +38,6 @@ public abstract class MethodOperation ex super(element, simpleMethod); } - public void addErrorMessage(MethodContext methodContext, String message) { - String messageListName = methodContext.getMethodType() == MethodContext.EVENT ? this.simpleMethod.getEventErrorMessageListName() : this.simpleMethod.getServiceErrorMessageListName(); - addMessage(methodContext, messageListName, message); - } - - public void addMessage(MethodContext methodContext, String message) { - String messageListName = methodContext.getMethodType() == MethodContext.EVENT ? this.simpleMethod.getEventEventMessageListName() : this.simpleMethod.getServiceSuccessMessageListName(); - addMessage(methodContext, messageListName, message); - } - - private void addMessage(MethodContext methodContext, String messageListName, String message) { - List<String> messages = methodContext.getEnv(messageListName); - if (messages == null) { - messages = FastList.newInstance(); - methodContext.putEnv(messageListName, messages); - } - messages.add(message); - } - /** Execute the operation. Returns false if no further operations should be executed. * @throws MiniLangException */ public abstract boolean exec(MethodContext methodContext) throws MiniLangException; Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallBsh.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallBsh.java?rev=1334358&r1=1334357&r2=1334358&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallBsh.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallBsh.java Sat May 5 09:25:03 2012 @@ -91,7 +91,7 @@ public final class CallBsh extends Metho if (UtilValidate.isNotEmpty(this.resource)) { InputStream is = methodContext.getLoader().getResourceAsStream(this.resource); if (is == null) { - addErrorMessage(methodContext, "Could not find bsh resource: " + this.resource); + this.simpleMethod.addErrorMessage(methodContext, "Could not find bsh resource: " + this.resource); } else { BufferedReader reader = null; try { @@ -108,13 +108,13 @@ public final class CallBsh extends Metho methodContext.putAllEnv(UtilGenerics.<String, Object> checkMap(resourceResult)); } } catch (IOException e) { - addErrorMessage(methodContext, "IO error loading bsh resource: " + e.getMessage()); + this.simpleMethod.addErrorMessage(methodContext, "IO error loading bsh resource: " + e.getMessage()); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { - addErrorMessage(methodContext, "IO error closing BufferedReader: " + e.getMessage()); + this.simpleMethod.addErrorMessage(methodContext, "IO error closing BufferedReader: " + e.getMessage()); } } } @@ -132,7 +132,7 @@ public final class CallBsh extends Metho } } catch (EvalError e) { Debug.logWarning(e, "BeanShell execution caused an error", module); - addErrorMessage(methodContext, "BeanShell execution caused an error: " + e.getMessage()); + this.simpleMethod.addErrorMessage(methodContext, "BeanShell execution caused an error: " + e.getMessage()); } // always return true, error messages just go on the error list return true; 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=1334358&r1=1334357&r2=1334358&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 Sat May 5 09:25:03 2012 @@ -169,7 +169,7 @@ public final class SetOperation extends } catch (GeneralException e) { String errMsg = "Could not convert field value for the field: [" + this.fieldFma.toString() + "] to the [" + this.type + "] type for the value [" + newValue + "]: " + e.toString(); Debug.logWarning(e, errMsg, module); - addErrorMessage(methodContext, errMsg); + this.simpleMethod.addErrorMessage(methodContext, errMsg); return false; } } |
| Free forum by Nabble | Edit this page |
