001: /*
002: * $Id: CheckErrors.java,v 1.1 2003/08/17 06:06:13 ajzeneski 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.List;
027:
028: import org.ofbiz.base.util.UtilXml;
029: import org.ofbiz.minilang.SimpleMethod;
030: import org.ofbiz.minilang.method.ContextAccessor;
031: import org.ofbiz.minilang.method.MethodContext;
032: import org.ofbiz.minilang.method.MethodOperation;
033: import org.ofbiz.service.ServiceUtil;
034: import org.w3c.dom.Element;
035:
036: /**
037: * An event operation that checks a message list and may introduce a return code and stop the event
038: *
039: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
040: * @version $Revision: 1.1 $
041: * @since 2.0
042: */
043: public class CheckErrors extends MethodOperation {
044:
045: ContextAccessor errorListAcsr;
046: String errorCode;
047:
048: FlexibleMessage errorPrefix;
049: FlexibleMessage errorSuffix;
050: FlexibleMessage messagePrefix;
051: FlexibleMessage messageSuffix;
052:
053: public CheckErrors(Element element, SimpleMethod simpleMethod) {
054: super (element, simpleMethod);
055: errorCode = element.getAttribute("error-code");
056: if (errorCode == null || errorCode.length() == 0)
057: errorCode = "error";
058:
059: errorListAcsr = new ContextAccessor(element
060: .getAttribute("error-list-name"), "error_list");
061:
062: errorPrefix = new FlexibleMessage(UtilXml.firstChildElement(
063: element, "error-prefix"), "check.error.prefix");
064: errorSuffix = new FlexibleMessage(UtilXml.firstChildElement(
065: element, "error-suffix"), "check.error.suffix");
066: messagePrefix = new FlexibleMessage(UtilXml.firstChildElement(
067: element, "message-prefix"), "check.message.prefix");
068: messageSuffix = new FlexibleMessage(UtilXml.firstChildElement(
069: element, "message-suffix"), "check.message.suffix");
070: }
071:
072: public boolean exec(MethodContext methodContext) {
073: List messages = (List) errorListAcsr.get(methodContext);
074:
075: if (messages != null && messages.size() > 0) {
076: String errorCode = methodContext
077: .expandString(this .errorCode);
078:
079: if (methodContext.getMethodType() == MethodContext.EVENT) {
080: String errMsg = errorPrefix.getMessage(methodContext
081: .getLoader(), methodContext)
082: + ServiceUtil.makeMessageList(messages,
083: messagePrefix.getMessage(methodContext
084: .getLoader(), methodContext),
085: messageSuffix.getMessage(methodContext
086: .getLoader(), methodContext))
087: + errorSuffix.getMessage(methodContext
088: .getLoader(), methodContext);
089:
090: methodContext.putEnv(simpleMethod
091: .getEventErrorMessageName(), errMsg);
092:
093: methodContext.putEnv(simpleMethod
094: .getEventResponseCodeName(), errorCode);
095: return false;
096: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
097: methodContext.putEnv(simpleMethod
098: .getServiceErrorMessageListName(), messages);
099: methodContext.putEnv(simpleMethod
100: .getServiceResponseMessageName(), errorCode);
101: return false;
102: } else {
103: return false;
104: }
105: }
106:
107: return true;
108: }
109: }
|