001: /*
002: * $Id: CallSimpleMethod.java,v 1.2 2003/09/14 05:40:41 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.callops;
025:
026: import java.util.*;
027:
028: import org.w3c.dom.*;
029: import org.ofbiz.base.util.*;
030: import org.ofbiz.minilang.*;
031: import org.ofbiz.minilang.method.*;
032:
033: /**
034: * An operation that calls a simple method in the same, or from another, file
035: *
036: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
037: * @version $Revision: 1.2 $
038: * @since 2.0
039: */
040: public class CallSimpleMethod extends MethodOperation {
041:
042: public static final String module = CallSimpleMethod.class
043: .getName();
044:
045: String xmlResource;
046: String methodName;
047:
048: public CallSimpleMethod(Element element, SimpleMethod simpleMethod) {
049: super (element, simpleMethod);
050: this .methodName = element.getAttribute("method-name");
051: this .xmlResource = element.getAttribute("xml-resource");
052: }
053:
054: public boolean exec(MethodContext methodContext) {
055: if (this .methodName != null && this .methodName.length() > 0) {
056: String methodName = methodContext
057: .expandString(this .methodName);
058: String xmlResource = methodContext
059: .expandString(this .xmlResource);
060:
061: SimpleMethod simpleMethodToCall = null;
062: if (xmlResource == null || xmlResource.length() == 0) {
063: simpleMethodToCall = this .simpleMethod
064: .getSimpleMethodInSameFile(methodName);
065: } else {
066: Map simpleMethods = null;
067: try {
068: simpleMethods = SimpleMethod.getSimpleMethods(
069: xmlResource, methodName, methodContext
070: .getLoader());
071: } catch (MiniLangException e) {
072: String errMsg = "ERROR: Could not complete the "
073: + simpleMethod.getShortDescription()
074: + " process [error getting methods from resource: "
075: + e.getMessage() + "]";
076: Debug.logError(e, errMsg, module);
077: methodContext.setErrorReturn(errMsg, simpleMethod);
078: return false;
079: }
080:
081: simpleMethodToCall = (SimpleMethod) simpleMethods
082: .get(methodName);
083: }
084:
085: if (simpleMethodToCall == null) {
086: String errMsg = "ERROR: Could not complete the "
087: + simpleMethod.getShortDescription()
088: + " process, could not find SimpleMethod "
089: + methodName + " in XML document in resource: "
090: + xmlResource;
091: methodContext.setErrorReturn(errMsg, simpleMethod);
092: return false;
093: }
094:
095: String returnVal = simpleMethodToCall.exec(methodContext);
096: if (Debug.verboseOn())
097: Debug
098: .logVerbose(
099: "Called inline simple-method named ["
100: + methodName
101: + "] in resource ["
102: + xmlResource
103: + "], returnVal is ["
104: + returnVal + "]", module);
105:
106: if (returnVal != null
107: && returnVal.equals(simpleMethodToCall
108: .getDefaultErrorCode())) {
109: // in this case just set the error code just in case it hasn't already been set, the error messages will already be in place...
110: if (methodContext.getMethodType() == MethodContext.EVENT) {
111: methodContext.putEnv(simpleMethod
112: .getEventResponseCodeName(), simpleMethod
113: .getDefaultErrorCode());
114: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
115: methodContext.putEnv(simpleMethod
116: .getServiceResponseMessageName(),
117: simpleMethod.getDefaultErrorCode());
118: }
119: return false;
120: }
121:
122: // if the response code/meassge is error, if so show the error and return false
123: if (methodContext.getMethodType() == MethodContext.EVENT) {
124: String responseCode = (String) methodContext
125: .getEnv(simpleMethod.getEventResponseCodeName());
126: if (responseCode != null
127: && responseCode.equals(simpleMethod
128: .getDefaultErrorCode())) {
129: Debug.logWarning("Got error ["
130: + responseCode
131: + "] calling inline simple-method named ["
132: + methodName
133: + "] in resource ["
134: + xmlResource
135: + "], message is "
136: + methodContext.getEnv(simpleMethod
137: .getEventErrorMessageName()),
138: module);
139: return false;
140: }
141: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
142: String resonseMessage = (String) methodContext
143: .getEnv(simpleMethod
144: .getServiceResponseMessageName());
145: if (resonseMessage != null
146: && resonseMessage.equals(simpleMethod
147: .getDefaultErrorCode())) {
148: Debug.logWarning("Got error ["
149: + resonseMessage
150: + "] calling inline simple-method named ["
151: + methodName
152: + "] in resource ["
153: + xmlResource
154: + "], message is "
155: + methodContext.getEnv(simpleMethod
156: .getServiceErrorMessageName())
157: + ", and the error message list is: "
158: + methodContext.getEnv(simpleMethod
159: .getServiceErrorMessageListName()),
160: module);
161: return false;
162: }
163: }
164: } else {
165: String errMsg = "ERROR in call-simple-method: methodName was missing; not running simpleMethod";
166: Debug.logError(errMsg, module);
167: methodContext.setErrorReturn(errMsg, simpleMethod);
168: return false;
169: }
170:
171: return true;
172: }
173: }
|