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