1 24 package org.ofbiz.widget.tree; 25 26 import java.lang.reflect.Method ; 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.base.util.Debug; 33 import org.ofbiz.base.util.GeneralException; 34 import org.ofbiz.base.util.ObjectType; 35 import org.ofbiz.base.util.UtilXml; 36 import org.ofbiz.base.util.collections.FlexibleMapAccessor; 37 import org.ofbiz.base.util.string.FlexibleStringExpander; 38 import org.ofbiz.entity.GenericValue; 39 import org.ofbiz.entityext.permission.EntityPermissionChecker; 40 import org.ofbiz.minilang.operation.BaseCompare; 41 import org.ofbiz.security.Security; 42 43 import org.apache.oro.text.regex.MalformedPatternException; 44 import org.apache.oro.text.regex.Pattern; 45 import org.apache.oro.text.regex.PatternCompiler; 46 import org.apache.oro.text.regex.PatternMatcher; 47 import org.apache.oro.text.regex.Perl5Compiler; 48 import org.apache.oro.text.regex.Perl5Matcher; 49 import org.w3c.dom.Element ; 50 51 58 public class ModelTreeCondition { 59 public static final String module = ModelTreeCondition.class.getName(); 60 61 protected ModelTree modelTree; 62 protected TreeCondition rootCondition; 63 64 public ModelTreeCondition(ModelTree modelTree, Element conditionElement) { 65 this.modelTree = modelTree; 66 Element firstChildElement = UtilXml.firstChildElement(conditionElement); 67 this.rootCondition = readCondition(modelTree, firstChildElement); 68 } 69 70 public boolean eval(Map context) { 71 if (rootCondition == null) { 72 return true; 73 } 74 return rootCondition.eval(context); 75 } 76 77 public static abstract class TreeCondition { 78 protected ModelTree modelTree; 79 80 public TreeCondition(ModelTree modelTree, Element conditionElement) { 81 this.modelTree = modelTree; 82 } 83 84 public abstract boolean eval(Map context); 85 } 86 87 public static List readSubConditions(ModelTree modelTree, Element conditionElement) { 88 List condList = new LinkedList (); 89 List subElementList = UtilXml.childElementList(conditionElement); 90 Iterator subElementIter = subElementList.iterator(); 91 while (subElementIter.hasNext()) { 92 Element subElement = (Element ) subElementIter.next(); 93 condList.add(readCondition(modelTree, subElement)); 94 } 95 return condList; 96 } 97 98 public static TreeCondition readCondition(ModelTree modelTree, Element conditionElement) { 99 if (conditionElement == null) { 100 return null; 101 } 102 if ("and".equals(conditionElement.getNodeName())) { 103 return new And(modelTree, conditionElement); 104 } else if ("xor".equals(conditionElement.getNodeName())) { 105 return new Xor(modelTree, conditionElement); 106 } else if ("or".equals(conditionElement.getNodeName())) { 107 return new Or(modelTree, conditionElement); 108 } else if ("not".equals(conditionElement.getNodeName())) { 109 return new Not(modelTree, conditionElement); 110 } else if ("if-has-permission".equals(conditionElement.getNodeName())) { 111 return new IfHasPermission(modelTree, conditionElement); 112 } else if ("if-validate-method".equals(conditionElement.getNodeName())) { 113 return new IfValidateMethod(modelTree, conditionElement); 114 } else if ("if-compare".equals(conditionElement.getNodeName())) { 115 return new IfCompare(modelTree, conditionElement); 116 } else if ("if-compare-field".equals(conditionElement.getNodeName())) { 117 return new IfCompareField(modelTree, conditionElement); 118 } else if ("if-regexp".equals(conditionElement.getNodeName())) { 119 return new IfRegexp(modelTree, conditionElement); 120 } else if ("if-empty".equals(conditionElement.getNodeName())) { 121 return new IfEmpty(modelTree, conditionElement); 122 } else if ("if-entity-permission".equals(conditionElement.getNodeName())) { 123 return new IfEntityPermission(modelTree, conditionElement); 124 } else { 125 throw new IllegalArgumentException ("Condition element not supported with name: " + conditionElement.getNodeName()); 126 } 127 } 128 129 public static class And extends TreeCondition { 130 protected List subConditions; 131 132 public And(ModelTree modelTree, Element condElement) { 133 super (modelTree, condElement); 134 this.subConditions = readSubConditions(modelTree, condElement); 135 } 136 137 public boolean eval(Map context) { 138 Iterator subConditionIter = this.subConditions.iterator(); 140 while (subConditionIter.hasNext()) { 141 TreeCondition subCondition = (TreeCondition) subConditionIter.next(); 142 if (!subCondition.eval(context)) { 143 return false; 144 } 145 } 146 return true; 147 } 148 } 149 150 public static class Xor extends TreeCondition { 151 protected List subConditions; 152 153 public Xor(ModelTree modelTree, Element condElement) { 154 super (modelTree, condElement); 155 this.subConditions = readSubConditions(modelTree, condElement); 156 } 157 158 public boolean eval(Map context) { 159 boolean foundOneTrue = false; 161 Iterator subConditionIter = this.subConditions.iterator(); 162 while (subConditionIter.hasNext()) { 163 TreeCondition subCondition = (TreeCondition) subConditionIter.next(); 164 if (subCondition.eval(context)) { 165 if (foundOneTrue) { 166 return false; 168 } else { 169 foundOneTrue = true; 170 } 171 } 172 } 173 return foundOneTrue; 174 } 175 } 176 177 public static class Or extends TreeCondition { 178 protected List subConditions; 179 180 public Or(ModelTree modelTree, Element condElement) { 181 super (modelTree, condElement); 182 this.subConditions = readSubConditions(modelTree, condElement); 183 } 184 185 public boolean eval(Map context) { 186 Iterator subConditionIter = this.subConditions.iterator(); 188 while (subConditionIter.hasNext()) { 189 TreeCondition subCondition = (TreeCondition) subConditionIter.next(); 190 if (subCondition.eval(context)) { 191 return true; 192 } 193 } 194 return false; 195 } 196 } 197 198 public static class Not extends TreeCondition { 199 protected TreeCondition subCondition; 200 201 public Not(ModelTree modelTree, Element condElement) { 202 super (modelTree, condElement); 203 Element firstChildElement = UtilXml.firstChildElement(condElement); 204 this.subCondition = readCondition(modelTree, firstChildElement); 205 } 206 207 public boolean eval(Map context) { 208 return !this.subCondition.eval(context); 209 } 210 } 211 212 public static class IfHasPermission extends TreeCondition { 213 protected FlexibleStringExpander permissionExdr; 214 protected FlexibleStringExpander actionExdr; 215 216 public IfHasPermission(ModelTree modelTree, Element condElement) { 217 super (modelTree, condElement); 218 this.permissionExdr = new FlexibleStringExpander(condElement.getAttribute("permission")); 219 this.actionExdr = new FlexibleStringExpander(condElement.getAttribute("action")); 220 } 221 222 public boolean eval(Map context) { 223 GenericValue userLogin = (GenericValue) context.get("userLogin"); 225 if (userLogin != null) { 226 String permission = permissionExdr.expandString(context); 227 String action = actionExdr.expandString(context); 228 229 Security security = (Security) context.get("security"); 230 if (action != null && action.length() > 0) { 231 if (security.hasEntityPermission(permission, action, userLogin)) { 233 return true; 234 } 235 } else { 236 if (security.hasPermission(permission, userLogin)) { 238 return true; 239 } 240 } 241 } 242 return false; 243 } 244 } 245 246 public static class IfValidateMethod extends TreeCondition { 247 protected FlexibleMapAccessor fieldAcsr; 248 protected FlexibleStringExpander methodExdr; 249 protected FlexibleStringExpander classExdr; 250 251 public IfValidateMethod(ModelTree modelTree, Element condElement) { 252 super (modelTree, condElement); 253 this.fieldAcsr = new FlexibleMapAccessor(condElement.getAttribute("field-name")); 254 this.methodExdr = new FlexibleStringExpander(condElement.getAttribute("method")); 255 this.classExdr = new FlexibleStringExpander(condElement.getAttribute("class")); 256 } 257 258 public boolean eval(Map context) { 259 String methodName = this.methodExdr.expandString(context); 260 String className = this.classExdr.expandString(context); 261 262 Object fieldVal = this.fieldAcsr.get(context); 263 String fieldString = null; 264 if (fieldVal != null) { 265 try { 266 fieldString = (String ) ObjectType.simpleTypeConvert(fieldVal, "String", null, null); 267 } catch (GeneralException e) { 268 Debug.logError(e, "Could not convert object to String, using empty String", module); 269 } 270 } 271 272 if (fieldString == null) fieldString = ""; 274 275 Class [] paramTypes = new Class [] {String .class}; 276 Object [] params = new Object [] {fieldString}; 277 278 Class valClass; 279 try { 280 valClass = ObjectType.loadClass(className); 281 } catch (ClassNotFoundException cnfe) { 282 Debug.logError("Could not find validation class: " + className, module); 283 return false; 284 } 285 286 Method valMethod; 287 try { 288 valMethod = valClass.getMethod(methodName, paramTypes); 289 } catch (NoSuchMethodException cnfe) { 290 Debug.logError("Could not find validation method: " + methodName + " of class " + className, module); 291 return false; 292 } 293 294 Boolean resultBool = Boolean.FALSE; 295 try { 296 resultBool = (Boolean ) valMethod.invoke(null, params); 297 } catch (Exception e) { 298 Debug.logError(e, "Error in IfValidationMethod " + methodName + " of class " + className + ", defaulting to false ", module); 299 } 300 301 return resultBool.booleanValue(); 302 } 303 } 304 305 public static class IfCompare extends TreeCondition { 306 protected FlexibleMapAccessor fieldAcsr; 307 protected FlexibleStringExpander valueExdr; 308 309 protected String operator; 310 protected String type; 311 protected FlexibleStringExpander formatExdr; 312 313 public IfCompare(ModelTree modelTree, Element condElement) { 314 super (modelTree, condElement); 315 this.fieldAcsr = new FlexibleMapAccessor(condElement.getAttribute("field-name")); 316 this.valueExdr = new FlexibleStringExpander(condElement.getAttribute("value")); 317 318 this.operator = condElement.getAttribute("operator"); 319 this.type = condElement.getAttribute("type"); 320 321 this.formatExdr = new FlexibleStringExpander(condElement.getAttribute("format")); 322 } 323 324 public boolean eval(Map context) { 325 String value = this.valueExdr.expandString(context); 326 String format = this.formatExdr.expandString(context); 327 328 Object fieldVal = this.fieldAcsr.get(context); 329 330 if (fieldVal == null) { 332 fieldVal = ""; 333 } 334 335 List messages = new LinkedList (); 336 Boolean resultBool = BaseCompare.doRealCompare(fieldVal, value, operator, type, format, messages, null, null); 337 if (messages.size() > 0) { 338 messages.add(0, "Error with comparison in if-compare between field [" + fieldAcsr.toString() + "] with value [" + fieldVal + "] and value [" + value + "] with operator [" + operator + "] and type [" + type + "]: "); 339 340 StringBuffer fullString = new StringBuffer (); 341 Iterator miter = messages.iterator(); 342 while (miter.hasNext()) { 343 fullString.append((String ) miter.next()); 344 } 345 Debug.logWarning(fullString.toString(), module); 346 347 throw new IllegalArgumentException (fullString.toString()); 348 } 349 350 return resultBool.booleanValue(); 351 } 352 } 353 354 public static class IfCompareField extends TreeCondition { 355 protected FlexibleMapAccessor fieldAcsr; 356 protected FlexibleMapAccessor toFieldAcsr; 357 358 protected String operator; 359 protected String type; 360 protected FlexibleStringExpander formatExdr; 361 362 public IfCompareField(ModelTree modelTree, Element condElement) { 363 super (modelTree, condElement); 364 this.fieldAcsr = new FlexibleMapAccessor(condElement.getAttribute("field-name")); 365 this.toFieldAcsr = new FlexibleMapAccessor(condElement.getAttribute("to-field-name")); 366 367 this.operator = condElement.getAttribute("operator"); 368 this.type = condElement.getAttribute("type"); 369 370 this.formatExdr = new FlexibleStringExpander(condElement.getAttribute("format")); 371 } 372 373 public boolean eval(Map context) { 374 String format = this.formatExdr.expandString(context); 375 376 Object fieldVal = this.fieldAcsr.get(context); 377 Object toFieldVal = this.toFieldAcsr.get(context); 378 379 if (fieldVal == null) { 381 fieldVal = ""; 382 } 383 384 List messages = new LinkedList (); 385 Boolean resultBool = BaseCompare.doRealCompare(fieldVal, toFieldVal, operator, type, format, messages, null, null); 386 if (messages.size() > 0) { 387 messages.add(0, "Error with comparison in if-compare-field between field [" + fieldAcsr.toString() + "] with value [" + fieldVal + "] and to-field [" + toFieldVal.toString() + "] with value [" + toFieldVal + "] with operator [" + operator + "] and type [" + type + "]: "); 388 389 StringBuffer fullString = new StringBuffer (); 390 Iterator miter = messages.iterator(); 391 while (miter.hasNext()) { 392 fullString.append((String ) miter.next()); 393 } 394 Debug.logWarning(fullString.toString(), module); 395 396 throw new IllegalArgumentException (fullString.toString()); 397 } 398 399 return resultBool.booleanValue(); 400 } 401 } 402 403 public static class IfRegexp extends TreeCondition { 404 static PatternMatcher matcher = new Perl5Matcher(); 405 static PatternCompiler compiler = new Perl5Compiler(); 406 407 protected FlexibleMapAccessor fieldAcsr; 408 protected FlexibleStringExpander exprExdr; 409 410 public IfRegexp(ModelTree modelTree, Element condElement) { 411 super (modelTree, condElement); 412 this.fieldAcsr = new FlexibleMapAccessor(condElement.getAttribute("field-name")); 413 this.exprExdr = new FlexibleStringExpander(condElement.getAttribute("expr")); 414 } 415 416 public boolean eval(Map context) { 417 Object fieldVal = this.fieldAcsr.get(context); 418 String expr = this.exprExdr.expandString(context); 419 Pattern pattern = null; 420 try { 421 pattern = compiler.compile(expr); 422 } catch (MalformedPatternException e) { 423 String errMsg = "Error in evaluation in if-regexp in screen: " + e.toString(); 424 Debug.logError(e, errMsg, module); 425 throw new IllegalArgumentException (errMsg); 426 } 427 428 String fieldString = null; 429 try { 430 fieldString = (String ) ObjectType.simpleTypeConvert(fieldVal, "String", null, null); 431 } catch (GeneralException e) { 432 Debug.logError(e, "Could not convert object to String, using empty String", module); 433 } 434 if (fieldString == null) fieldString = ""; 436 437 return matcher.matches(fieldString, pattern); 438 } 439 } 440 441 public static class IfEmpty extends TreeCondition { 442 protected FlexibleMapAccessor fieldAcsr; 443 444 public IfEmpty(ModelTree modelTree, Element condElement) { 445 super (modelTree, condElement); 446 this.fieldAcsr = new FlexibleMapAccessor(condElement.getAttribute("field-name")); 447 } 448 449 public boolean eval(Map context) { 450 Object fieldVal = this.fieldAcsr.get(context); 451 return ObjectType.isEmpty(fieldVal); 452 } 453 } 454 public static class IfEntityPermission extends TreeCondition { 455 protected EntityPermissionChecker permissionChecker; 456 457 public IfEntityPermission(ModelTree modelTree, Element condElement) { 458 super (modelTree, condElement); 459 this.permissionChecker = new EntityPermissionChecker(condElement); 460 } 461 462 public boolean eval(Map context) { 463 464 boolean passed = permissionChecker.runPermissionCheck(context); 465 return passed; 466 } 467 } 468 } 469 470 | Popular Tags |