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