001: /*
002: * $Id: CompareFieldCondition.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 field 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 CompareFieldCondition implements Conditional {
041:
042: public static final String module = CompareFieldCondition.class
043: .getName();
044:
045: SimpleMethod simpleMethod;
046:
047: ContextAccessor mapAcsr;
048: ContextAccessor fieldAcsr;
049: ContextAccessor toMapAcsr;
050: ContextAccessor toFieldAcsr;
051:
052: String operator;
053: String type;
054: String format;
055:
056: public CompareFieldCondition(Element element,
057: SimpleMethod simpleMethod) {
058: this .simpleMethod = simpleMethod;
059:
060: this .mapAcsr = new ContextAccessor(element
061: .getAttribute("map-name"));
062: this .fieldAcsr = new ContextAccessor(element
063: .getAttribute("field-name"));
064:
065: this .toMapAcsr = new ContextAccessor(element
066: .getAttribute("to-map-name"));
067: // set fieldAcsr to their defualt value of fieldAcsr if empty
068: this .toFieldAcsr = new ContextAccessor(element
069: .getAttribute("to-field-name"), this .fieldAcsr
070: .toString());
071:
072: // do NOT default the to-map-name to the map-name because that
073: //would make it impossible to compare from a map field to an
074: //environment field
075:
076: this .operator = element.getAttribute("operator");
077: this .type = element.getAttribute("type");
078: this .format = element.getAttribute("format");
079: }
080:
081: public boolean checkCondition(MethodContext methodContext) {
082: String operator = methodContext.expandString(this .operator);
083: String type = methodContext.expandString(this .type);
084: String format = methodContext.expandString(this .format);
085:
086: Object fieldVal1 = null;
087: Object fieldVal2 = null;
088:
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 null for comparison", module);
095: } else {
096: fieldVal1 = fieldAcsr.get(fromMap, methodContext);
097: }
098: } else {
099: // no map name, try the env
100: fieldVal1 = fieldAcsr.get(methodContext);
101: }
102:
103: if (!toMapAcsr.isEmpty()) {
104: Map toMap = (Map) toMapAcsr.get(methodContext);
105: if (toMap == null) {
106: if (Debug.infoOn())
107: Debug.logInfo(
108: "To Map not found with name " + toMapAcsr
109: + ", using null for comparison",
110: module);
111: } else {
112: fieldVal2 = toFieldAcsr.get(toMap, methodContext);
113: }
114: } else {
115: // no map name, try the env
116: fieldVal2 = toFieldAcsr.get(methodContext);
117: }
118:
119: List messages = new LinkedList();
120: Boolean resultBool = BaseCompare.doRealCompare(fieldVal1,
121: fieldVal2, operator, type, format, messages, null,
122: methodContext.getLoader());
123:
124: if (messages.size() > 0) {
125: messages.add(0,
126: "Error with comparison in if-compare-field between fields ["
127: + mapAcsr.toString() + "."
128: + fieldAcsr.toString() + "] with value ["
129: + fieldVal1 + "] and ["
130: + toMapAcsr.toString() + "."
131: + toFieldAcsr.toString() + "] with value ["
132: + fieldVal2 + "] with operator ["
133: + operator + "] and type [" + type + "]: ");
134: if (methodContext.getMethodType() == MethodContext.EVENT) {
135: StringBuffer fullString = new StringBuffer();
136:
137: Iterator miter = messages.iterator();
138: while (miter.hasNext()) {
139: fullString.append((String) miter.next());
140: }
141: Debug.logWarning(fullString.toString(), module);
142:
143: methodContext.putEnv(simpleMethod
144: .getEventErrorMessageName(), fullString
145: .toString());
146: methodContext.putEnv(simpleMethod
147: .getEventResponseCodeName(), simpleMethod
148: .getDefaultErrorCode());
149: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
150: methodContext.putEnv(simpleMethod
151: .getServiceErrorMessageListName(), messages);
152: methodContext.putEnv(simpleMethod
153: .getServiceResponseMessageName(), simpleMethod
154: .getDefaultErrorCode());
155: }
156: return false;
157: }
158:
159: if (resultBool != null)
160: return resultBool.booleanValue();
161:
162: return false;
163: }
164: }
|