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