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.entityext.eca;
019:
020: import java.util.Iterator;
021: import java.util.LinkedList;
022: import java.util.List;
023: import java.util.Set;
024:
025: import org.ofbiz.base.util.Debug;
026: import org.ofbiz.base.util.UtilXml;
027: import org.ofbiz.entity.GenericEntity;
028: import org.ofbiz.entity.GenericEntityException;
029: import org.ofbiz.service.DispatchContext;
030: import org.w3c.dom.Element;
031:
032: /**
033: * EntityEcaRule
034: */
035: public class EntityEcaRule implements java.io.Serializable {
036:
037: public static final String module = EntityEcaRule.class.getName();
038:
039: protected String entityName = null;
040: protected String operationName = null;
041: protected String eventName = null;
042: protected boolean runOnError = false;
043: protected List conditions = new LinkedList();
044: protected List actions = new LinkedList();
045: protected boolean enabled = true;
046:
047: protected EntityEcaRule() {
048: }
049:
050: public EntityEcaRule(Element eca) {
051: this .entityName = eca.getAttribute("entity");
052: this .operationName = eca.getAttribute("operation");
053: this .eventName = eca.getAttribute("event");
054: this .runOnError = "true".equals(eca
055: .getAttribute("run-on-error"));
056:
057: List condList = UtilXml.childElementList(eca, "condition");
058: Iterator ci = condList.iterator();
059: while (ci.hasNext()) {
060: conditions.add(new EntityEcaCondition((Element) ci.next(),
061: true));
062: }
063:
064: List condFList = UtilXml.childElementList(eca,
065: "condition-field");
066: Iterator cfi = condFList.iterator();
067: while (cfi.hasNext()) {
068: conditions.add(new EntityEcaCondition((Element) cfi.next(),
069: false));
070: }
071:
072: if (Debug.verboseOn())
073: Debug.logVerbose("Conditions: " + conditions, module);
074:
075: List actList = UtilXml.childElementList(eca, "action");
076: Iterator ai = actList.iterator();
077: while (ai.hasNext()) {
078: actions.add(new EntityEcaAction((Element) ai.next()));
079: }
080:
081: if (Debug.verboseOn())
082: Debug.logVerbose("Actions: " + actions, module);
083: }
084:
085: public void eval(String currentOperation, DispatchContext dctx,
086: GenericEntity value, boolean isError, Set actionsRun)
087: throws GenericEntityException {
088: if (!enabled) {
089: Debug.logInfo("Entity ECA [" + this .entityName + "] on ["
090: + this .eventName + "] is disabled; not running.",
091: module);
092: return;
093: }
094:
095: //Debug.logInfo("eval eeca rule: operation=" + currentOperation + ", in event=" + this.eventName + ", on entity=" + this.entityName + ", for value=" + value, module);
096: if (isError && !this .runOnError) {
097: return;
098: }
099:
100: if (!"any".equals(this .operationName)
101: && this .operationName.indexOf(currentOperation) == -1) {
102: return;
103: }
104:
105: boolean allCondTrue = true;
106: Iterator c = conditions.iterator();
107: while (c.hasNext()) {
108: EntityEcaCondition ec = (EntityEcaCondition) c.next();
109: if (!ec.eval(dctx, value)) {
110: allCondTrue = false;
111: break;
112: }
113: }
114:
115: if (allCondTrue) {
116: Iterator a = actions.iterator();
117: while (a.hasNext()) {
118: EntityEcaAction ea = (EntityEcaAction) a.next();
119: // in order to enable OR logic without multiple calls to the given service,
120: //only execute a given service name once per service call phase
121: if (!actionsRun.contains(ea.serviceName)) {
122: if (Debug.infoOn())
123: Debug.logInfo("Running Entity ECA Service: "
124: + ea.serviceName
125: + ", triggered by rule on Entity: "
126: + value.getEntityName(), module);
127: ea.runAction(dctx, value);
128: actionsRun.add(ea.serviceName);
129: }
130: }
131: }
132: }
133:
134: public void setEnabled(boolean enabled) {
135: this .enabled = enabled;
136: }
137:
138: public boolean isEnabled() {
139: return this.enabled;
140: }
141: }
|