0001: /*
0002: * $Id: SimpleMethod.java,v 1.5 2003/11/23 11:57:09 jonesde Exp $
0003: *
0004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
0005: *
0006: * Permission is hereby granted, free of charge, to any person obtaining a
0007: * copy of this software and associated documentation files (the "Software"),
0008: * to deal in the Software without restriction, including without limitation
0009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0010: * and/or sell copies of the Software, and to permit persons to whom the
0011: * Software is furnished to do so, subject to the following conditions:
0012: *
0013: * The above copyright notice and this permission notice shall be included
0014: * in all copies or substantial portions of the Software.
0015: *
0016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
0021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
0022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0023: */
0024: package org.ofbiz.minilang;
0025:
0026: import java.net.URL;
0027: import java.util.HashMap;
0028: import java.util.Iterator;
0029: import java.util.LinkedList;
0030: import java.util.List;
0031: import java.util.Map;
0032:
0033: import javax.servlet.http.HttpServletRequest;
0034: import javax.servlet.http.HttpServletResponse;
0035:
0036: import org.ofbiz.base.util.Debug;
0037: import org.ofbiz.base.util.UtilCache;
0038: import org.ofbiz.base.util.UtilURL;
0039: import org.ofbiz.base.util.UtilXml;
0040: import org.ofbiz.entity.GenericValue;
0041: import org.ofbiz.entity.transaction.GenericTransactionException;
0042: import org.ofbiz.entity.transaction.TransactionUtil;
0043: import org.ofbiz.minilang.method.MethodContext;
0044: import org.ofbiz.minilang.method.MethodOperation;
0045: import org.ofbiz.service.DispatchContext;
0046: import org.ofbiz.service.ModelService;
0047: import org.w3c.dom.Document;
0048: import org.w3c.dom.Element;
0049:
0050: /**
0051: * SimpleMethod Mini Language Core Object
0052: *
0053: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
0054: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
0055: * @version $Revision: 1.5 $
0056: * @since 2.0
0057: */
0058: public class SimpleMethod {
0059:
0060: public static final String module = SimpleMethod.class.getName();
0061:
0062: protected static UtilCache simpleMethodsDirectCache = new UtilCache(
0063: "minilang.SimpleMethodsDirect", 0, 0);
0064: protected static UtilCache simpleMethodsResourceCache = new UtilCache(
0065: "minilang.SimpleMethodsResource", 0, 0);
0066: protected static UtilCache simpleMethodsURLCache = new UtilCache(
0067: "minilang.SimpleMethodsURL", 0, 0);
0068:
0069: // ----- Event Context Invokers -----
0070:
0071: public static String runSimpleEvent(String xmlResource,
0072: String methodName, HttpServletRequest request,
0073: HttpServletResponse response) throws MiniLangException {
0074: return runSimpleMethod(xmlResource, methodName,
0075: new MethodContext(request, response, null));
0076: }
0077:
0078: public static String runSimpleEvent(String xmlResource,
0079: String methodName, HttpServletRequest request,
0080: HttpServletResponse response, ClassLoader loader)
0081: throws MiniLangException {
0082: return runSimpleMethod(xmlResource, methodName,
0083: new MethodContext(request, response, loader));
0084: }
0085:
0086: public static String runSimpleEvent(URL xmlURL, String methodName,
0087: HttpServletRequest request, HttpServletResponse response,
0088: ClassLoader loader) throws MiniLangException {
0089: return runSimpleMethod(xmlURL, methodName, new MethodContext(
0090: request, response, loader));
0091: }
0092:
0093: // ----- Service Context Invokers -----
0094:
0095: public static Map runSimpleService(String xmlResource,
0096: String methodName, DispatchContext ctx, Map context)
0097: throws MiniLangException {
0098: MethodContext methodContext = new MethodContext(ctx, context,
0099: null);
0100:
0101: runSimpleMethod(xmlResource, methodName, methodContext);
0102: return methodContext.getResults();
0103: }
0104:
0105: public static Map runSimpleService(String xmlResource,
0106: String methodName, DispatchContext ctx, Map context,
0107: ClassLoader loader) throws MiniLangException {
0108: MethodContext methodContext = new MethodContext(ctx, context,
0109: loader);
0110:
0111: runSimpleMethod(xmlResource, methodName, methodContext);
0112: return methodContext.getResults();
0113: }
0114:
0115: public static Map runSimpleService(URL xmlURL, String methodName,
0116: DispatchContext ctx, Map context, ClassLoader loader)
0117: throws MiniLangException {
0118: MethodContext methodContext = new MethodContext(ctx, context,
0119: loader);
0120:
0121: runSimpleMethod(xmlURL, methodName, methodContext);
0122: return methodContext.getResults();
0123: }
0124:
0125: // ----- General Method Invokers -----
0126:
0127: public static String runSimpleMethod(String xmlResource,
0128: String methodName, MethodContext methodContext)
0129: throws MiniLangException {
0130: Map simpleMethods = getSimpleMethods(xmlResource, methodName,
0131: methodContext.getLoader());
0132: SimpleMethod simpleMethod = (SimpleMethod) simpleMethods
0133: .get(methodName);
0134:
0135: if (simpleMethod == null) {
0136: throw new MiniLangException("Could not find SimpleMethod "
0137: + methodName + " in XML document in resource: "
0138: + xmlResource);
0139: }
0140: return simpleMethod.exec(methodContext);
0141: }
0142:
0143: public static String runSimpleMethod(URL xmlURL, String methodName,
0144: MethodContext methodContext) throws MiniLangException {
0145: Map simpleMethods = getSimpleMethods(xmlURL, methodName);
0146: SimpleMethod simpleMethod = (SimpleMethod) simpleMethods
0147: .get(methodName);
0148:
0149: if (simpleMethod == null) {
0150: throw new MiniLangException("Could not find SimpleMethod "
0151: + methodName + " in XML document from URL: "
0152: + xmlURL.toString());
0153: }
0154: return simpleMethod.exec(methodContext);
0155: }
0156:
0157: public static Map getSimpleMethods(String xmlResource,
0158: String methodName, ClassLoader loader)
0159: throws MiniLangException {
0160: Map simpleMethods = (Map) simpleMethodsResourceCache
0161: .get(xmlResource);
0162:
0163: if (simpleMethods == null) {
0164: synchronized (SimpleMethod.class) {
0165: simpleMethods = (Map) simpleMethodsResourceCache
0166: .get(xmlResource);
0167: if (simpleMethods == null) {
0168: URL xmlURL = UtilURL.fromResource(xmlResource,
0169: loader);
0170:
0171: if (xmlURL == null) {
0172: throw new MiniLangException(
0173: "Could not find SimpleMethod XML document in resource: "
0174: + xmlResource);
0175: }
0176: simpleMethods = getAllSimpleMethods(xmlURL);
0177:
0178: // put it in the cache
0179: simpleMethodsResourceCache.put(xmlResource,
0180: simpleMethods);
0181: }
0182: }
0183: }
0184:
0185: return simpleMethods;
0186: }
0187:
0188: public static Map getSimpleMethods(URL xmlURL, String methodName)
0189: throws MiniLangException {
0190: Map simpleMethods = (Map) simpleMethodsURLCache.get(xmlURL);
0191:
0192: if (simpleMethods == null) {
0193: synchronized (SimpleMethod.class) {
0194: simpleMethods = (Map) simpleMethodsURLCache.get(xmlURL);
0195: if (simpleMethods == null) {
0196: simpleMethods = getAllSimpleMethods(xmlURL);
0197:
0198: // put it in the cache
0199: simpleMethodsURLCache.put(xmlURL, simpleMethods);
0200: }
0201: }
0202: }
0203:
0204: return simpleMethods;
0205: }
0206:
0207: protected static Map getAllSimpleMethods(URL xmlURL)
0208: throws MiniLangException {
0209: Map simpleMethods = new HashMap();
0210:
0211: // read in the file
0212: Document document = null;
0213:
0214: try {
0215: document = UtilXml.readXmlDocument(xmlURL, true);
0216: } catch (java.io.IOException e) {
0217: throw new MiniLangException("Could not read XML file", e);
0218: } catch (org.xml.sax.SAXException e) {
0219: throw new MiniLangException("Could not parse XML file", e);
0220: } catch (javax.xml.parsers.ParserConfigurationException e) {
0221: throw new MiniLangException(
0222: "XML parser not setup correctly", e);
0223: }
0224:
0225: if (document == null) {
0226: throw new MiniLangException(
0227: "Could not find SimpleMethod XML document: "
0228: + xmlURL.toString());
0229: }
0230:
0231: Element rootElement = document.getDocumentElement();
0232: List simpleMethodElements = UtilXml.childElementList(
0233: rootElement, "simple-method");
0234:
0235: Iterator simpleMethodIter = simpleMethodElements.iterator();
0236:
0237: while (simpleMethodIter.hasNext()) {
0238: Element simpleMethodElement = (Element) simpleMethodIter
0239: .next();
0240: SimpleMethod simpleMethod = new SimpleMethod(
0241: simpleMethodElement, simpleMethods);
0242: simpleMethods.put(simpleMethod.getMethodName(),
0243: simpleMethod);
0244: }
0245:
0246: return simpleMethods;
0247: }
0248:
0249: public static Map getDirectSimpleMethods(String name, String content)
0250: throws MiniLangException {
0251: Map simpleMethods = (Map) simpleMethodsDirectCache.get(name);
0252:
0253: if (simpleMethods == null) {
0254: synchronized (SimpleMethod.class) {
0255: simpleMethods = (Map) simpleMethodsDirectCache
0256: .get(name);
0257: if (simpleMethods == null) {
0258: simpleMethods = getAllDirectSimpleMethods(name,
0259: content);
0260:
0261: // put it in the cache
0262: simpleMethodsDirectCache.put(name, simpleMethods);
0263: }
0264: }
0265: }
0266:
0267: return simpleMethods;
0268: }
0269:
0270: protected static Map getAllDirectSimpleMethods(String name,
0271: String content) throws MiniLangException {
0272: Map simpleMethods = new HashMap();
0273:
0274: // read in the file
0275: Document document = null;
0276:
0277: try {
0278: if (content != null) {
0279: document = UtilXml.readXmlDocument(content, true);
0280: }
0281: } catch (java.io.IOException e) {
0282: throw new MiniLangException("Could not read XML content", e);
0283: } catch (org.xml.sax.SAXException e) {
0284: throw new MiniLangException(
0285: "Could not parse direct XML content", e);
0286: } catch (javax.xml.parsers.ParserConfigurationException e) {
0287: throw new MiniLangException(
0288: "XML parser not setup correctly", e);
0289: }
0290:
0291: if (document == null) {
0292: throw new MiniLangException(
0293: "Could not load SimpleMethod XML document: " + name);
0294: }
0295:
0296: Element rootElement = document.getDocumentElement();
0297: List simpleMethodElements = UtilXml.childElementList(
0298: rootElement, "simple-method");
0299:
0300: Iterator simpleMethodIter = simpleMethodElements.iterator();
0301:
0302: while (simpleMethodIter.hasNext()) {
0303: Element simpleMethodElement = (Element) simpleMethodIter
0304: .next();
0305: SimpleMethod simpleMethod = new SimpleMethod(
0306: simpleMethodElement, simpleMethods);
0307: simpleMethods.put(simpleMethod.getMethodName(),
0308: simpleMethod);
0309: }
0310:
0311: return simpleMethods;
0312: }
0313:
0314: // Member fields begin here...
0315: protected List methodOperations = new LinkedList();
0316: protected Map parentSimpleMethodsMap;
0317: protected String methodName;
0318: protected String shortDescription;
0319: protected String defaultErrorCode;
0320: protected String defaultSuccessCode;
0321:
0322: protected String parameterMapName;
0323:
0324: // event fields
0325: protected String eventRequestName;
0326: protected String eventResponseName;
0327: protected String eventResponseCodeName;
0328: protected String eventErrorMessageName;
0329: protected String eventEventMessageName;
0330:
0331: // service fields
0332: protected String serviceResponseMessageName;
0333: protected String serviceErrorMessageName;
0334: protected String serviceErrorMessageListName;
0335: protected String serviceErrorMessageMapName;
0336: protected String serviceSuccessMessageName;
0337: protected String serviceSuccessMessageListName;
0338:
0339: protected boolean loginRequired = true;
0340: protected boolean useTransaction = true;
0341:
0342: protected String localeName;
0343: protected String delegatorName;
0344: protected String securityName;
0345: protected String dispatcherName;
0346: protected String userLoginName;
0347:
0348: public SimpleMethod(Element simpleMethodElement,
0349: Map parentSimpleMethodsMap) {
0350: this .parentSimpleMethodsMap = parentSimpleMethodsMap;
0351: this .methodName = simpleMethodElement
0352: .getAttribute("method-name");
0353: this .shortDescription = simpleMethodElement
0354: .getAttribute("short-description");
0355:
0356: defaultErrorCode = simpleMethodElement
0357: .getAttribute("default-error-code");
0358: if (defaultErrorCode == null || defaultErrorCode.length() == 0) {
0359: defaultErrorCode = "error";
0360: }
0361: defaultSuccessCode = simpleMethodElement
0362: .getAttribute("default-success-code");
0363: if (defaultSuccessCode == null
0364: || defaultSuccessCode.length() == 0) {
0365: defaultSuccessCode = "success";
0366: }
0367:
0368: parameterMapName = simpleMethodElement
0369: .getAttribute("parameter-map-name");
0370: if (parameterMapName == null || parameterMapName.length() == 0) {
0371: parameterMapName = "parameters";
0372: }
0373:
0374: eventRequestName = simpleMethodElement
0375: .getAttribute("event-request-object-name");
0376: if (eventRequestName == null || eventRequestName.length() == 0) {
0377: eventRequestName = "request";
0378: }
0379: eventResponseName = simpleMethodElement
0380: .getAttribute("event-response-object-name");
0381: if (eventResponseName == null
0382: || eventResponseName.length() == 0) {
0383: eventResponseName = "response";
0384: }
0385: eventResponseCodeName = simpleMethodElement
0386: .getAttribute("event-response-code-name");
0387: if (eventResponseCodeName == null
0388: || eventResponseCodeName.length() == 0) {
0389: eventResponseCodeName = "_response_code_";
0390: }
0391: eventErrorMessageName = simpleMethodElement
0392: .getAttribute("event-error-message-name");
0393: if (eventErrorMessageName == null
0394: || eventErrorMessageName.length() == 0) {
0395: eventErrorMessageName = "_error_message_";
0396: }
0397: eventEventMessageName = simpleMethodElement
0398: .getAttribute("event-event-message-name");
0399: if (eventEventMessageName == null
0400: || eventEventMessageName.length() == 0) {
0401: eventEventMessageName = "_event_message_";
0402: }
0403:
0404: serviceResponseMessageName = simpleMethodElement
0405: .getAttribute("service-response-message-name");
0406: if (serviceResponseMessageName == null
0407: || serviceResponseMessageName.length() == 0) {
0408: serviceResponseMessageName = "responseMessage";
0409: }
0410: serviceErrorMessageName = simpleMethodElement
0411: .getAttribute("service-error-message-name");
0412: if (serviceErrorMessageName == null
0413: || serviceErrorMessageName.length() == 0) {
0414: serviceErrorMessageName = "errorMessage";
0415: }
0416: serviceErrorMessageListName = simpleMethodElement
0417: .getAttribute("service-error-message-list-name");
0418: if (serviceErrorMessageListName == null
0419: || serviceErrorMessageListName.length() == 0) {
0420: serviceErrorMessageListName = "errorMessageList";
0421: }
0422: serviceErrorMessageMapName = simpleMethodElement
0423: .getAttribute("service-error-message-map-name");
0424: if (serviceErrorMessageMapName == null
0425: || serviceErrorMessageMapName.length() == 0) {
0426: serviceErrorMessageMapName = "errorMessageMap";
0427: }
0428:
0429: serviceSuccessMessageName = simpleMethodElement
0430: .getAttribute("service-success-message-name");
0431: if (serviceSuccessMessageName == null
0432: || serviceSuccessMessageName.length() == 0) {
0433: serviceSuccessMessageName = "successMessage";
0434: }
0435: serviceSuccessMessageListName = simpleMethodElement
0436: .getAttribute("service-success-message-list-name");
0437: if (serviceSuccessMessageListName == null
0438: || serviceSuccessMessageListName.length() == 0) {
0439: serviceSuccessMessageListName = "successMessageList";
0440: }
0441:
0442: loginRequired = !"false".equals(simpleMethodElement
0443: .getAttribute("login-required"));
0444: useTransaction = !"false".equals(simpleMethodElement
0445: .getAttribute("use-transaction"));
0446:
0447: localeName = simpleMethodElement.getAttribute("locale-name");
0448: if (localeName == null || localeName.length() == 0) {
0449: localeName = "locale";
0450: }
0451: delegatorName = simpleMethodElement
0452: .getAttribute("delegator-name");
0453: if (delegatorName == null || delegatorName.length() == 0) {
0454: delegatorName = "delegator";
0455: }
0456: securityName = simpleMethodElement
0457: .getAttribute("security-name");
0458: if (securityName == null || securityName.length() == 0) {
0459: securityName = "security";
0460: }
0461: dispatcherName = simpleMethodElement
0462: .getAttribute("dispatcher-name");
0463: if (dispatcherName == null || dispatcherName.length() == 0) {
0464: dispatcherName = "dispatcher";
0465: }
0466: userLoginName = simpleMethodElement
0467: .getAttribute("user-login-name");
0468: if (userLoginName == null || userLoginName.length() == 0) {
0469: userLoginName = "userLogin";
0470: }
0471:
0472: readOperations(simpleMethodElement, this .methodOperations, this );
0473: }
0474:
0475: public String getMethodName() {
0476: return this .methodName;
0477: }
0478:
0479: public SimpleMethod getSimpleMethodInSameFile(
0480: String simpleMethodName) {
0481: if (parentSimpleMethodsMap == null)
0482: return null;
0483: return (SimpleMethod) parentSimpleMethodsMap
0484: .get(simpleMethodName);
0485: }
0486:
0487: public String getShortDescription() {
0488: return this .shortDescription;
0489: }
0490:
0491: public String getDefaultErrorCode() {
0492: return this .defaultErrorCode;
0493: }
0494:
0495: public String getDefaultSuccessCode() {
0496: return this .defaultSuccessCode;
0497: }
0498:
0499: public String getParameterMapName() {
0500: return this .parameterMapName;
0501: }
0502:
0503: // event fields
0504: public String getEventRequestName() {
0505: return this .eventRequestName;
0506: }
0507:
0508: public String getEventResponseCodeName() {
0509: return this .eventResponseCodeName;
0510: }
0511:
0512: public String getEventErrorMessageName() {
0513: return this .eventErrorMessageName;
0514: }
0515:
0516: public String getEventEventMessageName() {
0517: return this .eventEventMessageName;
0518: }
0519:
0520: // service fields
0521: public String getServiceResponseMessageName() {
0522: return this .serviceResponseMessageName;
0523: }
0524:
0525: public String getServiceErrorMessageName() {
0526: return this .serviceErrorMessageName;
0527: }
0528:
0529: public String getServiceErrorMessageListName() {
0530: return this .serviceErrorMessageListName;
0531: }
0532:
0533: public String getServiceSuccessMessageName() {
0534: return this .serviceSuccessMessageName;
0535: }
0536:
0537: public String getServiceSuccessMessageListName() {
0538: return this .serviceSuccessMessageListName;
0539: }
0540:
0541: public boolean getLoginRequired() {
0542: return this .loginRequired;
0543: }
0544:
0545: public boolean getUseTransaction() {
0546: return this .useTransaction;
0547: }
0548:
0549: public String getDelegatorEnvName() {
0550: return this .delegatorName;
0551: }
0552:
0553: public String getSecurityEnvName() {
0554: return this .securityName;
0555: }
0556:
0557: public String getDispatcherEnvName() {
0558: return this .dispatcherName;
0559: }
0560:
0561: public String getUserLoginEnvName() {
0562: return this .userLoginName;
0563: }
0564:
0565: /** Execute the Simple Method operations */
0566: public String exec(MethodContext methodContext) {
0567: methodContext.putEnv(delegatorName, methodContext
0568: .getDelegator());
0569: methodContext.putEnv(securityName, methodContext.getSecurity());
0570: methodContext.putEnv(dispatcherName, methodContext
0571: .getDispatcher());
0572: methodContext.putEnv(localeName, methodContext.getLocale());
0573: methodContext.putEnv(parameterMapName, methodContext
0574: .getParameters());
0575:
0576: if (methodContext.getMethodType() == MethodContext.EVENT) {
0577: methodContext.putEnv(eventRequestName, methodContext
0578: .getRequest());
0579: methodContext.putEnv(eventResponseName, methodContext
0580: .getResponse());
0581: }
0582:
0583: GenericValue userLogin = methodContext.getUserLogin();
0584:
0585: if (userLogin != null) {
0586: methodContext.putEnv(userLoginName, userLogin);
0587: }
0588: if (loginRequired) {
0589: if (userLogin == null) {
0590: String errMsg = "You must be logged in to complete the "
0591: + shortDescription + " process.";
0592:
0593: if (methodContext.getMethodType() == MethodContext.EVENT) {
0594: methodContext.getRequest().setAttribute(
0595: "_ERROR_MESSAGE_", errMsg);
0596: return defaultErrorCode;
0597: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
0598: methodContext.putResult(ModelService.ERROR_MESSAGE,
0599: errMsg);
0600: methodContext.putResult(
0601: ModelService.RESPONSE_MESSAGE,
0602: ModelService.RESPOND_ERROR);
0603: return null;
0604: }
0605: }
0606: }
0607:
0608: // if using transaction, try to start here
0609: boolean beganTransaction = false;
0610:
0611: if (useTransaction) {
0612: try {
0613: beganTransaction = TransactionUtil.begin();
0614: } catch (GenericTransactionException e) {
0615: String errMsg = "Error trying to begin transaction, could not process method: "
0616: + e.getMessage();
0617:
0618: Debug.logWarning(errMsg, module);
0619: Debug.logWarning(e, module);
0620: if (methodContext.getMethodType() == MethodContext.EVENT) {
0621: methodContext.getRequest().setAttribute(
0622: "_ERROR_MESSAGE_", errMsg);
0623: return defaultErrorCode;
0624: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
0625: methodContext.putResult(ModelService.ERROR_MESSAGE,
0626: errMsg);
0627: methodContext.putResult(
0628: ModelService.RESPONSE_MESSAGE,
0629: ModelService.RESPOND_ERROR);
0630: return null;
0631: }
0632: }
0633: }
0634:
0635: // declare errorMsg here just in case transaction ops fail
0636: String errorMsg = "";
0637:
0638: boolean finished = false;
0639: try {
0640: finished = runSubOps(methodOperations, methodContext);
0641: } catch (Throwable t) {
0642: // make SURE nothing gets thrown through
0643: String errMsg = "Error running the simple-method: "
0644: + t.toString();
0645: Debug.log(t, errMsg, module);
0646: finished = false;
0647: errorMsg += errMsg + "<br>";
0648: }
0649:
0650: String returnValue = null;
0651: String response = null;
0652: if (methodContext.getMethodType() == MethodContext.EVENT) {
0653: boolean forceError = false;
0654:
0655: String tempErrorMsg = (String) methodContext
0656: .getEnv(eventErrorMessageName);
0657: if (errorMsg.length() > 0
0658: || (tempErrorMsg != null && tempErrorMsg.length() > 0)) {
0659: errorMsg += tempErrorMsg;
0660: methodContext.getRequest().setAttribute(
0661: "_ERROR_MESSAGE_", errorMsg);
0662: forceError = true;
0663: }
0664:
0665: String eventMsg = (String) methodContext
0666: .getEnv(eventEventMessageName);
0667: if (eventMsg != null && eventMsg.length() > 0) {
0668: methodContext.getRequest().setAttribute(
0669: "_EVENT_MESSAGE_", eventMsg);
0670: }
0671:
0672: response = (String) methodContext
0673: .getEnv(eventResponseCodeName);
0674: if (response == null || response.length() == 0) {
0675: if (forceError) {
0676: //override response code, always use error code
0677: Debug
0678: .logInfo(
0679: "No response code string found, but error messages found so assuming error; returning code ["
0680: + defaultErrorCode + "]",
0681: module);
0682: response = defaultErrorCode;
0683: } else {
0684: Debug.logInfo(
0685: "No response code string or errors found, assuming success; returning code ["
0686: + defaultSuccessCode + "]", module);
0687: response = defaultSuccessCode;
0688: }
0689: }
0690: returnValue = response;
0691: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
0692: boolean forceError = false;
0693:
0694: String tempErrorMsg = (String) methodContext
0695: .getEnv(serviceErrorMessageName);
0696: if (errorMsg.length() > 0
0697: || (tempErrorMsg != null && tempErrorMsg.length() > 0)) {
0698: errorMsg += tempErrorMsg;
0699: methodContext.putResult(ModelService.ERROR_MESSAGE,
0700: errorMsg);
0701: forceError = true;
0702: }
0703:
0704: List errorMsgList = (List) methodContext
0705: .getEnv(serviceErrorMessageListName);
0706: if (errorMsgList != null && errorMsgList.size() > 0) {
0707: methodContext.putResult(
0708: ModelService.ERROR_MESSAGE_LIST, errorMsgList);
0709: forceError = true;
0710: }
0711:
0712: Map errorMsgMap = (Map) methodContext
0713: .getEnv(serviceErrorMessageMapName);
0714: if (errorMsgMap != null && errorMsgMap.size() > 0) {
0715: methodContext.putResult(ModelService.ERROR_MESSAGE_MAP,
0716: errorMsgMap);
0717: forceError = true;
0718: }
0719:
0720: String successMsg = (String) methodContext
0721: .getEnv(serviceSuccessMessageName);
0722: if (successMsg != null && successMsg.length() > 0) {
0723: methodContext.putResult(ModelService.SUCCESS_MESSAGE,
0724: successMsg);
0725: }
0726:
0727: List successMsgList = (List) methodContext
0728: .getEnv(serviceSuccessMessageListName);
0729: if (successMsgList != null && successMsgList.size() > 0) {
0730: methodContext.putResult(
0731: ModelService.SUCCESS_MESSAGE_LIST,
0732: successMsgList);
0733: }
0734:
0735: response = (String) methodContext
0736: .getEnv(serviceResponseMessageName);
0737: if (response == null || response.length() == 0) {
0738: if (forceError) {
0739: //override response code, always use error code
0740: Debug
0741: .logVerbose(
0742: "No response code string found, but error messages found so assuming error; returning code ["
0743: + defaultErrorCode + "]",
0744: module);
0745: response = defaultErrorCode;
0746: } else {
0747: Debug.logVerbose(
0748: "No response code string or errors found, assuming success; returning code ["
0749: + defaultSuccessCode + "]", module);
0750: response = defaultSuccessCode;
0751: }
0752: }
0753: methodContext.putResult(ModelService.RESPONSE_MESSAGE,
0754: response);
0755: returnValue = null;
0756: } else {
0757: response = defaultSuccessCode;
0758: returnValue = defaultSuccessCode;
0759: }
0760:
0761: // decide whether or not to commit based on the response message, ie only rollback if error is returned and not finished
0762: boolean doCommit = true;
0763: if (!finished) {
0764: if (defaultErrorCode.equals(response)) {
0765: doCommit = false;
0766: }
0767: }
0768:
0769: if (doCommit) {
0770: // commit here passing beganTransaction to perform it properly
0771: try {
0772: TransactionUtil.commit(beganTransaction);
0773: } catch (GenericTransactionException e) {
0774: String errMsg = "Error trying to commit transaction, could not process method: "
0775: + e.getMessage();
0776: Debug.logWarning(errMsg, module);
0777: Debug.logWarning(e, module);
0778: errorMsg += errMsg + "<br>";
0779: }
0780: } else {
0781: // rollback here passing beganTransaction to either rollback, or set rollback only
0782: try {
0783: TransactionUtil.rollback(beganTransaction);
0784: } catch (GenericTransactionException e) {
0785: String errMsg = "Error trying to rollback transaction, could not process method: "
0786: + e.getMessage();
0787: Debug.logWarning(errMsg, module);
0788: Debug.logWarning(e, module);
0789: errorMsg += errMsg + "<br>";
0790: }
0791: }
0792:
0793: return returnValue;
0794: }
0795:
0796: public static void readOperations(Element simpleMethodElement,
0797: List methodOperations, SimpleMethod simpleMethod) {
0798: List operationElements = UtilXml.childElementList(
0799: simpleMethodElement, null);
0800:
0801: if (operationElements != null && operationElements.size() > 0) {
0802: Iterator operElemIter = operationElements.iterator();
0803:
0804: while (operElemIter.hasNext()) {
0805: Element curOperElem = (Element) operElemIter.next();
0806: String nodeName = curOperElem.getNodeName();
0807:
0808: if ("call-map-processor".equals(nodeName)) {
0809: methodOperations
0810: .add(new org.ofbiz.minilang.method.callops.CallSimpleMapProcessor(
0811: curOperElem, simpleMethod));
0812: } else if ("check-errors".equals(nodeName)) {
0813: methodOperations
0814: .add(new org.ofbiz.minilang.method.callops.CheckErrors(
0815: curOperElem, simpleMethod));
0816: } else if ("add-error".equals(nodeName)) {
0817: methodOperations
0818: .add(new org.ofbiz.minilang.method.callops.AddError(
0819: curOperElem, simpleMethod));
0820: } else if ("return".equals(nodeName)) {
0821: methodOperations
0822: .add(new org.ofbiz.minilang.method.callops.Return(
0823: curOperElem, simpleMethod));
0824: } else if ("set-service-fields".equals(nodeName)) {
0825: methodOperations
0826: .add(new org.ofbiz.minilang.method.callops.SetServiceFields(
0827: curOperElem, simpleMethod));
0828: } else if ("call-service".equals(nodeName)) {
0829: methodOperations
0830: .add(new org.ofbiz.minilang.method.callops.CallService(
0831: curOperElem, simpleMethod));
0832: } else if ("call-service-asynch".equals(nodeName)) {
0833: methodOperations
0834: .add(new org.ofbiz.minilang.method.callops.CallServiceAsynch(
0835: curOperElem, simpleMethod));
0836: } else if ("call-bsh".equals(nodeName)) {
0837: methodOperations
0838: .add(new org.ofbiz.minilang.method.callops.CallBsh(
0839: curOperElem, simpleMethod));
0840: } else if ("call-simple-method".equals(nodeName)) {
0841: methodOperations
0842: .add(new org.ofbiz.minilang.method.callops.CallSimpleMethod(
0843: curOperElem, simpleMethod));
0844:
0845: } else if ("call-object-method".equals(nodeName)) {
0846: methodOperations
0847: .add(new org.ofbiz.minilang.method.callops.CallObjectMethod(
0848: curOperElem, simpleMethod));
0849: } else if ("call-class-method".equals(nodeName)) {
0850: methodOperations
0851: .add(new org.ofbiz.minilang.method.callops.CallClassMethod(
0852: curOperElem, simpleMethod));
0853: } else if ("create-object".equals(nodeName)) {
0854: methodOperations
0855: .add(new org.ofbiz.minilang.method.callops.CreateObject(
0856: curOperElem, simpleMethod));
0857:
0858: } else if ("field-to-request".equals(nodeName)) {
0859: methodOperations
0860: .add(new org.ofbiz.minilang.method.eventops.FieldToRequest(
0861: curOperElem, simpleMethod));
0862: } else if ("field-to-session".equals(nodeName)) {
0863: methodOperations
0864: .add(new org.ofbiz.minilang.method.eventops.FieldToSession(
0865: curOperElem, simpleMethod));
0866: } else if ("request-to-field".equals(nodeName)) {
0867: methodOperations
0868: .add(new org.ofbiz.minilang.method.eventops.RequestToField(
0869: curOperElem, simpleMethod));
0870: } else if ("request-parameters-to-list"
0871: .equals(nodeName)) {
0872: methodOperations
0873: .add(new org.ofbiz.minilang.method.eventops.RequestParametersToList(
0874: curOperElem, simpleMethod));
0875: } else if ("session-to-field".equals(nodeName)) {
0876: methodOperations
0877: .add(new org.ofbiz.minilang.method.eventops.SessionToField(
0878: curOperElem, simpleMethod));
0879: } else if ("webapp-property-to-field".equals(nodeName)) {
0880: methodOperations
0881: .add(new org.ofbiz.minilang.method.eventops.WebappPropertyToField(
0882: curOperElem, simpleMethod));
0883:
0884: } else if ("field-to-result".equals(nodeName)) {
0885: methodOperations
0886: .add(new org.ofbiz.minilang.method.serviceops.FieldToResult(
0887: curOperElem, simpleMethod));
0888:
0889: } else if ("map-to-map".equals(nodeName)) {
0890: methodOperations
0891: .add(new org.ofbiz.minilang.method.envops.MapToMap(
0892: curOperElem, simpleMethod));
0893: } else if ("field-to-field".equals(nodeName)) {
0894: methodOperations
0895: .add(new org.ofbiz.minilang.method.envops.FieldToField(
0896: curOperElem, simpleMethod));
0897: } else if ("field-to-list".equals(nodeName)) {
0898: methodOperations
0899: .add(new org.ofbiz.minilang.method.envops.FieldToList(
0900: curOperElem, simpleMethod));
0901: } else if ("list-to-list".equals(nodeName)) {
0902: methodOperations
0903: .add(new org.ofbiz.minilang.method.envops.ListToList(
0904: curOperElem, simpleMethod));
0905: } else if ("env-to-env".equals(nodeName)) {
0906: methodOperations
0907: .add(new org.ofbiz.minilang.method.envops.EnvToEnv(
0908: curOperElem, simpleMethod));
0909: } else if ("env-to-field".equals(nodeName)) {
0910: methodOperations
0911: .add(new org.ofbiz.minilang.method.envops.EnvToField(
0912: curOperElem, simpleMethod));
0913: } else if ("field-to-env".equals(nodeName)) {
0914: methodOperations
0915: .add(new org.ofbiz.minilang.method.envops.FieldToEnv(
0916: curOperElem, simpleMethod));
0917: } else if ("string-append".equals(nodeName)) {
0918: methodOperations
0919: .add(new org.ofbiz.minilang.method.envops.StringAppend(
0920: curOperElem, simpleMethod));
0921: } else if ("string-to-field".equals(nodeName)) {
0922: methodOperations
0923: .add(new org.ofbiz.minilang.method.envops.StringToField(
0924: curOperElem, simpleMethod));
0925: } else if ("string-to-list".equals(nodeName)) {
0926: methodOperations
0927: .add(new org.ofbiz.minilang.method.envops.StringToList(
0928: curOperElem, simpleMethod));
0929: } else if ("to-string".equals(nodeName)) {
0930: methodOperations
0931: .add(new org.ofbiz.minilang.method.envops.ToString(
0932: curOperElem, simpleMethod));
0933: } else if ("clear-field".equals(nodeName)) {
0934: methodOperations
0935: .add(new org.ofbiz.minilang.method.envops.ClearField(
0936: curOperElem, simpleMethod));
0937: } else if ("iterate".equals(nodeName)) {
0938: methodOperations
0939: .add(new org.ofbiz.minilang.method.envops.Iterate(
0940: curOperElem, simpleMethod));
0941: } else if ("iterate-map".equals(nodeName)) {
0942: methodOperations
0943: .add(new org.ofbiz.minilang.method.envops.IterateMap(
0944: curOperElem, simpleMethod));
0945: } else if ("first-from-list".equals(nodeName)) {
0946: methodOperations
0947: .add(new org.ofbiz.minilang.method.envops.FirstFromList(
0948: curOperElem, simpleMethod));
0949:
0950: } else if ("transaction-begin".equals(nodeName)) {
0951: methodOperations
0952: .add(new org.ofbiz.minilang.method.entityops.TransactionBegin(
0953: curOperElem, simpleMethod));
0954: } else if ("transaction-commit".equals(nodeName)) {
0955: methodOperations
0956: .add(new org.ofbiz.minilang.method.entityops.TransactionCommit(
0957: curOperElem, simpleMethod));
0958: } else if ("transaction-rollback".equals(nodeName)) {
0959: methodOperations
0960: .add(new org.ofbiz.minilang.method.entityops.TransactionRollback(
0961: curOperElem, simpleMethod));
0962:
0963: } else if ("now-timestamp-to-env".equals(nodeName)) {
0964: methodOperations
0965: .add(new org.ofbiz.minilang.method.entityops.NowTimestampToEnv(
0966: curOperElem, simpleMethod));
0967: } else if ("now-date-to-env".equals(nodeName)) {
0968: methodOperations
0969: .add(new org.ofbiz.minilang.method.entityops.NowDateToEnv(
0970: curOperElem, simpleMethod));
0971: } else if ("sequenced-id-to-env".equals(nodeName)) {
0972: methodOperations
0973: .add(new org.ofbiz.minilang.method.entityops.SequencedIdToEnv(
0974: curOperElem, simpleMethod));
0975: } else if ("make-next-seq-id".equals(nodeName)) {
0976: methodOperations
0977: .add(new org.ofbiz.minilang.method.entityops.MakeNextSeqId(
0978: curOperElem, simpleMethod));
0979: } else if ("set-current-user-login".equals(nodeName)) {
0980: methodOperations
0981: .add(new org.ofbiz.minilang.method.entityops.SetCurrentUserLogin(
0982: curOperElem, simpleMethod));
0983:
0984: } else if ("find-by-primary-key".equals(nodeName)) {
0985: methodOperations
0986: .add(new org.ofbiz.minilang.method.entityops.FindByPrimaryKey(
0987: curOperElem, simpleMethod));
0988: } else if ("find-by-and".equals(nodeName)) {
0989: methodOperations
0990: .add(new org.ofbiz.minilang.method.entityops.FindByAnd(
0991: curOperElem, simpleMethod));
0992: } else if ("get-related-one".equals(nodeName)) {
0993: methodOperations
0994: .add(new org.ofbiz.minilang.method.entityops.GetRelatedOne(
0995: curOperElem, simpleMethod));
0996: } else if ("get-related".equals(nodeName)) {
0997: methodOperations
0998: .add(new org.ofbiz.minilang.method.entityops.GetRelated(
0999: curOperElem, simpleMethod));
1000: } else if ("filter-list-by-and".equals(nodeName)) {
1001: methodOperations
1002: .add(new org.ofbiz.minilang.method.entityops.FilterListByAnd(
1003: curOperElem, simpleMethod));
1004: } else if ("filter-list-by-date".equals(nodeName)) {
1005: methodOperations
1006: .add(new org.ofbiz.minilang.method.entityops.FilterListByDate(
1007: curOperElem, simpleMethod));
1008: } else if ("order-value-list".equals(nodeName)) {
1009: methodOperations
1010: .add(new org.ofbiz.minilang.method.entityops.OrderValueList(
1011: curOperElem, simpleMethod));
1012:
1013: } else if ("make-value".equals(nodeName)) {
1014: methodOperations
1015: .add(new org.ofbiz.minilang.method.entityops.MakeValue(
1016: curOperElem, simpleMethod));
1017: } else if ("clone-value".equals(nodeName)) {
1018: methodOperations
1019: .add(new org.ofbiz.minilang.method.entityops.CloneValue(
1020: curOperElem, simpleMethod));
1021: } else if ("create-value".equals(nodeName)) {
1022: methodOperations
1023: .add(new org.ofbiz.minilang.method.entityops.CreateValue(
1024: curOperElem, simpleMethod));
1025: } else if ("store-value".equals(nodeName)) {
1026: methodOperations
1027: .add(new org.ofbiz.minilang.method.entityops.StoreValue(
1028: curOperElem, simpleMethod));
1029: } else if ("refresh-value".equals(nodeName)) {
1030: methodOperations
1031: .add(new org.ofbiz.minilang.method.entityops.RefreshValue(
1032: curOperElem, simpleMethod));
1033: } else if ("remove-value".equals(nodeName)) {
1034: methodOperations
1035: .add(new org.ofbiz.minilang.method.entityops.RemoveValue(
1036: curOperElem, simpleMethod));
1037: } else if ("remove-related".equals(nodeName)) {
1038: methodOperations
1039: .add(new org.ofbiz.minilang.method.entityops.RemoveRelated(
1040: curOperElem, simpleMethod));
1041: } else if ("remove-by-and".equals(nodeName)) {
1042: methodOperations
1043: .add(new org.ofbiz.minilang.method.entityops.RemoveByAnd(
1044: curOperElem, simpleMethod));
1045: } else if ("clear-cache-line".equals(nodeName)) {
1046: methodOperations
1047: .add(new org.ofbiz.minilang.method.entityops.ClearCacheLine(
1048: curOperElem, simpleMethod));
1049: } else if ("clear-entity-caches".equals(nodeName)) {
1050: methodOperations
1051: .add(new org.ofbiz.minilang.method.entityops.ClearEntityCaches(
1052: curOperElem, simpleMethod));
1053: } else if ("set-pk-fields".equals(nodeName)) {
1054: methodOperations
1055: .add(new org.ofbiz.minilang.method.entityops.SetPkFields(
1056: curOperElem, simpleMethod));
1057: } else if ("set-nonpk-fields".equals(nodeName)) {
1058: methodOperations
1059: .add(new org.ofbiz.minilang.method.entityops.SetNonpkFields(
1060: curOperElem, simpleMethod));
1061:
1062: } else if ("store-list".equals(nodeName)) {
1063: methodOperations
1064: .add(new org.ofbiz.minilang.method.entityops.StoreList(
1065: curOperElem, simpleMethod));
1066: } else if ("remove-list".equals(nodeName)) {
1067: methodOperations
1068: .add(new org.ofbiz.minilang.method.entityops.RemoveList(
1069: curOperElem, simpleMethod));
1070:
1071: } else if ("if".equals(nodeName)) {
1072: methodOperations
1073: .add(new org.ofbiz.minilang.method.conditional.MasterIf(
1074: curOperElem, simpleMethod));
1075: } else if ("if-validate-method".equals(nodeName)) {
1076: methodOperations
1077: .add(new org.ofbiz.minilang.method.ifops.IfValidateMethod(
1078: curOperElem, simpleMethod));
1079: } else if ("if-compare".equals(nodeName)) {
1080: methodOperations
1081: .add(new org.ofbiz.minilang.method.ifops.IfCompare(
1082: curOperElem, simpleMethod));
1083: } else if ("if-compare-field".equals(nodeName)) {
1084: methodOperations
1085: .add(new org.ofbiz.minilang.method.ifops.IfCompareField(
1086: curOperElem, simpleMethod));
1087: } else if ("if-regexp".equals(nodeName)) {
1088: methodOperations
1089: .add(new org.ofbiz.minilang.method.ifops.IfRegexp(
1090: curOperElem, simpleMethod));
1091: } else if ("if-empty".equals(nodeName)) {
1092: methodOperations
1093: .add(new org.ofbiz.minilang.method.ifops.IfEmpty(
1094: curOperElem, simpleMethod));
1095: } else if ("if-not-empty".equals(nodeName)) {
1096: methodOperations
1097: .add(new org.ofbiz.minilang.method.ifops.IfNotEmpty(
1098: curOperElem, simpleMethod));
1099: } else if ("if-has-permission".equals(nodeName)) {
1100: methodOperations
1101: .add(new org.ofbiz.minilang.method.ifops.IfHasPermission(
1102: curOperElem, simpleMethod));
1103: } else if ("check-permission".equals(nodeName)) {
1104: methodOperations
1105: .add(new org.ofbiz.minilang.method.ifops.CheckPermission(
1106: curOperElem, simpleMethod));
1107: } else if ("check-id".equals(nodeName)) {
1108: methodOperations
1109: .add(new org.ofbiz.minilang.method.ifops.CheckId(
1110: curOperElem, simpleMethod));
1111: } else if ("else".equals(nodeName)) {
1112: // don't add anything, but don't complain either, this one is handled in the individual operations
1113: } else if ("property-to-field".equals(nodeName)) {
1114: methodOperations
1115: .add(new org.ofbiz.minilang.method.otherops.PropertyToField(
1116: curOperElem, simpleMethod));
1117: } else if ("calculate".equals(nodeName)) {
1118: methodOperations
1119: .add(new org.ofbiz.minilang.method.otherops.Calculate(
1120: curOperElem, simpleMethod));
1121: } else if ("log".equals(nodeName)) {
1122: methodOperations
1123: .add(new org.ofbiz.minilang.method.otherops.Log(
1124: curOperElem, simpleMethod));
1125:
1126: } else {
1127: Debug.logWarning("Operation element \"" + nodeName
1128: + "\" no recognized", module);
1129: }
1130: }
1131: }
1132: }
1133:
1134: /** Execs the given operations returning true if all return true, or returning
1135: * false and stopping if any return false.
1136: */
1137: public static boolean runSubOps(List methodOperations,
1138: MethodContext methodContext) {
1139: Iterator methodOpsIter = methodOperations.iterator();
1140: while (methodOpsIter.hasNext()) {
1141: MethodOperation methodOperation = (MethodOperation) methodOpsIter
1142: .next();
1143: if (!methodOperation.exec(methodContext)) {
1144: return false;
1145: }
1146: }
1147: return true;
1148: }
1149: }
|