001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.ifops;
019:
020: import java.util.*;
021:
022: import org.w3c.dom.*;
023: import org.ofbiz.base.util.*;
024: import org.ofbiz.minilang.*;
025: import org.ofbiz.minilang.method.*;
026:
027: import org.ofbiz.minilang.operation.*;
028:
029: /**
030: * Iff the comparison between the specified field and the other field is true process sub-operations
031: */
032: public class IfCompareField extends MethodOperation {
033:
034: public static final String module = IfCompareField.class.getName();
035:
036: List subOps = new LinkedList();
037: List elseSubOps = null;
038:
039: ContextAccessor mapAcsr;
040: ContextAccessor fieldAcsr;
041: ContextAccessor toMapAcsr;
042: ContextAccessor toFieldAcsr;
043:
044: String operator;
045: String type;
046: String format;
047:
048: public IfCompareField(Element element, SimpleMethod simpleMethod) {
049: super (element, simpleMethod);
050: this .mapAcsr = new ContextAccessor(element
051: .getAttribute("map-name"));
052: this .fieldAcsr = new ContextAccessor(element
053: .getAttribute("field-name"));
054:
055: this .toMapAcsr = new ContextAccessor(element
056: .getAttribute("to-map-name"));
057: // set fieldAcsr to their defualt value of fieldAcsr if empty
058: this .toFieldAcsr = new ContextAccessor(element
059: .getAttribute("to-field-name"), element
060: .getAttribute("field-name"));
061:
062: // do NOT default the to-map-name to the map-name because that
063: //would make it impossible to compare from a map field to an
064: //environment field
065:
066: this .operator = element.getAttribute("operator");
067: this .type = element.getAttribute("type");
068: this .format = element.getAttribute("format");
069:
070: SimpleMethod.readOperations(element, subOps, simpleMethod);
071: Element elseElement = UtilXml
072: .firstChildElement(element, "else");
073: if (elseElement != null) {
074: elseSubOps = new LinkedList();
075: SimpleMethod.readOperations(elseElement, elseSubOps,
076: simpleMethod);
077: }
078: }
079:
080: public boolean exec(MethodContext methodContext) {
081: // if conditions fails, always return true; if a sub-op returns false
082: // return false and stop, otherwise return true
083:
084: String operator = methodContext.expandString(this .operator);
085: String type = methodContext.expandString(this .type);
086: String format = methodContext.expandString(this .format);
087:
088: Object fieldVal1 = null;
089: Object fieldVal2 = null;
090:
091: if (!mapAcsr.isEmpty()) {
092: Map fromMap = (Map) mapAcsr.get(methodContext);
093: if (fromMap == null) {
094: if (Debug.infoOn())
095: Debug.logInfo("Map not found with name " + mapAcsr
096: + ", using null for comparison", module);
097: } else {
098: fieldVal1 = fieldAcsr.get(fromMap, methodContext);
099: }
100: } else {
101: // no map name, try the env
102: fieldVal1 = fieldAcsr.get(methodContext);
103: }
104:
105: if (!toMapAcsr.isEmpty()) {
106: Map toMap = (Map) toMapAcsr.get(methodContext);
107: if (toMap == null) {
108: if (Debug.infoOn())
109: Debug.logInfo(
110: "To Map not found with name " + toMapAcsr
111: + ", using null for comparison",
112: module);
113: } else {
114: fieldVal2 = toFieldAcsr.get(toMap, methodContext);
115: }
116: } else {
117: // no map name, try the env
118: fieldVal2 = toFieldAcsr.get(methodContext);
119: }
120:
121: List messages = new LinkedList();
122: Boolean resultBool = BaseCompare.doRealCompare(fieldVal1,
123: fieldVal2, operator, type, format, messages, null,
124: methodContext.getLoader(), false);
125:
126: if (messages.size() > 0) {
127: messages.add(0,
128: "Error with comparison in if-compare-field between fields ["
129: + mapAcsr.toString() + "."
130: + fieldAcsr.toString() + "] with value ["
131: + fieldVal1 + "] and ["
132: + toMapAcsr.toString() + "."
133: + toFieldAcsr.toString() + "] with value ["
134: + fieldVal2 + "] with operator ["
135: + operator + "] and type [" + type + "]: ");
136: if (methodContext.getMethodType() == MethodContext.EVENT) {
137: StringBuffer fullString = new StringBuffer();
138:
139: Iterator miter = messages.iterator();
140: while (miter.hasNext()) {
141: fullString.append((String) miter.next());
142: }
143: Debug.logWarning(fullString.toString(), module);
144:
145: methodContext.putEnv(simpleMethod
146: .getEventErrorMessageName(), fullString
147: .toString());
148: methodContext.putEnv(simpleMethod
149: .getEventResponseCodeName(), simpleMethod
150: .getDefaultErrorCode());
151: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
152: methodContext.putEnv(simpleMethod
153: .getServiceErrorMessageListName(), messages);
154: methodContext.putEnv(simpleMethod
155: .getServiceResponseMessageName(), simpleMethod
156: .getDefaultErrorCode());
157: }
158: return false;
159: }
160:
161: if (resultBool != null && resultBool.booleanValue()) {
162: return SimpleMethod.runSubOps(subOps, methodContext);
163: } else {
164: if (elseSubOps != null) {
165: return SimpleMethod
166: .runSubOps(elseSubOps, methodContext);
167: } else {
168: return true;
169: }
170: }
171: }
172:
173: public String rawString() {
174: // TODO: add all attributes and other info
175: return "<if-compare-field field-name=\"" + this .fieldAcsr
176: + "\" map-name=\"" + this .mapAcsr + "\"/>";
177: }
178:
179: public String expandedString(MethodContext methodContext) {
180: // TODO: something more than a stub/dummy
181: return this.rawString();
182: }
183: }
|