001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.operation;
019:
020: import java.util.*;
021: import java.lang.reflect.*;
022:
023: import org.w3c.dom.*;
024:
025: import org.ofbiz.base.util.*;
026:
027: /**
028: * A string operation that calls a validation method
029: */
030: public class ValidateMethod extends SimpleMapOperation {
031:
032: public static final String module = ValidateMethod.class.getName();
033:
034: String methodName;
035: String className;
036:
037: public ValidateMethod(Element element,
038: SimpleMapProcess simpleMapProcess) {
039: super (element, simpleMapProcess);
040: this .methodName = element.getAttribute("method");
041: this .className = element.getAttribute("class");
042: }
043:
044: public void exec(Map inMap, Map results, List messages,
045: Locale locale, ClassLoader loader) {
046: Object obj = inMap.get(fieldName);
047:
048: String fieldValue = null;
049:
050: try {
051: fieldValue = (String) ObjectType.simpleTypeConvert(obj,
052: "String", null, locale);
053: } catch (GeneralException e) {
054: messages
055: .add("Could not convert field value for comparison: "
056: + e.getMessage());
057: return;
058: }
059:
060: if (loader == null) {
061: loader = Thread.currentThread().getContextClassLoader();
062: }
063:
064: Class[] paramTypes = new Class[] { String.class };
065: Object[] params = new Object[] { fieldValue };
066:
067: Class valClass;
068:
069: try {
070: valClass = loader.loadClass(className);
071: } catch (ClassNotFoundException cnfe) {
072: String msg = "Could not find validation class: "
073: + className;
074:
075: messages.add(msg);
076: Debug.logError("[ValidateMethod.exec] " + msg, module);
077: return;
078: }
079:
080: Method valMethod;
081:
082: try {
083: valMethod = valClass.getMethod(methodName, paramTypes);
084: } catch (NoSuchMethodException cnfe) {
085: String msg = "Could not find validation method: "
086: + methodName + " of class " + className;
087:
088: messages.add(msg);
089: Debug.logError("[ValidateMethod.exec] " + msg, module);
090: return;
091: }
092:
093: Boolean resultBool = Boolean.FALSE;
094:
095: try {
096: resultBool = (Boolean) valMethod.invoke(null, params);
097: } catch (Exception e) {
098: String msg = "Error in validation method " + methodName
099: + " of class " + className + ": " + e.getMessage();
100:
101: messages.add(msg);
102: Debug.logError("[ValidateMethod.exec] " + msg, module);
103: return;
104: }
105:
106: if (!resultBool.booleanValue()) {
107: addMessage(messages, loader, locale);
108: }
109: }
110: }
|