1 24 package org.ofbiz.minilang.method.conditional; 25 26 import java.util.*; 27 import java.lang.reflect.*; 28 import org.w3c.dom.*; 29 import org.ofbiz.base.util.*; 30 import org.ofbiz.minilang.method.*; 31 32 39 public class ValidateMethodCondition implements Conditional { 40 41 public static final String module = ValidateMethodCondition.class.getName(); 42 43 ContextAccessor mapAcsr; 44 ContextAccessor fieldAcsr; 45 String methodName; 46 String className; 47 48 public ValidateMethodCondition(Element element) { 49 this.mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 50 this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name")); 51 this.methodName = element.getAttribute("method"); 52 this.className = element.getAttribute("class"); 53 } 54 55 public boolean checkCondition(MethodContext methodContext) { 56 String methodName = methodContext.expandString(this.methodName); 57 String className = methodContext.expandString(this.className); 58 59 String fieldString = getFieldString(methodContext); 60 61 Class [] paramTypes = new Class [] {String .class}; 62 Object [] params = new Object [] {fieldString}; 63 64 Class valClass; 65 try { 66 valClass = methodContext.getLoader().loadClass(className); 67 } catch (ClassNotFoundException cnfe) { 68 Debug.logError("Could not find validation class: " + className, module); 69 return false; 70 } 71 72 Method valMethod; 73 try { 74 valMethod = valClass.getMethod(methodName, paramTypes); 75 } catch (NoSuchMethodException cnfe) { 76 Debug.logError("Could not find validation method: " + methodName + " of class " + className, module); 77 return false; 78 } 79 80 Boolean resultBool = Boolean.FALSE; 81 try { 82 resultBool = (Boolean ) valMethod.invoke(null, params); 83 } catch (Exception e) { 84 Debug.logError(e, "Error in IfValidationMethod " + methodName + " of class " + className + ", not processing sub-ops ", module); 85 } 86 87 if (resultBool != null) return resultBool.booleanValue(); 88 89 return false; 90 } 91 92 protected String getFieldString(MethodContext methodContext) { 93 String fieldString = null; 94 Object fieldVal = null; 95 96 if (!mapAcsr.isEmpty()) { 97 Map fromMap = (Map) mapAcsr.get(methodContext); 98 if (fromMap == null) { 99 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", using empty string for comparison", module); 100 } else { 101 fieldVal = fieldAcsr.get(fromMap, methodContext); 102 } 103 } else { 104 fieldVal = fieldAcsr.get(methodContext); 106 } 107 108 if (fieldVal != null) { 109 try { 110 fieldString = (String ) ObjectType.simpleTypeConvert(fieldVal, "String", null, null); 111 } catch (GeneralException e) { 112 Debug.logError(e, "Could not convert object to String, using empty String", module); 113 } 114 } 115 116 if (fieldString == null) fieldString = ""; 118 119 return fieldString; 120 } 121 122 public void prettyPrint(StringBuffer messageBuffer, MethodContext methodContext) { 123 String methodName = methodContext == null ? this.methodName : methodContext.expandString(this.methodName); 125 String className = methodContext == null ? this.className : methodContext.expandString(this.className); 126 127 messageBuffer.append("validate-method["); 128 messageBuffer.append(className); 129 messageBuffer.append("."); 130 messageBuffer.append(methodName); 131 messageBuffer.append("("); 132 if (!this.mapAcsr.isEmpty()) { 133 messageBuffer.append(this.mapAcsr); 134 messageBuffer.append("."); 135 } 136 messageBuffer.append(this.fieldAcsr); 137 if (methodContext != null) { 138 messageBuffer.append("="); 139 messageBuffer.append(getFieldString(methodContext)); 140 } 141 messageBuffer.append(")]"); 142 } 143 } 144 | Popular Tags |