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 32 import org.ofbiz.service.DispatchContext; 33 import org.ofbiz.service.GenericServiceException; 34 import org.ofbiz.service.LocalDispatcher; 35 import org.ofbiz.service.ServiceUtil; 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.base.util.ObjectType; 38 import org.ofbiz.base.util.UtilMisc; 39 40 import org.w3c.dom.Element ; 41 42 49 public class ServiceEcaCondition implements java.io.Serializable { 50 51 public static final String module = ServiceEcaCondition.class.getName(); 52 53 protected String conditionService = null; 54 protected String lhsValueName = null; 55 protected String rhsValueName = null; 56 protected String lhsMapName = null; 57 protected String rhsMapName = null; 58 protected String operator = null; 59 protected String compareType = null; 60 protected String format = null; 61 protected boolean isConstant = false; 62 protected boolean isService = false; 63 64 protected ServiceEcaCondition() {} 65 66 public ServiceEcaCondition(Element condition, boolean isConstant, boolean isService) { 67 if (isService) { 68 this.isService = isService; 69 this.conditionService = condition.getAttribute("service-name"); 70 } else { 71 this.lhsValueName = condition.getAttribute("field-name"); 72 this.lhsMapName = condition.getAttribute("map-name"); 73 74 this.isConstant = isConstant; 75 if (isConstant) { 76 this.rhsValueName = condition.getAttribute("value"); 77 this.rhsMapName = null; 78 } else { 79 this.rhsValueName = condition.getAttribute("to-field-name"); 80 this.rhsMapName = condition.getAttribute("to-map-name"); 81 } 82 83 this.operator = condition.getAttribute("operator"); 84 this.compareType = condition.getAttribute("type"); 85 this.format = condition.getAttribute("format"); 86 87 if (lhsValueName == null) { 88 lhsValueName = ""; 89 } 90 if (rhsValueName == null) { 91 rhsValueName = ""; 92 } 93 } 94 } 95 96 public boolean eval(String serviceName, DispatchContext dctx, Map context) throws GenericServiceException { 97 if (serviceName == null || dctx == null || context == null || dctx.getClassLoader() == null) { 98 throw new GenericServiceException("Cannot have null Service, Context or DispatchContext!"); 99 } 100 101 if (Debug.verboseOn()) Debug.logVerbose(this.toString() + ", In the context: " + context, module); 102 103 if (isService) { 105 LocalDispatcher dispatcher = dctx.getDispatcher(); 106 Map conditionServiceResult = dispatcher.runSync(conditionService, 107 UtilMisc.toMap("serviceContext", context, "serviceName", serviceName, 108 "userLogin", context.get("userLogin"))); 109 110 Boolean conditionReply = Boolean.FALSE; 111 if (ServiceUtil.isError(conditionServiceResult)) { 112 Debug.logError("Error in condition-service : " + 113 ServiceUtil.getErrorMessage(conditionServiceResult), module); 114 } else { 115 conditionReply = (Boolean ) conditionServiceResult.get("conditionReply"); 116 } 117 return conditionReply.booleanValue(); 118 } 119 120 Object lhsValue = null; 121 Object rhsValue = null; 122 if (lhsMapName != null && lhsMapName.length() > 0) { 123 try { 124 if (context.containsKey(lhsMapName)) { 125 Map envMap = (Map ) context.get(lhsMapName); 126 lhsValue = envMap.get(lhsValueName); 127 } else { 128 Debug.logWarning("From Map (" + lhsMapName + ") not found in context, defaulting to null.", module); 129 } 130 } catch (ClassCastException e) { 131 throw new GenericServiceException("From Map field [" + lhsMapName + "] is not a Map.", e); 132 } 133 } else { 134 if (context.containsKey(lhsValueName)) { 135 lhsValue = context.get(lhsValueName); 136 } else { 137 Debug.logWarning("From Field (" + lhsValueName + ") is not found in context for " + serviceName + ", defaulting to null.", module); 138 } 139 } 140 141 if (isConstant) { 142 rhsValue = rhsValueName; 143 } else if (rhsMapName != null && rhsMapName.length() > 0) { 144 try { 145 if (context.containsKey(rhsMapName)) { 146 Map envMap = (Map ) context.get(rhsMapName); 147 rhsValue = envMap.get(rhsValueName); 148 } else { 149 Debug.logWarning("To Map (" + rhsMapName + ") not found in context for " + serviceName + ", defaulting to null.", module); 150 } 151 } catch (ClassCastException e) { 152 throw new GenericServiceException("To Map field [" + rhsMapName + "] is not a Map.", e); 153 } 154 } else { 155 if (context.containsKey(rhsValueName)) { 156 rhsValue = context.get(rhsValueName); 157 } else { 158 Debug.logInfo("To Field (" + rhsValueName + ") is not found in context for " + serviceName + ", defaulting to null.", module); 159 } 160 } 161 162 if (Debug.verboseOn()) Debug.logVerbose("Comparing : " + lhsValue + " " + operator + " " + rhsValue, module); 163 164 List messages = new LinkedList (); 166 Boolean cond = ObjectType.doRealCompare(lhsValue, rhsValue, operator, compareType, format, messages, null, dctx.getClassLoader()); 167 168 if (messages.size() > 0) { 170 Iterator m = messages.iterator(); 171 while (m.hasNext()) { 172 Debug.logWarning((String ) m.next(), module); 173 } 174 } 175 if (cond != null) { 176 return cond.booleanValue(); 177 } else { 178 Debug.logWarning("doRealCompare returned null, returning false", module); 179 return false; 180 } 181 } 182 183 public String toString() { 184 StringBuffer buf = new StringBuffer (); 185 186 buf.append("[" + conditionService + "]"); 187 buf.append("[" + lhsMapName + "]"); 188 buf.append("[" + lhsValueName + "]"); 189 buf.append("[" + operator + "]"); 190 buf.append("[" + rhsMapName + "]"); 191 buf.append("[" + rhsValueName + "]"); 192 buf.append("[" + isConstant + "]"); 193 buf.append("[" + compareType + "]"); 194 buf.append("[" + format + "]"); 195 return buf.toString(); 196 } 197 } 198 | Popular Tags |