1 25 package org.ofbiz.service.eca; 26 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import org.ofbiz.service.DispatchContext; 34 import org.ofbiz.service.GenericServiceException; 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.UtilXml; 37 import org.w3c.dom.Element ; 38 39 46 public class ServiceEcaRule implements java.io.Serializable { 47 48 public static final String module = ServiceEcaRule.class.getName(); 49 50 protected String serviceName = null; 51 protected String eventName = null; 52 protected boolean runOnFailure = false; 53 protected boolean runOnError = false; 54 protected List conditions = new LinkedList (); 55 protected List actions = new LinkedList (); 56 protected boolean enabled = true; 57 58 protected ServiceEcaRule() {} 59 60 public ServiceEcaRule(Element eca) { 61 this.serviceName = eca.getAttribute("service"); 62 this.eventName = eca.getAttribute("event"); 63 this.runOnFailure = "true".equals(eca.getAttribute("run-on-failure")); 64 this.runOnError = "true".equals(eca.getAttribute("run-on-error")); 65 66 List condList = UtilXml.childElementList(eca, "condition"); 67 Iterator ci = condList.iterator(); 68 69 while (ci.hasNext()) { 70 conditions.add(new ServiceEcaCondition((Element ) ci.next(), true, false)); 71 } 72 73 List condFList = UtilXml.childElementList(eca, "condition-field"); 74 Iterator cfi = condFList.iterator(); 75 76 while (cfi.hasNext()) { 77 conditions.add(new ServiceEcaCondition((Element ) cfi.next(), false, false)); 78 } 79 80 List condSList = UtilXml.childElementList(eca, "condition-service"); 81 Iterator sfi = condSList.iterator(); 82 83 while (sfi.hasNext()) { 84 conditions.add(new ServiceEcaCondition((Element ) sfi.next(), false, true)); 85 } 86 87 if (Debug.verboseOn()) Debug.logVerbose("Conditions: " + conditions, module); 88 89 List actList = UtilXml.childElementList(eca, "action"); 90 Iterator ai = actList.iterator(); 91 92 while (ai.hasNext()) { 93 Element actionElement = (Element ) ai.next(); 94 actions.add(new ServiceEcaAction(actionElement, eventName)); 95 } 96 97 if (Debug.verboseOn()) Debug.logVerbose("Actions: " + actions, module); 98 } 99 100 public void eval(String serviceName, DispatchContext dctx, Map context, Map result, boolean isError, boolean isFailure, Set actionsRun) throws GenericServiceException { 101 if (!enabled) { 102 Debug.logInfo("Service ECA [" + this.serviceName + "] on [" + this.eventName + "] is disabled; not running.", module); 103 return; 104 } 105 if (isFailure && !this.runOnFailure) { 106 return; 107 } 108 if (isError && !this.runOnError) { 109 return; 110 } 111 112 boolean allCondTrue = true; 113 Iterator c = conditions.iterator(); 114 115 while (c.hasNext()) { 116 ServiceEcaCondition ec = (ServiceEcaCondition) c.next(); 117 if (!ec.eval(serviceName, dctx, context)) { 118 if (Debug.infoOn()) Debug.logInfo("Got false for condition: " + ec, module); 119 allCondTrue = false; 120 break; 121 } else { 122 if (Debug.verboseOn()) Debug.logVerbose("Got true for condition: " + ec, module); 123 } 124 } 125 126 if (allCondTrue) { 127 Iterator a = actions.iterator(); 128 boolean allOkay = true; 129 while (a.hasNext() && allOkay) { 130 ServiceEcaAction ea = (ServiceEcaAction) a.next(); 131 if (!actionsRun.contains(ea.serviceName)) { 134 if (Debug.infoOn()) Debug.logInfo("Running Service ECA Service: " + ea.serviceName + ", triggered by rule on Service: " + serviceName, module); 135 if (ea.runAction(serviceName, dctx, context, result)) { 136 actionsRun.add(ea.serviceName); 137 } else { 138 allOkay = false; 139 } 140 } 141 } 142 } 143 } 144 145 public void setEnabled(boolean enabled) { 146 this.enabled = enabled; 147 } 148 149 public boolean isEnabled() { 150 return this.enabled; 151 } 152 } 153 | Popular Tags |