001: /*
002: * $Id: EntityEcaRule.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: import java.util.Set;
031:
032: import org.ofbiz.base.util.Debug;
033: import org.ofbiz.base.util.UtilXml;
034: import org.ofbiz.entity.GenericEntity;
035: import org.ofbiz.entity.GenericEntityException;
036: import org.ofbiz.service.DispatchContext;
037: import org.w3c.dom.Element;
038:
039: /**
040: * EntityEcaRule
041: *
042: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
043: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
044: * @version $Revision: 1.1 $
045: * @since 2.1
046: */
047: public class EntityEcaRule {
048:
049: public static final String module = EntityEcaRule.class.getName();
050:
051: protected String entityName = null;
052: protected String operationName = null;
053: protected String eventName = null;
054: protected boolean runOnError = false;
055: protected List conditions = new LinkedList();
056: protected List actions = new LinkedList();
057:
058: protected EntityEcaRule() {
059: }
060:
061: public EntityEcaRule(Element eca) {
062: this .entityName = eca.getAttribute("entity");
063: this .operationName = eca.getAttribute("operation");
064: this .eventName = eca.getAttribute("event");
065: this .runOnError = "true".equals(eca
066: .getAttribute("run-on-error"));
067:
068: List condList = UtilXml.childElementList(eca, "condition");
069: Iterator ci = condList.iterator();
070: while (ci.hasNext()) {
071: conditions.add(new EntityEcaCondition((Element) ci.next(),
072: true));
073: }
074:
075: List condFList = UtilXml.childElementList(eca,
076: "condition-field");
077: Iterator cfi = condFList.iterator();
078: while (cfi.hasNext()) {
079: conditions.add(new EntityEcaCondition((Element) cfi.next(),
080: false));
081: }
082:
083: if (Debug.verboseOn())
084: Debug.logVerbose("Conditions: " + conditions, module);
085:
086: List actList = UtilXml.childElementList(eca, "action");
087: Iterator ai = actList.iterator();
088: while (ai.hasNext()) {
089: actions.add(new EntityEcaAction((Element) ai.next()));
090: }
091:
092: if (Debug.verboseOn())
093: Debug.logVerbose("Actions: " + actions, module);
094: }
095:
096: public void eval(String currentOperation, DispatchContext dctx,
097: GenericEntity value, boolean isError, Set actionsRun)
098: throws GenericEntityException {
099: //Debug.logInfo("eval eeca rule: operation=" + currentOperation + ", in event=" + this.eventName + ", on entity=" + this.entityName + ", for value=" + value, module);
100:
101: if (isError && !this .runOnError) {
102: return;
103: }
104:
105: if (!"any".equals(this .operationName)
106: && this .operationName.indexOf(currentOperation) == -1) {
107: return;
108: }
109:
110: boolean allCondTrue = true;
111: Iterator c = conditions.iterator();
112: while (c.hasNext()) {
113: EntityEcaCondition ec = (EntityEcaCondition) c.next();
114: if (!ec.eval(dctx, value)) {
115: allCondTrue = false;
116: break;
117: }
118: }
119:
120: if (allCondTrue) {
121: Iterator a = actions.iterator();
122: while (a.hasNext()) {
123: EntityEcaAction ea = (EntityEcaAction) a.next();
124: // in order to enable OR logic without multiple calls to the given service,
125: //only execute a given service name once per service call phase
126: if (!actionsRun.contains(ea.serviceName)) {
127: ea.runAction(dctx, value);
128: actionsRun.add(ea.serviceName);
129: }
130: }
131: }
132: }
133: }
|