001: /*
002: * $Id: IfCompare.java,v 1.2 2003/09/14 05:40:41 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.ifops;
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: import org.ofbiz.minilang.operation.*;
034:
035: /**
036: * Iff the comparison between the constant and the specified field is true process sub-operations
037: *
038: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
039: * @version $Revision: 1.2 $
040: * @since 2.0
041: */
042: public class IfCompare extends MethodOperation {
043:
044: public static final String module = IfCompare.class.getName();
045:
046: List subOps = new LinkedList();
047: List elseSubOps = null;
048:
049: ContextAccessor mapAcsr;
050: ContextAccessor fieldAcsr;
051: String value;
052:
053: String operator;
054: String type;
055: String format;
056:
057: public IfCompare(Element element, SimpleMethod simpleMethod) {
058: super (element, simpleMethod);
059: this .mapAcsr = new ContextAccessor(element
060: .getAttribute("map-name"));
061: this .fieldAcsr = new ContextAccessor(element
062: .getAttribute("field-name"));
063: this .value = element.getAttribute("value");
064:
065: this .operator = element.getAttribute("operator");
066: this .type = element.getAttribute("type");
067: this .format = element.getAttribute("format");
068:
069: SimpleMethod.readOperations(element, subOps, simpleMethod);
070: Element elseElement = UtilXml
071: .firstChildElement(element, "else");
072: if (elseElement != null) {
073: elseSubOps = new LinkedList();
074: SimpleMethod.readOperations(elseElement, elseSubOps,
075: simpleMethod);
076: }
077: }
078:
079: public boolean exec(MethodContext methodContext) {
080: // if conditions fails, always return true; if a sub-op returns false
081: // return false and stop, otherwise return true
082:
083: String value = methodContext.expandString(this .value);
084: String operator = methodContext.expandString(this .operator);
085: String type = methodContext.expandString(this .type);
086: String format = methodContext.expandString(this .format);
087:
088: Object fieldVal = null;
089: if (!mapAcsr.isEmpty()) {
090: Map fromMap = (Map) mapAcsr.get(methodContext);
091: if (fromMap == null) {
092: if (Debug.infoOn())
093: Debug.logInfo("Map not found with name " + mapAcsr
094: + ", using empty string for comparison",
095: module);
096: } else {
097: fieldVal = fieldAcsr.get(fromMap, methodContext);
098: }
099: } else {
100: // no map name, try the env
101: fieldVal = fieldAcsr.get(methodContext);
102: }
103:
104: // always use an empty string by default
105: if (fieldVal == null) {
106: fieldVal = "";
107: }
108:
109: List messages = new LinkedList();
110: Boolean resultBool = BaseCompare.doRealCompare(fieldVal, value,
111: operator, type, format, messages, null, methodContext
112: .getLoader());
113: if (messages.size() > 0) {
114: messages.add(0,
115: "Error with comparison in if-compare between field ["
116: + mapAcsr.toString() + "."
117: + fieldAcsr.toString() + "] with value ["
118: + fieldVal + "] and value [" + value
119: + "] with operator [" + operator
120: + "] and type [" + type + "]: ");
121: if (methodContext.getMethodType() == MethodContext.EVENT) {
122: StringBuffer fullString = new StringBuffer();
123:
124: Iterator miter = messages.iterator();
125: while (miter.hasNext()) {
126: fullString.append((String) miter.next());
127: }
128: Debug.logWarning(fullString.toString(), module);
129:
130: methodContext.putEnv(simpleMethod
131: .getEventErrorMessageName(), fullString
132: .toString());
133: methodContext.putEnv(simpleMethod
134: .getEventResponseCodeName(), simpleMethod
135: .getDefaultErrorCode());
136: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
137: methodContext.putEnv(simpleMethod
138: .getServiceErrorMessageListName(), messages);
139: methodContext.putEnv(simpleMethod
140: .getServiceResponseMessageName(), simpleMethod
141: .getDefaultErrorCode());
142: }
143: return false;
144: }
145:
146: if (resultBool != null && resultBool.booleanValue()) {
147: return SimpleMethod.runSubOps(subOps, methodContext);
148: } else {
149: if (elseSubOps != null) {
150: return SimpleMethod
151: .runSubOps(elseSubOps, methodContext);
152: } else {
153: return true;
154: }
155: }
156: }
157: }
|