1 24 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.apache.oro.text.perl.Perl5Util; 32 import org.apache.oro.text.regex.MalformedPatternException; 33 import org.apache.oro.text.regex.Pattern; 34 import org.apache.oro.text.regex.PatternCompiler; 35 import org.apache.oro.text.regex.PatternMatcher; 36 import org.apache.oro.text.regex.Perl5Compiler; 37 import org.apache.oro.text.regex.Perl5Matcher; 38 import org.ofbiz.base.util.Debug; 39 import org.ofbiz.entity.GenericDelegator; 40 import org.ofbiz.entity.GenericModelException; 41 import org.ofbiz.entity.model.ModelEntity; 42 import org.ofbiz.entity.model.ModelField; 43 44 52 public class EntityComparisonOperator extends EntityOperator { 53 54 public static final String module = EntityComparisonOperator.class.getName(); 55 56 protected static PatternMatcher matcher = new Perl5Matcher(); 57 protected static Perl5Util perl5Util = new Perl5Util(); 58 protected static PatternCompiler compiler = new Perl5Compiler(); 59 60 public static Pattern makeOroPattern(String sqlLike) { 61 try { 62 sqlLike = perl5Util.substitute("s/([$^.+*?])/\\\\$1/g", sqlLike); 63 sqlLike = perl5Util.substitute("s/%/.*/g", sqlLike); 64 sqlLike = perl5Util.substitute("s/_/./g", sqlLike); 65 } catch (Throwable t) { 66 String errMsg = "Error in ORO pattern substitution for SQL like clause [" + sqlLike + "]: " + t.toString(); 67 Debug.logError(t, errMsg, module); 68 throw new IllegalArgumentException (errMsg); 69 } 70 try { 71 return compiler.compile(sqlLike); 72 } catch (MalformedPatternException e) { 73 e.printStackTrace(); 74 } 75 return null; 76 } 77 78 public void validateSql(ModelEntity entity, Object lhs, Object rhs) throws GenericModelException { 79 if (lhs instanceof EntityConditionValue) { 80 EntityConditionValue ecv = (EntityConditionValue) lhs; 81 ecv.validateSql(entity); 82 } 83 if (rhs instanceof EntityConditionValue) { 84 EntityConditionValue ecv = (EntityConditionValue) rhs; 85 ecv.validateSql(entity); 86 } 87 } 88 89 public void visit(EntityConditionVisitor visitor, Object lhs, Object rhs) { 90 visitor.accept(lhs); 91 visitor.accept(rhs); 92 } 93 94 public void addSqlValue(StringBuffer sql, ModelEntity entity, List entityConditionParams, boolean compat, Object lhs, Object rhs) { 95 ModelField field; 97 if (lhs instanceof EntityConditionValue) { 98 EntityConditionValue ecv = (EntityConditionValue) lhs; 99 ecv.addSqlValue(sql, entity, entityConditionParams, false, null); 100 field = ecv.getModelField(entity); 101 } else if (compat && lhs instanceof String ) { 102 field = getField(entity, (String ) lhs); 103 if (field == null) { 104 sql.append(lhs); 105 } else { 106 sql.append(field.getColName()); 107 } 108 } else { 109 addValue(sql, null, lhs, entityConditionParams); 110 field = null; 111 } 112 113 makeRHSWhereString(entity, entityConditionParams, sql, field, rhs); 114 } 115 116 protected void makeRHSWhereString(ModelEntity entity, List entityConditionParams, StringBuffer sql, ModelField field, Object rhs) { 117 sql.append(' ').append(getCode()).append(' '); 118 makeRHSWhereStringValue(entity, entityConditionParams, sql, field, rhs); 119 } 120 121 protected void makeRHSWhereStringValue(ModelEntity entity, List entityConditionParams, StringBuffer sql, ModelField field, Object rhs) { 122 if (rhs instanceof EntityConditionValue) { 123 EntityConditionValue ecv = (EntityConditionValue) rhs; 124 ecv.addSqlValue(sql, entity, entityConditionParams, false, null); 125 } else { 126 addValue(sql, field, rhs, entityConditionParams); 127 } 128 } 129 130 public boolean compare(Object lhs, Object rhs) { 131 throw new UnsupportedOperationException (codeString); 132 } 133 134 public Object eval(GenericDelegator delegator, Map map, Object lhs, Object rhs) { 135 return mapMatches(delegator, map, lhs, rhs) ? Boolean.TRUE : Boolean.FALSE; 136 } 137 138 public boolean mapMatches(GenericDelegator delegator, Map map, Object lhs, Object rhs) { 139 Object leftValue; 140 if (lhs instanceof EntityConditionValue) { 141 EntityConditionValue ecv = (EntityConditionValue) lhs; 142 leftValue = ecv.getValue(delegator, map); 143 } else if (lhs instanceof String ) { 144 leftValue = map.get(lhs); 145 } else { 146 leftValue = lhs; 147 } 148 Object rightValue; 149 if (rhs instanceof EntityConditionValue) { 150 EntityConditionValue ecv = (EntityConditionValue) rhs; 151 rightValue = ecv.getValue(delegator, map); 152 } else { 153 rightValue = rhs; 154 } 155 156 if (leftValue == WILDCARD || rightValue == WILDCARD) return true; 157 return compare(leftValue, rightValue); 158 } 159 160 public EntityCondition freeze(Object lhs, Object rhs) { 161 return new EntityExpr(freeze(lhs), this, freeze(rhs)); 162 } 163 164 protected Object freeze(Object item) { 165 if (item instanceof EntityConditionValue) { 166 EntityConditionValue ecv = (EntityConditionValue) item; 167 return ecv.freeze(); 168 } else { 169 return item; 170 } 171 } 172 173 public EntityComparisonOperator(int id, String code) { 174 super(id, code); 175 } 176 177 public static final boolean compareEqual(Object lhs, Object rhs) { 178 if (lhs == null) { 179 if (rhs != null) { 180 return false; 181 } 182 } else if (!lhs.equals(rhs)) { 183 return false; 184 } 185 return true; 186 } 187 188 public static final boolean compareNotEqual(Object lhs, Object rhs) { 189 if (lhs == null) { 190 if (rhs == null) { 191 return false; 192 } 193 } else if (lhs.equals(rhs)) { 194 return false; 195 } 196 return true; 197 } 198 199 public static final boolean compareGreaterThan(Object lhs, Object rhs) { 200 if (lhs == null) { 201 if (rhs != null) { 202 return false; 203 } 204 } else if (((Comparable ) lhs).compareTo(rhs) <= 0) { 205 return false; 206 } 207 return true; 208 } 209 210 public static final boolean compareGreaterThanEqualTo(Object lhs, Object rhs) { 211 if (lhs == null) { 212 if (rhs != null) { 213 return false; 214 } 215 } else if (((Comparable ) lhs).compareTo(rhs) < 0) { 216 return false; 217 } 218 return true; 219 } 220 221 public static final boolean compareLessThan(Object lhs, Object rhs) { 222 if (lhs == null) { 223 if (rhs != null) { 224 return false; 225 } 226 } else if (((Comparable ) lhs).compareTo(rhs) >= 0) { 227 return false; 228 } 229 return true; 230 } 231 232 public static final boolean compareLessThanEqualTo(Object lhs, Object rhs) { 233 if (lhs == null) { 234 if (rhs != null) { 235 return false; 236 } 237 } else if (((Comparable ) lhs).compareTo(rhs) > 0) { 238 return false; 239 } 240 return true; 241 } 242 243 public static final boolean compareIn(Object lhs, Object rhs) { 244 if (lhs == null) { 245 if (rhs != null) { 246 return false; 247 } else { 248 return true; 249 } 250 } else if (rhs instanceof Collection ) { 251 if (((Collection ) rhs).contains(lhs)) { 252 return true; 253 } else { 254 return false; 255 } 256 } else if (lhs.equals(rhs)) { 257 return true; 258 } else { 259 return false; 260 } 261 } 262 263 public static final boolean compareLike(Object lhs, Object rhs) { 264 if (lhs == null) { 265 if (rhs != null) { 266 return false; 267 } 268 } else if (lhs instanceof String && rhs instanceof String ) { 269 return matcher.matches((String ) lhs, makeOroPattern((String ) rhs)); 271 } 272 return true; 273 } 274 } 275 | Popular Tags |