001: /*
002: * $Id: CompareCondition.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.conditional;
025:
026: import java.util.*;
027: import org.w3c.dom.*;
028: import org.ofbiz.base.util.*;
029: import org.ofbiz.minilang.*;
030: import org.ofbiz.minilang.method.*;
031: import org.ofbiz.minilang.operation.*;
032:
033: /**
034: * Implements compare to a constant condition.
035: *
036: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
037: * @version $Revision: 1.2 $
038: * @since 2.1
039: */
040: public class CompareCondition implements Conditional {
041:
042: public static final String module = CompareCondition.class
043: .getName();
044:
045: SimpleMethod simpleMethod;
046:
047: ContextAccessor mapAcsr;
048: ContextAccessor fieldAcsr;
049: String value;
050:
051: String operator;
052: String type;
053: String format;
054:
055: public CompareCondition(Element element, SimpleMethod simpleMethod) {
056: this .simpleMethod = simpleMethod;
057:
058: this .mapAcsr = new ContextAccessor(element
059: .getAttribute("map-name"));
060: this .fieldAcsr = new ContextAccessor(element
061: .getAttribute("field-name"));
062: this .value = element.getAttribute("value");
063:
064: this .operator = element.getAttribute("operator");
065: this .type = element.getAttribute("type");
066: this .format = element.getAttribute("format");
067: }
068:
069: public boolean checkCondition(MethodContext methodContext) {
070: String value = methodContext.expandString(this .value);
071: String operator = methodContext.expandString(this .operator);
072: String type = methodContext.expandString(this .type);
073: String format = methodContext.expandString(this .format);
074:
075: Object fieldVal = null;
076: if (!mapAcsr.isEmpty()) {
077: Map fromMap = (Map) mapAcsr.get(methodContext);
078: if (fromMap == null) {
079: if (Debug.infoOn())
080: Debug.logInfo("Map not found with name " + mapAcsr
081: + ", using empty string for comparison",
082: module);
083: } else {
084: fieldVal = fieldAcsr.get(fromMap, methodContext);
085: }
086: } else {
087: // no map name, try the env
088: fieldVal = fieldAcsr.get(methodContext);
089: }
090:
091: // always use an empty string by default
092: if (fieldVal == null) {
093: fieldVal = "";
094: }
095:
096: List messages = new LinkedList();
097: Boolean resultBool = BaseCompare.doRealCompare(fieldVal, value,
098: operator, type, format, messages, null, methodContext
099: .getLoader());
100: if (messages.size() > 0) {
101: messages.add(0,
102: "Error with comparison in if-compare between field ["
103: + mapAcsr.toString() + "."
104: + fieldAcsr.toString() + "] with value ["
105: + fieldVal + "] and value [" + value
106: + "] with operator [" + operator
107: + "] and type [" + type + "]: ");
108: if (methodContext.getMethodType() == MethodContext.EVENT) {
109: StringBuffer fullString = new StringBuffer();
110:
111: Iterator miter = messages.iterator();
112: while (miter.hasNext()) {
113: fullString.append((String) miter.next());
114: }
115: Debug.logWarning(fullString.toString(), module);
116:
117: methodContext.putEnv(simpleMethod
118: .getEventErrorMessageName(), fullString
119: .toString());
120: methodContext.putEnv(simpleMethod
121: .getEventResponseCodeName(), simpleMethod
122: .getDefaultErrorCode());
123: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
124: methodContext.putEnv(simpleMethod
125: .getServiceErrorMessageListName(), messages);
126: methodContext.putEnv(simpleMethod
127: .getServiceResponseMessageName(), simpleMethod
128: .getDefaultErrorCode());
129: }
130: return false;
131: }
132:
133: if (resultBool != null)
134: return resultBool.booleanValue();
135:
136: return false;
137: }
138: }
|