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.List;
021:
022: import org.ofbiz.base.util.UtilXml;
023: import org.ofbiz.minilang.SimpleMethod;
024: import org.ofbiz.minilang.method.ContextAccessor;
025: import org.ofbiz.minilang.method.MethodContext;
026: import org.ofbiz.minilang.method.MethodOperation;
027: import org.w3c.dom.Element;
028:
029: /**
030: * An event operation that checks a message list and may introduce a return code and stop the event
031: */
032: public class CheckErrors extends MethodOperation {
033:
034: ContextAccessor errorListAcsr;
035: String errorCode;
036:
037: FlexibleMessage errorPrefix;
038: FlexibleMessage errorSuffix;
039: FlexibleMessage messagePrefix;
040: FlexibleMessage messageSuffix;
041:
042: public CheckErrors(Element element, SimpleMethod simpleMethod) {
043: super (element, simpleMethod);
044: errorCode = element.getAttribute("error-code");
045: if (errorCode == null || errorCode.length() == 0)
046: errorCode = "error";
047:
048: errorListAcsr = new ContextAccessor(element
049: .getAttribute("error-list-name"), "error_list");
050:
051: errorPrefix = new FlexibleMessage(UtilXml.firstChildElement(
052: element, "error-prefix"), "check.error.prefix");
053: errorSuffix = new FlexibleMessage(UtilXml.firstChildElement(
054: element, "error-suffix"), "check.error.suffix");
055: messagePrefix = new FlexibleMessage(UtilXml.firstChildElement(
056: element, "message-prefix"), "check.message.prefix");
057: messageSuffix = new FlexibleMessage(UtilXml.firstChildElement(
058: element, "message-suffix"), "check.message.suffix");
059: }
060:
061: public boolean exec(MethodContext methodContext) {
062: List messages = (List) errorListAcsr.get(methodContext);
063:
064: if (messages != null && messages.size() > 0) {
065: String errorCode = methodContext
066: .expandString(this .errorCode);
067:
068: if (methodContext.getMethodType() == MethodContext.EVENT) {
069: /* The OLD way, now puts formatting control in the template...
070: String errMsg = errorPrefix.getMessage(methodContext.getLoader(), methodContext) +
071: ServiceUtil.makeMessageList(messages, messagePrefix.getMessage(methodContext.getLoader(), methodContext),
072: messageSuffix.getMessage(methodContext.getLoader(), methodContext)) +
073: errorSuffix.getMessage(methodContext.getLoader(), methodContext);
074: methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
075: */
076: methodContext.putEnv(simpleMethod
077: .getEventErrorMessageListName(), messages);
078: methodContext.putEnv(simpleMethod
079: .getEventResponseCodeName(), errorCode);
080: return false;
081: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
082: methodContext.putEnv(simpleMethod
083: .getServiceErrorMessageListName(), messages);
084: methodContext.putEnv(simpleMethod
085: .getServiceResponseMessageName(), errorCode);
086: return false;
087: } else {
088: return false;
089: }
090: }
091:
092: return true;
093: }
094:
095: public String rawString() {
096: // TODO: something more than the empty tag
097: return "<check-errors/>";
098: }
099:
100: public String expandedString(MethodContext methodContext) {
101: // TODO: something more than a stub/dummy
102: return this.rawString();
103: }
104: }
|