svn commit: r1814160 - in /ofbiz/ofbiz-framework/trunk/framework: entity/dtd/ entityext/src/main/java/org/apache/ofbiz/entityext/eca/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1814160 - in /ofbiz/ofbiz-framework/trunk/framework: entity/dtd/ entityext/src/main/java/org/apache/ofbiz/entityext/eca/

Deepak Dixit-5
Author: deepak
Date: Fri Nov  3 10:28:05 2017
New Revision: 1814160

URL: http://svn.apache.org/viewvc?rev=1814160&view=rev
Log:
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.

Modified:
    ofbiz/ofbiz-framework/trunk/framework/entity/dtd/entity-eca.xsd
    ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaAction.java
    ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaCondition.java
    ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaRule.java
    ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaUtil.java

Modified: ofbiz/ofbiz-framework/trunk/framework/entity/dtd/entity-eca.xsd
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/dtd/entity-eca.xsd?rev=1814160&r1=1814159&r2=1814160&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/entity/dtd/entity-eca.xsd (original)
+++ ofbiz/ofbiz-framework/trunk/framework/entity/dtd/entity-eca.xsd Fri Nov  3 10:28:05 2017
@@ -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>

Modified: ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaAction.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaAction.java?rev=1814160&r1=1814159&r2=1814160&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaAction.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaAction.java Fri Nov  3 10:28:05 2017
@@ -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 imple
             }
         }
     }
+
+    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;
+        }
+    }
 }

Modified: ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaCondition.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaCondition.java?rev=1814160&r1=1814159&r2=1814160&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaCondition.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaCondition.java Fri Nov  3 10:28:05 2017
@@ -110,14 +110,44 @@ public final class EntityEcaCondition im
     @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>();

Modified: ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaRule.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaRule.java?rev=1814160&r1=1814159&r2=1814160&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaRule.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaRule.java Fri Nov  3 10:28:05 2017
@@ -57,6 +57,7 @@ public final class EntityEcaRule impleme
         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 impleme
     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;
+    }
 }

Modified: ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaUtil.java?rev=1814160&r1=1814159&r2=1814160&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaUtil.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/eca/EntityEcaUtil.java Fri Nov  3 10:28:05 2017
@@ -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);
             }
         }