001: /*
002: * $Id: IfCompareField.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 specified field and the other 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 IfCompareField extends MethodOperation {
043:
044: public static final String module = IfCompareField.class.getName();
045:
046: List subOps = new LinkedList();
047: List elseSubOps = null;
048:
049: ContextAccessor mapAcsr;
050: ContextAccessor fieldAcsr;
051: ContextAccessor toMapAcsr;
052: ContextAccessor toFieldAcsr;
053:
054: String operator;
055: String type;
056: String format;
057:
058: public IfCompareField(Element element, SimpleMethod simpleMethod) {
059: super (element, simpleMethod);
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"), element
070: .getAttribute("field-name"));
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: SimpleMethod.readOperations(element, subOps, simpleMethod);
081: Element elseElement = UtilXml
082: .firstChildElement(element, "else");
083: if (elseElement != null) {
084: elseSubOps = new LinkedList();
085: SimpleMethod.readOperations(elseElement, elseSubOps,
086: simpleMethod);
087: }
088: }
089:
090: public boolean exec(MethodContext methodContext) {
091: // if conditions fails, always return true; if a sub-op returns false
092: // return false and stop, otherwise return true
093:
094: String operator = methodContext.expandString(this .operator);
095: String type = methodContext.expandString(this .type);
096: String format = methodContext.expandString(this .format);
097:
098: Object fieldVal1 = null;
099: Object fieldVal2 = null;
100:
101: if (!mapAcsr.isEmpty()) {
102: Map fromMap = (Map) mapAcsr.get(methodContext);
103: if (fromMap == null) {
104: if (Debug.infoOn())
105: Debug.logInfo("Map not found with name " + mapAcsr
106: + ", using null for comparison", module);
107: } else {
108: fieldVal1 = fieldAcsr.get(fromMap, methodContext);
109: }
110: } else {
111: // no map name, try the env
112: fieldVal1 = fieldAcsr.get(methodContext);
113: }
114:
115: if (!toMapAcsr.isEmpty()) {
116: Map toMap = (Map) toMapAcsr.get(methodContext);
117: if (toMap == null) {
118: if (Debug.infoOn())
119: Debug.logInfo(
120: "To Map not found with name " + toMapAcsr
121: + ", using null for comparison",
122: module);
123: } else {
124: fieldVal2 = toFieldAcsr.get(toMap, methodContext);
125: }
126: } else {
127: // no map name, try the env
128: fieldVal2 = toFieldAcsr.get(methodContext);
129: }
130:
131: List messages = new LinkedList();
132: Boolean resultBool = BaseCompare.doRealCompare(fieldVal1,
133: fieldVal2, operator, type, format, messages, null,
134: methodContext.getLoader());
135:
136: if (messages.size() > 0) {
137: messages.add(0,
138: "Error with comparison in if-compare-field between fields ["
139: + mapAcsr.toString() + "."
140: + fieldAcsr.toString() + "] with value ["
141: + fieldVal1 + "] and ["
142: + toMapAcsr.toString() + "."
143: + toFieldAcsr.toString() + "] with value ["
144: + fieldVal2 + "] with operator ["
145: + operator + "] and type [" + type + "]: ");
146: if (methodContext.getMethodType() == MethodContext.EVENT) {
147: StringBuffer fullString = new StringBuffer();
148:
149: Iterator miter = messages.iterator();
150: while (miter.hasNext()) {
151: fullString.append((String) miter.next());
152: }
153: Debug.logWarning(fullString.toString(), module);
154:
155: methodContext.putEnv(simpleMethod
156: .getEventErrorMessageName(), fullString
157: .toString());
158: methodContext.putEnv(simpleMethod
159: .getEventResponseCodeName(), simpleMethod
160: .getDefaultErrorCode());
161: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
162: methodContext.putEnv(simpleMethod
163: .getServiceErrorMessageListName(), messages);
164: methodContext.putEnv(simpleMethod
165: .getServiceResponseMessageName(), simpleMethod
166: .getDefaultErrorCode());
167: }
168: return false;
169: }
170:
171: if (resultBool != null && resultBool.booleanValue()) {
172: return SimpleMethod.runSubOps(subOps, methodContext);
173: } else {
174: if (elseSubOps != null) {
175: return SimpleMethod
176: .runSubOps(elseSubOps, methodContext);
177: } else {
178: return true;
179: }
180: }
181: }
182: }
|