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: * Adds the fail-message or fail-property value to the error-list.
029: */
030: public class AddError extends MethodOperation {
031: String message = null;
032: String propertyResource = null;
033: boolean isProperty = false;
034:
035: ContextAccessor errorListAcsr;
036:
037: public AddError(Element element, SimpleMethod simpleMethod) {
038: super (element, simpleMethod);
039: errorListAcsr = new ContextAccessor(element
040: .getAttribute("error-list-name"), "error_list");
041:
042: Element failMessage = UtilXml.firstChildElement(element,
043: "fail-message");
044: Element failProperty = UtilXml.firstChildElement(element,
045: "fail-property");
046:
047: if (failMessage != null) {
048: this .message = failMessage.getAttribute("message");
049: this .isProperty = false;
050: } else if (failProperty != null) {
051: this .propertyResource = failProperty
052: .getAttribute("resource");
053: this .message = failProperty.getAttribute("property");
054: this .isProperty = true;
055: }
056: }
057:
058: public boolean exec(MethodContext methodContext) {
059:
060: List messages = (List) errorListAcsr.get(methodContext);
061: if (messages == null) {
062: messages = new LinkedList();
063: errorListAcsr.put(methodContext, messages);
064: }
065:
066: this .addMessage(messages, methodContext.getLoader(),
067: methodContext);
068: return true;
069: }
070:
071: public void addMessage(List messages, ClassLoader loader,
072: MethodContext methodContext) {
073: String message = methodContext.expandString(this .message);
074: String propertyResource = methodContext
075: .expandString(this .propertyResource);
076:
077: if (!isProperty && message != null) {
078: messages.add(message);
079: // if (Debug.infoOn()) Debug.logInfo("[SimpleMapOperation.addMessage] Adding message: " + message, module);
080: } else if (isProperty && propertyResource != null
081: && message != null) {
082: //String propMsg = UtilProperties.getPropertyValue(UtilURL.fromResource(propertyResource, loader), message);
083: String propMsg = UtilProperties.getMessage(
084: propertyResource, message, methodContext
085: .getEnvMap(), methodContext.getLocale());
086:
087: if (propMsg == null || propMsg.length() == 0) {
088: messages
089: .add("Simple Method error occurred, but no message was found, sorry.");
090: } else {
091: messages.add(methodContext.expandString(propMsg));
092: }
093: // if (Debug.infoOn()) Debug.logInfo("[SimpleMapOperation.addMessage] Adding property message: " + propMsg, module);
094: } else {
095: messages
096: .add("Simple Method error occurred, but no message was found, sorry.");
097: // if (Debug.infoOn()) Debug.logInfo("[SimpleMapOperation.addMessage] ERROR: No message found", module);
098: }
099: }
100:
101: public String rawString() {
102: // TODO: something more than the empty tag
103: return "<add-error/>";
104: }
105:
106: public String expandedString(MethodContext methodContext) {
107: // TODO: something more than a stub/dummy
108: return this.rawString();
109: }
110: }
|