001: /*
002: * $Id: EntityEcaCondition.java,v 1.1 2003/08/17 06:44:25 jonesde Exp $
003: *
004: * Copyright (c) 2002-2003 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.entityext.eca;
026:
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.List;
030:
031: import org.ofbiz.base.util.Debug;
032: import org.ofbiz.base.util.ObjectType;
033: import org.ofbiz.entity.GenericEntity;
034: import org.ofbiz.entity.GenericEntityException;
035: import org.ofbiz.service.DispatchContext;
036: import org.w3c.dom.Element;
037:
038: /**
039: * EntityEcaCondition
040: *
041: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
042: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
043: * @version $Revision: 1.1 $
044: * @since 2.1
045: */
046: public class EntityEcaCondition {
047:
048: public static final String module = EntityEcaCondition.class
049: .getName();
050:
051: protected String lhsValueName, rhsValueName;
052: protected String operator;
053: protected String compareType;
054: protected String format;
055: protected boolean constant = false;
056:
057: protected EntityEcaCondition() {
058: }
059:
060: public EntityEcaCondition(Element condition, boolean constant) {
061: this .lhsValueName = condition.getAttribute("field-name");
062:
063: this .constant = constant;
064: if (constant) {
065: this .rhsValueName = condition.getAttribute("value");
066: } else {
067: this .rhsValueName = condition.getAttribute("to-field-name");
068: }
069:
070: this .operator = condition.getAttribute("operator");
071: this .compareType = condition.getAttribute("type");
072: this .format = condition.getAttribute("format");
073:
074: if (lhsValueName == null)
075: lhsValueName = "";
076: if (rhsValueName == null)
077: rhsValueName = "";
078: }
079:
080: public boolean eval(DispatchContext dctx, GenericEntity value)
081: throws GenericEntityException {
082: if (dctx == null || value == null
083: || dctx.getClassLoader() == null) {
084: throw new GenericEntityException(
085: "Cannot have null Value or DispatchContext!");
086: }
087:
088: if (Debug.verboseOn())
089: Debug.logVerbose(this .toString(), module);
090:
091: Object lhsValue = value.get(lhsValueName);
092:
093: Object rhsValue;
094: if (constant) {
095: rhsValue = rhsValueName;
096: } else {
097: rhsValue = value.get(rhsValueName);
098: }
099:
100: if (Debug.verboseOn())
101: Debug.logVerbose("Comparing : " + lhsValue + " " + operator
102: + " " + rhsValue, module);
103:
104: // evaluate the condition & invoke the action(s)
105: List messages = new LinkedList();
106: Boolean cond = ObjectType.doRealCompare(lhsValue, rhsValue,
107: operator, compareType, format, messages, null, dctx
108: .getClassLoader());
109:
110: // if any messages were returned send them out
111: if (messages.size() > 0) {
112: Iterator m = messages.iterator();
113: while (m.hasNext()) {
114: Debug.logWarning((String) m.next(), module);
115: }
116: }
117: if (cond != null) {
118: return cond.booleanValue();
119: } else {
120: return false;
121: }
122: }
123:
124: public String toString() {
125: StringBuffer buf = new StringBuffer();
126: buf.append("[" + lhsValueName + "]");
127: buf.append("[" + operator + "]");
128: buf.append("[" + rhsValueName + "]");
129: buf.append("[" + constant + "]");
130: buf.append("[" + compareType + "]");
131: buf.append("[" + format + "]");
132: return buf.toString();
133: }
134: }
|