1 24 package org.ofbiz.minilang.operation; 25 26 import java.util.*; 27 import java.lang.reflect.*; 28 29 import org.w3c.dom.*; 30 31 import org.ofbiz.base.util.*; 32 33 40 public class ValidateMethod extends SimpleMapOperation { 41 42 public static final String module = ValidateMethod.class.getName(); 43 44 String methodName; 45 String className; 46 47 public ValidateMethod(Element element, SimpleMapProcess simpleMapProcess) { 48 super(element, simpleMapProcess); 49 this.methodName = element.getAttribute("method"); 50 this.className = element.getAttribute("class"); 51 } 52 53 public void exec(Map inMap, Map results, List messages, Locale locale, ClassLoader loader) { 54 Object obj = inMap.get(fieldName); 55 56 String fieldValue = null; 57 58 try { 59 fieldValue = (String ) ObjectType.simpleTypeConvert(obj, "String", null, locale); 60 } catch (GeneralException e) { 61 messages.add("Could not convert field value for comparison: " + e.getMessage()); 62 return; 63 } 64 65 if (loader == null) { 66 loader = Thread.currentThread().getContextClassLoader(); 67 } 68 69 Class [] paramTypes = new Class [] {String .class}; 70 Object [] params = new Object [] {fieldValue}; 71 72 Class valClass; 73 74 try { 75 valClass = loader.loadClass(className); 76 } catch (ClassNotFoundException cnfe) { 77 String msg = "Could not find validation class: " + className; 78 79 messages.add(msg); 80 Debug.logError("[ValidateMethod.exec] " + msg, module); 81 return; 82 } 83 84 Method valMethod; 85 86 try { 87 valMethod = valClass.getMethod(methodName, paramTypes); 88 } catch (NoSuchMethodException cnfe) { 89 String msg = "Could not find validation method: " + methodName + " of class " + className; 90 91 messages.add(msg); 92 Debug.logError("[ValidateMethod.exec] " + msg, module); 93 return; 94 } 95 96 Boolean resultBool = Boolean.FALSE; 97 98 try { 99 resultBool = (Boolean ) valMethod.invoke(null, params); 100 } catch (Exception e) { 101 String msg = "Error in validation method " + methodName + " of class " + className + ": " + e.getMessage(); 102 103 messages.add(msg); 104 Debug.logError("[ValidateMethod.exec] " + msg, module); 105 return; 106 } 107 108 if (!resultBool.booleanValue()) { 109 addMessage(messages, loader, locale); 110 } 111 } 112 } 113 | Popular Tags |