From aae3cebccc184cf519f33e2e2d91c0806f68145c Mon Sep 17 00:00:00 2001 From: Deepak Dixit Date: Fri, 3 Nov 2017 10:28:05 +0000 Subject: [PATCH] Improved: Add ability to disable entity eca rule (OFBIZ-9897) We have enabled flag in EntityEcaRule class, if its set false then entity eca rule will not be execute. But there is not way to disable entity eca. We can add enabled flag in Entity ECA definition to disable the existing entity eca rule. Add new attribute on entity eca tag named enabled Default value will be true for this. If user want to disable existing OOTB entity eca rule, then user can define same rule in custom component and set the enabled=false Also as per current flow if same seca rule is define more then once, system will execute all the rule, ideally it should not execute same rule (same rule, condition and action) if its defined more than one. Also need to ovride hasCode, equals and toString method for EntityEcaRule, EntityEcaAction and EntityEcaCondition class file. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1814160 13f79535-47bb-0310-9956-ffa450edef68 --- framework/entity/dtd/entity-eca.xsd | 8 +++ .../ofbiz/entityext/eca/EntityEcaAction.java | 47 ++++++++++++++++ .../entityext/eca/EntityEcaCondition.java | 42 ++++++++++++--- .../ofbiz/entityext/eca/EntityEcaRule.java | 54 +++++++++++++++++++ .../ofbiz/entityext/eca/EntityEcaUtil.java | 5 ++ 5 files changed, 150 insertions(+), 6 deletions(-) diff --git a/framework/entity/dtd/entity-eca.xsd b/framework/entity/dtd/entity-eca.xsd index 1c61f099a2..0dc3f76826 100644 --- a/framework/entity/dtd/entity-eca.xsd +++ b/framework/entity/dtd/entity-eca.xsd @@ -77,6 +77,14 @@ under the License. + + + + + + + + diff --git a/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaAction.java b/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaAction.java index 542855d363..aa61ed2c8e 100644 --- a/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaAction.java +++ b/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaAction.java @@ -22,6 +22,7 @@ import java.util.Map; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.UtilMisc; +import org.apache.ofbiz.base.util.UtilValidate; import org.apache.ofbiz.entity.GenericEntity; import org.apache.ofbiz.entity.GenericEntityException; import org.apache.ofbiz.entity.GenericValue; @@ -115,4 +116,50 @@ public final class EntityEcaAction implements java.io.Serializable { } } } + + public String toString() { + StringBuilder buf = new StringBuilder(); + if (UtilValidate.isNotEmpty(serviceName)) buf.append("[").append(serviceName).append("]"); + if (UtilValidate.isNotEmpty(serviceMode)) buf.append("[").append(serviceMode).append("]"); + if (UtilValidate.isNotEmpty(runAsUser)) buf.append("[").append(runAsUser).append("]"); + if (UtilValidate.isNotEmpty(valueAttr)) buf.append("[").append(valueAttr).append("]"); + if (UtilValidate.isNotEmpty(resultToValue)) buf.append("[").append(resultToValue).append("]"); + if (UtilValidate.isNotEmpty(abortOnError)) buf.append("[").append(abortOnError).append("]"); + if (UtilValidate.isNotEmpty(rollbackOnError)) buf.append("[").append(rollbackOnError).append("]"); + if (UtilValidate.isNotEmpty(persist)) buf.append("[").append(persist).append("]"); + return buf.toString(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((serviceName == null) ? 0 : serviceName.hashCode()); + result = prime * result + ((serviceMode == null) ? 0 : serviceMode.hashCode()); + result = prime * result + ((runAsUser == null) ? 0 : runAsUser.hashCode()); + result = prime * result + ((valueAttr == null) ? 0 : valueAttr.hashCode()); + result = prime * result + (resultToValue ? 1231 : 1237); + result = prime * result + (abortOnError ? 1231 : 1237); + result = prime * result + (rollbackOnError ? 1231 : 1237); + result = prime * result + (persist ? 1231 : 1237); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof EntityEcaAction) { + EntityEcaAction other = (EntityEcaAction) obj; + if (!UtilValidate.areEqual(this.serviceName, other.serviceName)) return false; + if (!UtilValidate.areEqual(this.serviceMode, other.serviceMode)) return false; + if (!UtilValidate.areEqual(this.runAsUser, other.runAsUser)) return false; + if (!UtilValidate.areEqual(this.valueAttr, other.valueAttr)) return false; + if (this.resultToValue != other.resultToValue) return false; + if (this.abortOnError != other.abortOnError) return false; + if (this.rollbackOnError != other.rollbackOnError) return false; + if (this.persist != other.persist) return false; + return true; + } else { + return false; + } + } } diff --git a/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaCondition.java b/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaCondition.java index c011203148..f174668ba5 100644 --- a/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaCondition.java +++ b/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaCondition.java @@ -110,14 +110,44 @@ public final class EntityEcaCondition implements java.io.Serializable { @Override public String toString() { StringBuilder buf = new StringBuilder(); - buf.append("[").append(lhsValueName).append("]"); - buf.append("[").append(operator).append("]"); - buf.append("[").append(rhsValueName).append("]"); - buf.append("[").append(constant).append("]"); - buf.append("[").append(compareType).append("]"); - buf.append("[").append(format).append("]"); + if (UtilValidate.isNotEmpty(lhsValueName)) buf.append("[").append(lhsValueName).append("]"); + if (UtilValidate.isNotEmpty(operator)) buf.append("[").append(operator).append("]"); + if (UtilValidate.isNotEmpty(rhsValueName)) buf.append("[").append(rhsValueName).append("]"); + if (UtilValidate.isNotEmpty(constant)) buf.append("[").append(constant).append("]"); + if (UtilValidate.isNotEmpty(compareType)) buf.append("[").append(compareType).append("]"); + if (UtilValidate.isNotEmpty(format)) buf.append("[").append(format).append("]"); return buf.toString(); } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((lhsValueName == null) ? 0 : lhsValueName.hashCode()); + result = prime * result + ((operator == null) ? 0 : operator.hashCode()); + result = prime * result + ((rhsValueName == null) ? 0 : rhsValueName.hashCode()); + result = prime * result + (constant ? 1231 : 1237); + result = prime * result + ((compareType == null) ? 0 : compareType.hashCode()); + result = prime * result + ((format == null) ? 0 : format.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof EntityEcaCondition) { + EntityEcaCondition other = (EntityEcaCondition) obj; + + if (!UtilValidate.areEqual(this.lhsValueName, other.lhsValueName)) return false; + if (!UtilValidate.areEqual(this.rhsValueName, other.rhsValueName)) return false; + if (!UtilValidate.areEqual(this.operator, other.operator)) return false; + if (!UtilValidate.areEqual(this.compareType, other.compareType)) return false; + if (!UtilValidate.areEqual(this.format, other.format)) return false; + if (this.constant != other.constant) return false; + + return true; + } else { + return false; + } + } protected List getFieldNames() { List fieldNameList = new ArrayList(); diff --git a/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaRule.java b/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaRule.java index 3b7d8a97a4..a1b14f39f6 100644 --- a/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaRule.java +++ b/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaRule.java @@ -57,6 +57,7 @@ public final class EntityEcaRule implements java.io.Serializable { this.operationName = eca.getAttribute("operation"); this.eventName = eca.getAttribute("event"); this.runOnError = "true".equals(eca.getAttribute("run-on-error")); + this.enabled = !"false".equals(eca.getAttribute("enabled")); ArrayList conditions = new ArrayList(); ArrayList actionsAndSets = new ArrayList(); for (Element element: UtilXml.childElementList(eca)) { @@ -185,4 +186,57 @@ public final class EntityEcaRule implements java.io.Serializable { public boolean isEnabled() { return this.enabled; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((entityName == null) ? 0 : entityName.hashCode()); + result = prime * result + ((operationName == null) ? 0 : operationName.hashCode()); + result = prime * result + ((eventName == null) ? 0 : eventName.hashCode()); + result = prime * result + ((actionsAndSets == null) ? 0 : actionsAndSets.hashCode()); + result = prime * result + ((conditions == null) ? 0 : conditions.hashCode()); + result = prime * result + ((conditionFieldNames == null) ? 0 : conditionFieldNames.hashCode()); + result = prime * result + (enabled ? 1231 : 1237); + result = prime * result + (runOnError ? 1231 : 1237); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof EntityEcaRule) { + EntityEcaRule other = (EntityEcaRule) obj; + if (!UtilValidate.areEqual(this.entityName, other.entityName)) { + return false; + } + if (!UtilValidate.areEqual(this.operationName, other.operationName)) { + return false; + } + if (!UtilValidate.areEqual(this.eventName, other.eventName)) { + return false; + } + if (!this.conditions.equals(other.conditions)) { + return false; + } + if (!this.actionsAndSets.equals(other.actionsAndSets)) { + return false; + } + if (!this.conditionFieldNames.equals(other.conditionFieldNames)) { + return false; + } + + if (this.runOnError != other.runOnError) { + return false; + } + + return true; + } else { + return false; + } + } + + @Override + public String toString() { + return "EntityEcaRule:" + this.entityName + ":" + this.operationName + ":" + this.eventName + ":runOnError=" + this.runOnError + ":enabled=" + this.enabled + ":conditions=" + this.conditions + ":actionsAndSets=" + this.actionsAndSets + ":conditionFieldNames" + this.conditionFieldNames; + } } diff --git a/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaUtil.java b/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaUtil.java index 5dcee83104..b403f6cac6 100644 --- a/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaUtil.java +++ b/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaUtil.java @@ -121,6 +121,11 @@ public final class EntityEcaUtil { eventMap.put(eventName, rules); } } + //remove the old rule if found and keep the recent one + //This will prevent duplicate rule execution along with enabled/disabled eca workflow + if (rules.remove(rule)) { + Debug.logWarning("Duplicate Entity ECA [" + entityName + "]" + "for operation [ "+ rule.getOperationName() + "] " + "on [" + eventName + "] ", module); + } rules.add(rule); } }