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
This commit is contained in:
Deepak Dixit
2017-11-03 10:28:05 +00:00
parent 4785a20bb5
commit aae3cebccc
5 changed files with 150 additions and 6 deletions
+8
View File
@@ -77,6 +77,14 @@ under the License.
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="enabled" default="true">
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:enumeration value="true"/>
<xs:enumeration value="false"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:attributeGroup>
<xs:element name="condition">
<xs:complexType>
@@ -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;
}
}
}
@@ -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<String> getFieldNames() {
List<String> fieldNameList = new ArrayList<String>();
@@ -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<EntityEcaCondition> conditions = new ArrayList<EntityEcaCondition>();
ArrayList<Object> actionsAndSets = new ArrayList<Object>();
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;
}
}
@@ -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);
}
}