001: /*
002: * $Id: ServiceEcaCondition.java,v 1.2 2003/09/25 21:23:10 ajzeneski Exp $
003: *
004: * Copyright (c) 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: */
025: package org.ofbiz.service.eca;
026:
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.List;
030: import java.util.Map;
031:
032: import org.ofbiz.service.DispatchContext;
033: import org.ofbiz.service.GenericServiceException;
034: import org.ofbiz.base.util.Debug;
035: import org.ofbiz.base.util.ObjectType;
036: import org.w3c.dom.Element;
037:
038: /**
039: * ServiceEcaCondition
040: *
041: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
042: * @version $Revision: 1.2 $
043: * @since 2.0
044: */
045: public class ServiceEcaCondition {
046:
047: public static final String module = ServiceEcaCondition.class
048: .getName();
049:
050: protected String lhsValueName, rhsValueName;
051: protected String lhsMapName, rhsMapName;
052: protected String operator;
053: protected String compareType;
054: protected String format;
055: protected boolean constant = false;
056:
057: protected ServiceEcaCondition() {
058: }
059:
060: public ServiceEcaCondition(Element condition, boolean constant) {
061: this .lhsValueName = condition.getAttribute("field-name");
062: this .lhsMapName = condition.getAttribute("map-name");
063:
064: this .constant = constant;
065: if (constant) {
066: this .rhsValueName = condition.getAttribute("value");
067: this .rhsMapName = null;
068: } else {
069: this .rhsValueName = condition.getAttribute("to-field-name");
070: this .rhsMapName = condition.getAttribute("to-map-name");
071: }
072:
073: this .operator = condition.getAttribute("operator");
074: this .compareType = condition.getAttribute("type");
075: this .format = condition.getAttribute("format");
076:
077: if (lhsValueName == null)
078: lhsValueName = "";
079: if (rhsValueName == null)
080: rhsValueName = "";
081: }
082:
083: public boolean eval(String serviceName, DispatchContext dctx,
084: Map context) throws GenericServiceException {
085: if (serviceName == null || dctx == null || context == null
086: || dctx.getClassLoader() == null) {
087: throw new GenericServiceException(
088: "Cannot have null Service, Context or DispatchContext!");
089: }
090:
091: if (Debug.verboseOn())
092: Debug.logVerbose(this .toString() + ", In the context: "
093: + context, module);
094:
095: Object lhsValue = null;
096: Object rhsValue = null;
097: if (lhsMapName != null && lhsMapName.length() > 0) {
098: try {
099: if (context.containsKey(lhsMapName)) {
100: Map envMap = (Map) context.get(lhsMapName);
101: lhsValue = envMap.get(lhsValueName);
102: } else {
103: Debug
104: .logWarning(
105: "From Map ("
106: + lhsMapName
107: + ") not found in context, defaulting to null.",
108: module);
109: }
110: } catch (ClassCastException e) {
111: throw new GenericServiceException("From Map field ["
112: + lhsMapName + "] is not a Map.", e);
113: }
114: } else {
115: if (context.containsKey(lhsValueName)) {
116: lhsValue = context.get(lhsValueName);
117: } else {
118: Debug
119: .logInfo(
120: "From Field ("
121: + lhsValueName
122: + ") is not found in context for "
123: + serviceName
124: + ", defaulting to null.",
125: module);
126: }
127: }
128:
129: if (constant) {
130: rhsValue = rhsValueName;
131: } else if (rhsMapName != null && rhsMapName.length() > 0) {
132: try {
133: if (context.containsKey(rhsMapName)) {
134: Map envMap = (Map) context.get(rhsMapName);
135: rhsValue = envMap.get(rhsValueName);
136: } else {
137: Debug.logWarning("To Map (" + rhsMapName
138: + ") not found in context for "
139: + serviceName + ", defaulting to null.",
140: module);
141: }
142: } catch (ClassCastException e) {
143: throw new GenericServiceException("To Map field ["
144: + rhsMapName + "] is not a Map.", e);
145: }
146: } else {
147: if (context.containsKey(rhsValueName)) {
148: rhsValue = context.get(rhsValueName);
149: } else {
150: Debug
151: .logInfo(
152: "To Field ("
153: + rhsValueName
154: + ") is not found in context for "
155: + serviceName
156: + ", defaulting to null.",
157: module);
158: }
159: }
160:
161: if (Debug.verboseOn())
162: Debug.logVerbose("Comparing : " + lhsValue + " " + operator
163: + " " + rhsValue, module);
164:
165: // evaluate the condition & invoke the action(s)
166: List messages = new LinkedList();
167: Boolean cond = ObjectType.doRealCompare(lhsValue, rhsValue,
168: operator, compareType, format, messages, null, dctx
169: .getClassLoader());
170:
171: // if any messages were returned send them out
172: if (messages.size() > 0) {
173: Iterator m = messages.iterator();
174: while (m.hasNext()) {
175: Debug.logWarning((String) m.next(), module);
176: }
177: }
178: if (cond != null) {
179: return cond.booleanValue();
180: } else {
181: Debug.logWarning(
182: "doRealCompare returned null, returning false",
183: module);
184: return false;
185: }
186: }
187:
188: public String toString() {
189: StringBuffer buf = new StringBuffer();
190:
191: buf.append("[" + lhsMapName + "]");
192: buf.append("[" + lhsValueName + "]");
193: buf.append("[" + operator + "]");
194: buf.append("[" + rhsMapName + "]");
195: buf.append("[" + rhsValueName + "]");
196: buf.append("[" + constant + "]");
197: buf.append("[" + compareType + "]");
198: buf.append("[" + format + "]");
199: return buf.toString();
200: }
201: }
|