1 24 25 package org.ofbiz.entity.condition; 26 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import org.ofbiz.base.util.Debug; 34 import org.ofbiz.entity.GenericDelegator; 35 import org.ofbiz.entity.GenericEntity; 36 import org.ofbiz.entity.GenericModelException; 37 import org.ofbiz.entity.model.ModelEntity; 38 import org.ofbiz.entity.model.ModelField; 39 40 49 public abstract class EntityOperator extends EntityConditionBase { 50 51 public static final int ID_EQUALS = 1; 52 public static final int ID_NOT_EQUAL = 2; 53 public static final int ID_LESS_THAN = 3; 54 public static final int ID_GREATER_THAN = 4; 55 public static final int ID_LESS_THAN_EQUAL_TO = 5; 56 public static final int ID_GREATER_THAN_EQUAL_TO = 6; 57 public static final int ID_IN = 7; 58 public static final int ID_BETWEEN = 8; 59 public static final int ID_NOT = 9; 60 public static final int ID_AND = 10; 61 public static final int ID_OR = 11; 62 public static final int ID_LIKE = 12; 63 public static final int ID_NOT_IN = 13; 64 public static final int ID_NOT_LIKE = 14; 65 66 private static HashMap registry = new HashMap (); 67 68 private static void register(String name, EntityOperator operator) { 69 registry.put(name, operator); 70 } 71 72 public static EntityOperator lookup(String name) { 73 return (EntityOperator) registry.get(name); 74 } 75 76 public static EntityComparisonOperator lookupComparison(String name) { 77 EntityOperator operator = lookup(name); 78 if ( !(operator instanceof EntityComparisonOperator ) ) 79 throw new IllegalArgumentException (name + " is not a comparison operator"); 80 return (EntityComparisonOperator)operator; 81 } 82 83 public static EntityJoinOperator lookupJoin(String name) { 84 EntityOperator operator = lookup(name); 85 if ( !(operator instanceof EntityJoinOperator ) ) 86 throw new IllegalArgumentException (name + " is not a join operator"); 87 return (EntityJoinOperator)operator; 88 } 89 90 public static final EntityComparisonOperator EQUALS = new EntityComparisonOperator(ID_EQUALS, "=") { 91 public boolean compare(Object lhs, Object rhs) { return EntityComparisonOperator.compareEqual(lhs, rhs); } 92 protected void makeRHSWhereString(ModelEntity entity, List entityConditionParams, StringBuffer sb, ModelField field, Object rhs) { 93 if (rhs == null || rhs == GenericEntity.NULL_FIELD) { 94 sb.append(" IS NULL"); 96 } else { 97 super.makeRHSWhereString(entity, entityConditionParams, sb, field, rhs); 99 } 100 } 101 }; 102 static { register( "equals", EQUALS ); } 103 public static final EntityComparisonOperator NOT_EQUAL = new EntityComparisonOperator(ID_NOT_EQUAL, "<>") { 104 public boolean compare(Object lhs, Object rhs) { return EntityComparisonOperator.compareNotEqual(lhs, rhs); } 105 protected void makeRHSWhereString(ModelEntity entity, List entityConditionParams, StringBuffer sb, ModelField field, Object rhs) { 106 if (rhs == null || rhs == GenericEntity.NULL_FIELD) { 107 sb.append(" IS NOT NULL"); 108 } else { 109 super.makeRHSWhereString(entity, entityConditionParams, sb, field, rhs); 110 } 111 } 112 }; 113 static { register( "not-equal", NOT_EQUAL ); } 114 static { register( "not-equals", NOT_EQUAL ); } 115 static { register( "notEqual", NOT_EQUAL ); } 116 public static final EntityComparisonOperator LESS_THAN = new EntityComparisonOperator(ID_LESS_THAN, "<") { 117 public boolean compare(Object lhs, Object rhs) { return EntityComparisonOperator.compareLessThan(lhs, rhs); } 118 }; 119 static { register( "less", LESS_THAN ); } 120 static { register( "less-than", LESS_THAN ); } 121 static { register( "lessThan", LESS_THAN ); } 122 public static final EntityComparisonOperator GREATER_THAN = new EntityComparisonOperator(ID_GREATER_THAN, ">") { 123 public boolean compare(Object lhs, Object rhs) { return EntityComparisonOperator.compareGreaterThan(lhs, rhs); } 124 }; 125 static { register( "greater", GREATER_THAN ); } 126 static { register( "greater-than", GREATER_THAN ); } 127 static { register( "greaterThan", GREATER_THAN ); } 128 public static final EntityComparisonOperator LESS_THAN_EQUAL_TO = new EntityComparisonOperator(ID_LESS_THAN_EQUAL_TO, "<=") { 129 public boolean compare(Object lhs, Object rhs) { return EntityComparisonOperator.compareLessThanEqualTo(lhs, rhs); } 130 }; 131 static { register( "less-equals", LESS_THAN_EQUAL_TO ); } 132 static { register( "less-than-equal-to", LESS_THAN_EQUAL_TO ); } 133 static { register( "lessThanEqualTo", LESS_THAN_EQUAL_TO ); } 134 public static final EntityComparisonOperator GREATER_THAN_EQUAL_TO = new EntityComparisonOperator(ID_GREATER_THAN_EQUAL_TO, ">=") { 135 public boolean compare(Object lhs, Object rhs) { return EntityComparisonOperator.compareGreaterThanEqualTo(lhs, rhs); } 136 }; 137 static { register( "greater-equals", GREATER_THAN_EQUAL_TO ); } 138 static { register( "greater-than-equal-to", GREATER_THAN_EQUAL_TO ); } 139 static { register( "greaterThanEqualTo", GREATER_THAN_EQUAL_TO ); } 140 public static final EntityComparisonOperator IN = new EntityComparisonOperator(ID_IN, "IN") { 141 public boolean compare(Object lhs, Object rhs) { return EntityComparisonOperator.compareIn(lhs, rhs); } 142 protected void makeRHSWhereStringValue(ModelEntity entity, List entityConditionParams, StringBuffer sb, ModelField field, Object rhs) { appendRHSList(entityConditionParams, sb, field, rhs); } 143 }; 144 static { register( "in", IN ); } 145 public static final EntityComparisonOperator BETWEEN = new EntityComparisonOperator(ID_BETWEEN, "BETWEEN"); 146 static { register( "between", BETWEEN ); } 147 public static final EntityComparisonOperator NOT = new EntityComparisonOperator(ID_NOT, "NOT"); 148 static { register( "not", NOT ); } 149 public static final EntityJoinOperator AND = new EntityJoinOperator(ID_AND, "AND", false); 150 static { register( "and", AND ); } 151 public static final EntityJoinOperator OR = new EntityJoinOperator(ID_OR, "OR", true); 152 static { register( "or", OR ); } 153 public static final EntityComparisonOperator LIKE = new EntityComparisonOperator(ID_LIKE, "LIKE") { 154 public boolean compare(Object lhs, Object rhs) { return EntityComparisonOperator.compareLike(lhs, rhs); } 155 }; 156 static { register( "like", LIKE ); } 157 public static final EntityComparisonOperator NOT_LIKE = new EntityComparisonOperator(ID_NOT_LIKE, "NOT LIKE") { 158 public boolean compare(Object lhs, Object rhs) { return !EntityComparisonOperator.compareLike(lhs, rhs); } 159 }; 160 static { register( "not-like", NOT_LIKE); } 161 public static final EntityComparisonOperator NOT_IN = new EntityComparisonOperator(ID_NOT_IN, "NOT IN") { 162 public boolean compare(Object lhs, Object rhs) { return !EntityComparisonOperator.compareIn(lhs, rhs); } 163 protected void makeRHSWhereStringValue(ModelEntity entity, List entityConditionParams, StringBuffer sb, ModelField field, Object rhs) { appendRHSList(entityConditionParams, sb, field, rhs); } 164 }; 165 static { register( "not-in", NOT_IN ); } 166 167 protected int idInt; 168 protected String codeString; 169 170 public EntityOperator(int id, String code) { 171 idInt = id; 172 codeString = code; 173 } 174 175 public String getCode() { 176 if (codeString == null) { 177 return "null"; 178 } else { 179 return codeString; 180 } 181 } 182 183 public int getId() { 184 return idInt; 185 } 186 187 public String toString() { 188 return codeString; 189 } 190 191 public int hashCode() { 192 return this.codeString.hashCode(); 193 } 194 195 public boolean equals(Object obj) { 196 EntityOperator otherOper = (EntityOperator) obj; 197 return this.idInt == otherOper.idInt; 198 } 199 200 public boolean entityMatches(GenericEntity entity, Object lhs, Object rhs) { 201 return mapMatches(entity.getDelegator(), entity, lhs, rhs); 202 } 203 204 protected void appendRHSList(List entityConditionParams, StringBuffer whereStringBuffer, ModelField field, Object rhs) { 205 whereStringBuffer.append('('); 206 207 if (rhs instanceof Collection ) { 208 Iterator rhsIter = ((Collection ) rhs).iterator(); 209 210 while (rhsIter.hasNext()) { 211 Object inObj = rhsIter.next(); 212 213 addValue(whereStringBuffer, field, inObj, entityConditionParams); 214 if (rhsIter.hasNext()) { 215 whereStringBuffer.append(", "); 216 } 217 } 218 } else { 219 addValue(whereStringBuffer, field, rhs, entityConditionParams); 220 } 221 whereStringBuffer.append(')'); 222 } 223 224 public Object eval(GenericDelegator delegator, Map map, Object lhs, Object rhs) { 225 return castBoolean(mapMatches(delegator, map, lhs, rhs)); 226 } 227 228 public abstract boolean mapMatches(GenericDelegator delegator, Map map, Object lhs, Object rhs); 229 public abstract void validateSql(ModelEntity entity, Object lhs, Object rhs) throws GenericModelException; 230 public void addSqlValue(StringBuffer sql, ModelEntity entity, List entityConditionParams, Object lhs, Object rhs) { 231 addSqlValue(sql, entity, entityConditionParams, true, lhs, rhs); 232 } 233 234 public abstract void addSqlValue(StringBuffer sql, ModelEntity entity, List entityConditionParams, boolean compat, Object rhs, Object lhs); 235 public abstract EntityCondition freeze(Object lhs, Object rhs); 236 public abstract void visit(EntityConditionVisitor visitor, Object lhs, Object rhs); 237 238 public static final Object WILDCARD = new Object () { 239 public String toString() { 240 return "(WILDCARD)"; 241 } 242 }; 243 } 244 | Popular Tags |