KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > callops > CallSimpleMethod


1 /*
2  * $Id: CallSimpleMethod.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.callops;
25
26 import java.util.*;
27
28 import org.w3c.dom.*;
29 import org.ofbiz.base.util.*;
30 import org.ofbiz.minilang.*;
31 import org.ofbiz.minilang.method.*;
32
33 /**
34  * An operation that calls a simple method in the same, or from another, file
35  *
36  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
37  * @version $Rev: 5462 $
38  * @since 2.0
39  */

40 public class CallSimpleMethod extends MethodOperation {
41     
42     public static final String JavaDoc module = CallSimpleMethod.class.getName();
43     
44     String JavaDoc xmlResource;
45     String JavaDoc methodName;
46
47     public CallSimpleMethod(Element element, SimpleMethod simpleMethod) {
48         super(element, simpleMethod);
49         this.methodName = element.getAttribute("method-name");
50         this.xmlResource = element.getAttribute("xml-resource");
51     }
52
53     public boolean exec(MethodContext methodContext) {
54         if (this.methodName != null && this.methodName.length() > 0) {
55             String JavaDoc methodName = methodContext.expandString(this.methodName);
56             String JavaDoc xmlResource = methodContext.expandString(this.xmlResource);
57
58             SimpleMethod simpleMethodToCall = null;
59             if (xmlResource == null || xmlResource.length() == 0) {
60                 simpleMethodToCall = this.simpleMethod.getSimpleMethodInSameFile(methodName);
61             } else {
62                 Map simpleMethods = null;
63                 try {
64                     simpleMethods = SimpleMethod.getSimpleMethods(xmlResource, methodName, methodContext.getLoader());
65                 } catch (MiniLangException e) {
66                     String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [error getting methods from resource: " + e.getMessage() + "]";
67                     Debug.logError(e, errMsg, module);
68                     methodContext.setErrorReturn(errMsg, simpleMethod);
69                     return false;
70                 }
71
72                 simpleMethodToCall = (SimpleMethod) simpleMethods.get(methodName);
73             }
74
75             if (simpleMethodToCall == null) {
76                 String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process, could not find SimpleMethod " + methodName + " in XML document in resource: " + xmlResource;
77                 methodContext.setErrorReturn(errMsg, simpleMethod);
78                 return false;
79             }
80             
81             String JavaDoc returnVal = simpleMethodToCall.exec(methodContext);
82             if (Debug.verboseOn()) Debug.logVerbose("Called inline simple-method named [" + methodName + "] in resource [" + xmlResource + "], returnVal is [" + returnVal + "]", module);
83             
84             if (returnVal != null && returnVal.equals(simpleMethodToCall.getDefaultErrorCode())) {
85                 // 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...
86
if (methodContext.getMethodType() == MethodContext.EVENT) {
87                     methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
88                 } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
89                     methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
90                 }
91                 return false;
92             }
93             
94             // if the response code/meassge is error, if so show the error and return false
95
if (methodContext.getMethodType() == MethodContext.EVENT) {
96                 String JavaDoc responseCode = (String JavaDoc) methodContext.getEnv(simpleMethod.getEventResponseCodeName());
97                 if (responseCode != null && responseCode.equals(simpleMethod.getDefaultErrorCode())) {
98                     Debug.logWarning("Got error [" + responseCode + "] calling inline simple-method named [" + methodName + "] in resource [" + xmlResource + "], message is " + methodContext.getEnv(simpleMethod.getEventErrorMessageName()) , module);
99                     return false;
100                 }
101             } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
102                 String JavaDoc resonseMessage = (String JavaDoc) methodContext.getEnv(simpleMethod.getServiceResponseMessageName());
103                 if (resonseMessage != null && resonseMessage.equals(simpleMethod.getDefaultErrorCode())) {
104                     Debug.logWarning("Got error [" + resonseMessage + "] calling inline simple-method named [" + methodName + "] in resource [" + xmlResource + "], message is " + methodContext.getEnv(simpleMethod.getServiceErrorMessageName()) + ", and the error message list is: " + methodContext.getEnv(simpleMethod.getServiceErrorMessageListName()), module);
105                     return false;
106                 }
107             }
108         } else {
109             String JavaDoc errMsg = "ERROR in call-simple-method: methodName was missing; not running simpleMethod";
110             Debug.logError(errMsg, module);
111             methodContext.setErrorReturn(errMsg, simpleMethod);
112             return false;
113         }
114
115         return true;
116     }
117
118     public String JavaDoc rawString() {
119         // TODO: something more than the empty tag
120
return "<call-simple-method/>";
121     }
122     public String JavaDoc expandedString(MethodContext methodContext) {
123         // TODO: something more than a stub/dummy
124
return this.rawString();
125     }
126 }
127
Popular Tags