001: /*
002: * $Id: ValidateMethodCondition.java,v 1.1 2003/08/17 06:06:12 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.conditional;
025:
026: import java.util.*;
027: import java.lang.reflect.*;
028: import org.w3c.dom.*;
029: import org.ofbiz.base.util.*;
030: import org.ofbiz.minilang.method.*;
031:
032: /**
033: * Implements validate method condition.
034: *
035: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
036: * @version $Revision: 1.1 $
037: * @since 2.1
038: */
039: public class ValidateMethodCondition implements Conditional {
040:
041: public static final String module = ValidateMethodCondition.class
042: .getName();
043:
044: ContextAccessor mapAcsr;
045: ContextAccessor fieldAcsr;
046: String methodName;
047: String className;
048:
049: public ValidateMethodCondition(Element element) {
050: this .mapAcsr = new ContextAccessor(element
051: .getAttribute("map-name"));
052: this .fieldAcsr = new ContextAccessor(element
053: .getAttribute("field-name"));
054: this .methodName = element.getAttribute("method");
055: this .className = element.getAttribute("class");
056: }
057:
058: public boolean checkCondition(MethodContext methodContext) {
059: String methodName = methodContext.expandString(this .methodName);
060: String className = methodContext.expandString(this .className);
061:
062: String fieldString = null;
063: Object fieldVal = null;
064:
065: if (!mapAcsr.isEmpty()) {
066: Map fromMap = (Map) mapAcsr.get(methodContext);
067: if (fromMap == null) {
068: if (Debug.infoOn())
069: Debug.logInfo("Map not found with name " + mapAcsr
070: + ", using empty string for comparison",
071: module);
072: } else {
073: fieldVal = fieldAcsr.get(fromMap, methodContext);
074: }
075: } else {
076: // no map name, try the env
077: fieldVal = fieldAcsr.get(methodContext);
078: }
079:
080: if (fieldVal != null) {
081: try {
082: fieldString = (String) ObjectType.simpleTypeConvert(
083: fieldVal, "String", null, null);
084: } catch (GeneralException e) {
085: Debug
086: .logError(
087: e,
088: "Could not convert object to String, using empty String",
089: module);
090: }
091: }
092:
093: // always use an empty string by default
094: if (fieldString == null)
095: fieldString = "";
096:
097: Class[] paramTypes = new Class[] { String.class };
098: Object[] params = new Object[] { fieldString };
099:
100: Class valClass;
101: try {
102: valClass = methodContext.getLoader().loadClass(className);
103: } catch (ClassNotFoundException cnfe) {
104: Debug.logError("Could not find validation class: "
105: + className, module);
106: return false;
107: }
108:
109: Method valMethod;
110: try {
111: valMethod = valClass.getMethod(methodName, paramTypes);
112: } catch (NoSuchMethodException cnfe) {
113: Debug.logError("Could not find validation method: "
114: + methodName + " of class " + className, module);
115: return false;
116: }
117:
118: Boolean resultBool = Boolean.FALSE;
119: try {
120: resultBool = (Boolean) valMethod.invoke(null, params);
121: } catch (Exception e) {
122: Debug.logError(e, "Error in IfValidationMethod "
123: + methodName + " of class " + className
124: + ", not processing sub-ops ", module);
125: }
126:
127: if (resultBool != null)
128: return resultBool.booleanValue();
129:
130: return false;
131: }
132: }
|