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