1 25 package org.ofbiz.entityext.eca; 26 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Set ; 31 32 import org.ofbiz.base.util.Debug; 33 import org.ofbiz.base.util.UtilXml; 34 import org.ofbiz.entity.GenericEntity; 35 import org.ofbiz.entity.GenericEntityException; 36 import org.ofbiz.service.DispatchContext; 37 import org.w3c.dom.Element ; 38 39 47 public class EntityEcaRule implements java.io.Serializable { 48 49 public static final String module = EntityEcaRule.class.getName(); 50 51 protected String entityName = null; 52 protected String operationName = null; 53 protected String eventName = null; 54 protected boolean runOnError = false; 55 protected List conditions = new LinkedList (); 56 protected List actions = new LinkedList (); 57 protected boolean enabled = true; 58 59 protected EntityEcaRule() {} 60 61 public EntityEcaRule(Element eca) { 62 this.entityName = eca.getAttribute("entity"); 63 this.operationName = eca.getAttribute("operation"); 64 this.eventName = eca.getAttribute("event"); 65 this.runOnError = "true".equals(eca.getAttribute("run-on-error")); 66 67 List condList = UtilXml.childElementList(eca, "condition"); 68 Iterator ci = condList.iterator(); 69 while (ci.hasNext()) { 70 conditions.add(new EntityEcaCondition((Element ) ci.next(), true)); 71 } 72 73 List condFList = UtilXml.childElementList(eca, "condition-field"); 74 Iterator cfi = condFList.iterator(); 75 while (cfi.hasNext()) { 76 conditions.add(new EntityEcaCondition((Element ) cfi.next(), false)); 77 } 78 79 if (Debug.verboseOn()) Debug.logVerbose("Conditions: " + conditions, module); 80 81 List actList = UtilXml.childElementList(eca, "action"); 82 Iterator ai = actList.iterator(); 83 while (ai.hasNext()) { 84 actions.add(new EntityEcaAction((Element ) ai.next())); 85 } 86 87 if (Debug.verboseOn()) Debug.logVerbose("Actions: " + actions, module); 88 } 89 90 public void eval(String currentOperation, DispatchContext dctx, GenericEntity value, boolean isError, Set actionsRun) throws GenericEntityException { 91 if (!enabled) { 92 Debug.logInfo("Entity ECA [" + this.entityName + "] on [" + this.eventName + "] is disabled; not running.", module); 93 return; 94 } 95 96 if (isError && !this.runOnError) { 98 return; 99 } 100 101 if (!"any".equals(this.operationName) && 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 if (!actionsRun.contains(ea.serviceName)) { 122 if (Debug.infoOn()) Debug.logInfo("Running Entity ECA Service: " + ea.serviceName + ", triggered by rule on Entity: " + value.getEntityName(), module); 123 ea.runAction(dctx, value); 124 actionsRun.add(ea.serviceName); 125 } 126 } 127 } 128 } 129 130 public void setEnabled(boolean enabled) { 131 this.enabled = enabled; 132 } 133 134 public boolean isEnabled() { 135 return this.enabled; 136 } 137 } 138 | Popular Tags |