1 25 package org.ofbiz.entity.condition; 26 27 import java.util.Collection ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.ofbiz.base.util.Debug; 32 import org.ofbiz.entity.EntityCryptoException; 33 import org.ofbiz.entity.GenericDelegator; 34 import org.ofbiz.entity.GenericEntity; 35 import org.ofbiz.entity.GenericModelException; 36 import org.ofbiz.entity.model.ModelEntity; 37 import org.ofbiz.entity.model.ModelField; 38 39 46 public class EntityExpr extends EntityCondition { 47 public static final String module = EntityExpr.class.getName(); 48 49 private Object lhs; 50 private EntityOperator operator; 51 private Object rhs; 52 53 protected EntityExpr() {} 54 55 public EntityExpr(Object lhs, EntityComparisonOperator operator, Object rhs) { 56 if (lhs == null) { 57 throw new IllegalArgumentException ("The field value cannot be null"); 58 } 59 if (lhs instanceof String ) { 60 Debug.logError(new Exception (), "EntityExpr called with lhs as a String; consider recompiling", module); 61 } 62 if (operator == null) { 63 throw new IllegalArgumentException ("The operator argument cannot be null"); 64 } 65 66 if (rhs == null || rhs == GenericEntity.NULL_FIELD) { 67 if (!EntityOperator.NOT_EQUAL.equals(operator) && !EntityOperator.EQUALS.equals(operator)) { 68 throw new IllegalArgumentException ("Operator must be EQUALS or NOT_EQUAL when right/rhs argument is NULL "); 69 } 70 } 71 72 if (EntityOperator.BETWEEN.equals(operator)) { 73 if (!(rhs instanceof Collection ) || (((Collection ) rhs).size() != 2)) { 74 throw new IllegalArgumentException ("BETWEEN Operator requires a Collection with 2 elements for the right/rhs argument"); 75 } 76 } 77 78 this.lhs = lhs; 79 this.operator = operator; 80 this.rhs = rhs; 81 82 } 84 85 public EntityExpr(String lhs, EntityComparisonOperator operator, Object rhs) { 86 this(new EntityFieldValue(lhs), operator, rhs); 87 } 89 90 public EntityExpr(String lhs, boolean leftUpper, EntityComparisonOperator operator, Object rhs, boolean rightUpper) { 91 if (lhs == null) { 92 throw new IllegalArgumentException ("The field value cannot be null"); 93 } 94 if (operator == null) { 95 throw new IllegalArgumentException ("The operator argument cannot be null"); 96 } 97 this.lhs = new EntityFieldValue(lhs); 98 if (leftUpper) this.lhs = new EntityFunction.UPPER(this.lhs); 99 this.operator = operator; 100 if (rhs instanceof EntityConditionValue) { 101 if (rightUpper) rhs = new EntityFunction.UPPER((EntityConditionValue) rhs); 102 this.rhs = rhs; 103 } else { 104 if (rightUpper) rhs = new EntityFunction.UPPER(rhs); 105 this.rhs = rhs; 106 } 107 } 108 109 public EntityExpr(EntityCondition lhs, EntityJoinOperator operator, EntityCondition rhs) { 110 if (lhs == null) { 111 throw new IllegalArgumentException ("The left EntityCondition argument cannot be null"); 112 } 113 if (rhs == null) { 114 throw new IllegalArgumentException ("The right EntityCondition argument cannot be null"); 115 } 116 if (operator == null) { 117 throw new IllegalArgumentException ("The operator argument cannot be null"); 118 } 119 120 this.lhs = lhs; 121 this.operator = operator; 122 this.rhs = rhs; 123 } 124 125 126 public void setLUpper(boolean upper) { 127 } 128 129 130 public boolean isLUpper() { 131 return lhs instanceof EntityFunction.UPPER; 132 } 133 134 135 public boolean isRUpper() { 136 return rhs instanceof EntityFunction.UPPER; 137 } 138 139 140 public void setRUpper(boolean upper) { 141 } 142 143 public Object getLhs() { 144 return lhs; 145 } 146 147 public EntityOperator getOperator() { 148 return operator; 149 } 150 151 public Object getRhs() { 152 return rhs; 153 } 154 155 public String makeWhereString(ModelEntity modelEntity, List entityConditionParams) { 156 StringBuffer sql = new StringBuffer (); 158 operator.addSqlValue(sql, modelEntity, entityConditionParams, true, lhs, rhs); 159 return sql.toString(); 160 } 161 162 public boolean mapMatches(GenericDelegator delegator, Map map) { 163 return operator.mapMatches(delegator, map, lhs, rhs); 164 } 165 166 public void checkCondition(ModelEntity modelEntity) throws GenericModelException { 167 if (lhs instanceof EntityCondition) { 169 ((EntityCondition) lhs).checkCondition(modelEntity); 170 ((EntityCondition) rhs).checkCondition(modelEntity); 171 } 172 } 173 174 protected void addValue(StringBuffer buffer, ModelField field, Object value, List params) { 175 if (rhs instanceof EntityFunction.UPPER) { 176 if (value instanceof String ) { 177 value = ((String ) value).toUpperCase(); 178 } 179 } 180 super.addValue(buffer, field, value, params); 181 } 182 183 public EntityCondition freeze() { 184 return operator.freeze(lhs, rhs); 185 } 186 187 public void encryptConditionFields(ModelEntity modelEntity, GenericDelegator delegator) { 188 if (this.lhs instanceof String ) { 189 ModelField modelField = modelEntity.getField((String ) this.lhs); 190 if (modelField != null && modelField.getEncrypt()) { 191 if (!(rhs instanceof EntityConditionValue)) { 192 try { 193 this.rhs = delegator.encryptFieldValue(modelEntity.getEntityName(), this.rhs); 194 } catch (EntityCryptoException e) { 195 Debug.logWarning(e, "Error encrypting field [" + modelEntity.getEntityName() + "." + modelField.getName() + "] with value: " + this.rhs, module); 196 } 197 } 198 } 199 } 200 } 201 202 public void visit(EntityConditionVisitor visitor) { 203 visitor.acceptEntityOperator(operator, lhs, rhs); 204 } 205 206 public void accept(EntityConditionVisitor visitor) { 207 visitor.acceptEntityExpr(this); 208 } 209 210 public boolean equals(Object obj) { 211 if (!(obj instanceof EntityExpr)) return false; 212 EntityExpr other = (EntityExpr) obj; 213 boolean isEqual = equals(lhs, other.lhs) && 214 equals(operator, other.operator) && 215 equals(rhs, other.rhs); 216 return isEqual; 222 } 223 224 public int hashCode() { 225 return hashCode(lhs) + 226 hashCode(operator) + 227 hashCode(rhs); 228 } 229 } 230 | Popular Tags |