001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.callops;
019:
020: import java.util.*;
021:
022: import org.w3c.dom.*;
023: import org.ofbiz.base.util.*;
024: import org.ofbiz.minilang.*;
025: import org.ofbiz.minilang.method.*;
026:
027: /**
028: * An operation that calls a simple method in the same, or from another, file
029: */
030: public class CallSimpleMethod extends MethodOperation {
031:
032: public static final String module = CallSimpleMethod.class
033: .getName();
034:
035: String xmlResource;
036: String methodName;
037:
038: public CallSimpleMethod(Element element, SimpleMethod simpleMethod) {
039: super (element, simpleMethod);
040: this .methodName = element.getAttribute("method-name");
041: this .xmlResource = element.getAttribute("xml-resource");
042: }
043:
044: public boolean exec(MethodContext methodContext) {
045: if (this .methodName != null && this .methodName.length() > 0) {
046: String methodName = methodContext
047: .expandString(this .methodName);
048: String xmlResource = methodContext
049: .expandString(this .xmlResource);
050:
051: SimpleMethod simpleMethodToCall = null;
052: if (xmlResource == null || xmlResource.length() == 0) {
053: simpleMethodToCall = this .simpleMethod
054: .getSimpleMethodInSameFile(methodName);
055: } else {
056: Map simpleMethods = null;
057: try {
058: simpleMethods = SimpleMethod.getSimpleMethods(
059: xmlResource, methodName, methodContext
060: .getLoader());
061: } catch (MiniLangException e) {
062: String errMsg = "ERROR: Could not complete the "
063: + simpleMethod.getShortDescription()
064: + " process [error getting methods from resource: "
065: + e.getMessage() + "]";
066: Debug.logError(e, errMsg, module);
067: methodContext.setErrorReturn(errMsg, simpleMethod);
068: return false;
069: }
070:
071: simpleMethodToCall = (SimpleMethod) simpleMethods
072: .get(methodName);
073: }
074:
075: if (simpleMethodToCall == null) {
076: String errMsg = "ERROR: Could not complete the "
077: + simpleMethod.getShortDescription()
078: + " process, could not find SimpleMethod "
079: + methodName + " in XML document in resource: "
080: + xmlResource;
081: methodContext.setErrorReturn(errMsg, simpleMethod);
082: return false;
083: }
084:
085: String returnVal = simpleMethodToCall.exec(methodContext);
086: if (Debug.verboseOn())
087: Debug
088: .logVerbose(
089: "Called inline simple-method named ["
090: + methodName
091: + "] in resource ["
092: + xmlResource
093: + "], returnVal is ["
094: + returnVal + "]", module);
095:
096: if (returnVal != null
097: && returnVal.equals(simpleMethodToCall
098: .getDefaultErrorCode())) {
099: // 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...
100: if (methodContext.getMethodType() == MethodContext.EVENT) {
101: methodContext.putEnv(simpleMethod
102: .getEventResponseCodeName(), simpleMethod
103: .getDefaultErrorCode());
104: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
105: methodContext.putEnv(simpleMethod
106: .getServiceResponseMessageName(),
107: simpleMethod.getDefaultErrorCode());
108: }
109: return false;
110: }
111:
112: // if the response code/meassge is error, if so show the error and return false
113: if (methodContext.getMethodType() == MethodContext.EVENT) {
114: String responseCode = (String) methodContext
115: .getEnv(simpleMethod.getEventResponseCodeName());
116: if (responseCode != null
117: && responseCode.equals(simpleMethod
118: .getDefaultErrorCode())) {
119: Debug.logWarning("Got error ["
120: + responseCode
121: + "] calling inline simple-method named ["
122: + methodName
123: + "] in resource ["
124: + xmlResource
125: + "], message is "
126: + methodContext.getEnv(simpleMethod
127: .getEventErrorMessageName()),
128: module);
129: return false;
130: }
131: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
132: String resonseMessage = (String) methodContext
133: .getEnv(simpleMethod
134: .getServiceResponseMessageName());
135: if (resonseMessage != null
136: && resonseMessage.equals(simpleMethod
137: .getDefaultErrorCode())) {
138: Debug.logWarning("Got error ["
139: + resonseMessage
140: + "] calling inline simple-method named ["
141: + methodName
142: + "] in resource ["
143: + xmlResource
144: + "], message is "
145: + methodContext.getEnv(simpleMethod
146: .getServiceErrorMessageName())
147: + ", and the error message list is: "
148: + methodContext.getEnv(simpleMethod
149: .getServiceErrorMessageListName()),
150: module);
151: return false;
152: }
153: }
154: } else {
155: String errMsg = "ERROR in call-simple-method: methodName was missing; not running simpleMethod";
156: Debug.logError(errMsg, module);
157: methodContext.setErrorReturn(errMsg, simpleMethod);
158: return false;
159: }
160:
161: return true;
162: }
163:
164: public String rawString() {
165: // TODO: something more than the empty tag
166: return "<call-simple-method/>";
167: }
168:
169: public String expandedString(MethodContext methodContext) {
170: // TODO: something more than a stub/dummy
171: return this.rawString();
172: }
173: }
|