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