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