001: /*
002: * $Id: IfValidateMethod.java,v 1.1 2003/08/17 06:06:13 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.ifops;
025:
026: import java.util.*;
027: import java.lang.reflect.*;
028:
029: import org.w3c.dom.*;
030: import org.ofbiz.base.util.*;
031: import org.ofbiz.minilang.*;
032: import org.ofbiz.minilang.method.*;
033:
034: /**
035: * Iff the validate method returns true with the specified field process sub-operations
036: *
037: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
038: * @version $Revision: 1.1 $
039: * @since 2.0
040: */
041: public class IfValidateMethod extends MethodOperation {
042:
043: public static final String module = IfValidateMethod.class
044: .getName();
045:
046: List subOps = new LinkedList();
047: List elseSubOps = null;
048:
049: ContextAccessor mapAcsr;
050: ContextAccessor fieldAcsr;
051: String methodName;
052: String className;
053:
054: public IfValidateMethod(Element element, SimpleMethod simpleMethod) {
055: super (element, simpleMethod);
056: this .mapAcsr = new ContextAccessor(element
057: .getAttribute("map-name"));
058: this .fieldAcsr = new ContextAccessor(element
059: .getAttribute("field-name"));
060: this .methodName = element.getAttribute("method");
061: this .className = element.getAttribute("class");
062:
063: SimpleMethod.readOperations(element, subOps, simpleMethod);
064:
065: Element elseElement = UtilXml
066: .firstChildElement(element, "else");
067: if (elseElement != null) {
068: elseSubOps = new LinkedList();
069: SimpleMethod.readOperations(elseElement, elseSubOps,
070: simpleMethod);
071: }
072: }
073:
074: public boolean exec(MethodContext methodContext) {
075: // if conditions fails, always return true; if a sub-op returns false
076: // return false and stop, otherwise return true
077:
078: String methodName = methodContext.expandString(this .methodName);
079: String className = methodContext.expandString(this .className);
080:
081: String fieldString = null;
082: Object fieldVal = null;
083:
084: if (!mapAcsr.isEmpty()) {
085: Map fromMap = (Map) mapAcsr.get(methodContext);
086: if (fromMap == null) {
087: if (Debug.infoOn())
088: Debug.logInfo("Map not found with name " + mapAcsr
089: + ", using empty string for comparison",
090: module);
091: } else {
092: fieldVal = fieldAcsr.get(fromMap, methodContext);
093: }
094: } else {
095: // no map name, try the env
096: fieldVal = fieldAcsr.get(methodContext);
097: }
098:
099: if (fieldVal != null) {
100: try {
101: fieldString = (String) ObjectType.simpleTypeConvert(
102: fieldVal, "String", null, null);
103: } catch (GeneralException e) {
104: Debug
105: .logError(
106: e,
107: "Could not convert object to String, using empty String",
108: module);
109: }
110: }
111:
112: // always use an empty string by default
113: if (fieldString == null)
114: fieldString = "";
115:
116: Class[] paramTypes = new Class[] { String.class };
117: Object[] params = new Object[] { fieldString };
118:
119: Class valClass;
120: try {
121: valClass = methodContext.getLoader().loadClass(className);
122: } catch (ClassNotFoundException cnfe) {
123: Debug.logError("Could not find validation class: "
124: + className, module);
125: return false;
126: }
127:
128: Method valMethod;
129: try {
130: valMethod = valClass.getMethod(methodName, paramTypes);
131: } catch (NoSuchMethodException cnfe) {
132: Debug.logError("Could not find validation method: "
133: + methodName + " of class " + className, module);
134: return false;
135: }
136:
137: Boolean resultBool = Boolean.FALSE;
138: try {
139: resultBool = (Boolean) valMethod.invoke(null, params);
140: } catch (Exception e) {
141: Debug.logError(e, "Error in IfValidationMethod "
142: + methodName + " of class " + className
143: + ", not processing sub-ops ", module);
144: }
145:
146: if (resultBool.booleanValue()) {
147: return SimpleMethod.runSubOps(subOps, methodContext);
148: } else {
149: if (elseSubOps != null) {
150: return SimpleMethod
151: .runSubOps(elseSubOps, methodContext);
152: } else {
153: return true;
154: }
155: }
156: }
157: }
|