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