1 25 package org.ofbiz.entityext.eca; 26 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 31 import org.ofbiz.base.util.Debug; 32 import org.ofbiz.base.util.ObjectType; 33 import org.ofbiz.entity.GenericEntity; 34 import org.ofbiz.entity.GenericEntityException; 35 import org.ofbiz.service.DispatchContext; 36 import org.w3c.dom.Element ; 37 38 46 public class EntityEcaCondition implements java.io.Serializable { 47 48 public static final String module = EntityEcaCondition.class.getName(); 49 50 protected String lhsValueName, rhsValueName; 51 protected String operator; 52 protected String compareType; 53 protected String format; 54 protected boolean constant = false; 55 56 protected EntityEcaCondition() {} 57 58 public EntityEcaCondition(Element condition, boolean constant) { 59 this.lhsValueName = condition.getAttribute("field-name"); 60 61 this.constant = constant; 62 if (constant) { 63 this.rhsValueName = condition.getAttribute("value"); 64 } else { 65 this.rhsValueName = condition.getAttribute("to-field-name"); 66 } 67 68 this.operator = condition.getAttribute("operator"); 69 this.compareType = condition.getAttribute("type"); 70 this.format = condition.getAttribute("format"); 71 72 if (lhsValueName == null) 73 lhsValueName = ""; 74 if (rhsValueName == null) 75 rhsValueName = ""; 76 } 77 78 public boolean eval(DispatchContext dctx, GenericEntity value) throws GenericEntityException { 79 if (dctx == null || value == null || dctx.getClassLoader() == null) { 80 throw new GenericEntityException("Cannot have null Value or DispatchContext!"); 81 } 82 83 if (Debug.verboseOn()) Debug.logVerbose(this.toString(), module); 84 85 Object lhsValue = value.get(lhsValueName); 86 87 Object rhsValue; 88 if (constant) { 89 rhsValue = rhsValueName; 90 } else { 91 rhsValue = value.get(rhsValueName); 92 } 93 94 if (Debug.verboseOn()) Debug.logVerbose("Comparing : " + lhsValue + " " + operator + " " + rhsValue, module); 95 96 List messages = new LinkedList (); 98 Boolean cond = ObjectType.doRealCompare(lhsValue, rhsValue, operator, compareType, format, messages, null, dctx.getClassLoader()); 99 100 if (messages.size() > 0) { 102 Iterator m = messages.iterator(); 103 while (m.hasNext()) { 104 Debug.logWarning((String ) m.next(), module); 105 } 106 } 107 if (cond != null) { 108 return cond.booleanValue(); 109 } else { 110 return false; 111 } 112 } 113 114 public String toString() { 115 StringBuffer buf = new StringBuffer (); 116 buf.append("[" + lhsValueName + "]"); 117 buf.append("[" + operator + "]"); 118 buf.append("[" + rhsValueName + "]"); 119 buf.append("[" + constant + "]"); 120 buf.append("[" + compareType + "]"); 121 buf.append("[" + format + "]"); 122 return buf.toString(); 123 } 124 } 125 | Popular Tags |