KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > conditional > ValidateMethodCondition


1 /*
2  * $Id: ValidateMethodCondition.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.conditional;
25
26 import java.util.*;
27 import java.lang.reflect.*;
28 import org.w3c.dom.*;
29 import org.ofbiz.base.util.*;
30 import org.ofbiz.minilang.method.*;
31
32 /**
33  * Implements validate method condition.
34  *
35  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
36  * @version $Rev: 5462 $
37  * @since 2.1
38  */

39 public class ValidateMethodCondition implements Conditional {
40     
41     public static final String JavaDoc module = ValidateMethodCondition.class.getName();
42     
43     ContextAccessor mapAcsr;
44     ContextAccessor fieldAcsr;
45     String JavaDoc methodName;
46     String JavaDoc className;
47     
48     public ValidateMethodCondition(Element element) {
49         this.mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
50         this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
51         this.methodName = element.getAttribute("method");
52         this.className = element.getAttribute("class");
53     }
54
55     public boolean checkCondition(MethodContext methodContext) {
56         String JavaDoc methodName = methodContext.expandString(this.methodName);
57         String JavaDoc className = methodContext.expandString(this.className);
58
59         String JavaDoc fieldString = getFieldString(methodContext);
60
61         Class JavaDoc[] paramTypes = new Class JavaDoc[] {String JavaDoc.class};
62         Object JavaDoc[] params = new Object JavaDoc[] {fieldString};
63
64         Class JavaDoc valClass;
65         try {
66             valClass = methodContext.getLoader().loadClass(className);
67         } catch (ClassNotFoundException JavaDoc cnfe) {
68             Debug.logError("Could not find validation class: " + className, module);
69             return false;
70         }
71
72         Method valMethod;
73         try {
74             valMethod = valClass.getMethod(methodName, paramTypes);
75         } catch (NoSuchMethodException JavaDoc cnfe) {
76             Debug.logError("Could not find validation method: " + methodName + " of class " + className, module);
77             return false;
78         }
79
80         Boolean JavaDoc resultBool = Boolean.FALSE;
81         try {
82             resultBool = (Boolean JavaDoc) valMethod.invoke(null, params);
83         } catch (Exception JavaDoc e) {
84             Debug.logError(e, "Error in IfValidationMethod " + methodName + " of class " + className + ", not processing sub-ops ", module);
85         }
86         
87         if (resultBool != null) return resultBool.booleanValue();
88         
89         return false;
90     }
91     
92     protected String JavaDoc getFieldString(MethodContext methodContext) {
93         String JavaDoc fieldString = null;
94         Object JavaDoc fieldVal = null;
95
96         if (!mapAcsr.isEmpty()) {
97             Map fromMap = (Map) mapAcsr.get(methodContext);
98             if (fromMap == null) {
99                 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", using empty string for comparison", module);
100             } else {
101                 fieldVal = fieldAcsr.get(fromMap, methodContext);
102             }
103         } else {
104             // no map name, try the env
105
fieldVal = fieldAcsr.get(methodContext);
106         }
107
108         if (fieldVal != null) {
109             try {
110                 fieldString = (String JavaDoc) ObjectType.simpleTypeConvert(fieldVal, "String", null, null);
111             } catch (GeneralException e) {
112                 Debug.logError(e, "Could not convert object to String, using empty String", module);
113             }
114         }
115
116         // always use an empty string by default
117
if (fieldString == null) fieldString = "";
118
119         return fieldString;
120     }
121
122     public void prettyPrint(StringBuffer JavaDoc messageBuffer, MethodContext methodContext) {
123         // allow methodContext to be null
124
String JavaDoc methodName = methodContext == null ? this.methodName : methodContext.expandString(this.methodName);
125         String JavaDoc className = methodContext == null ? this.className : methodContext.expandString(this.className);
126
127         messageBuffer.append("validate-method[");
128         messageBuffer.append(className);
129         messageBuffer.append(".");
130         messageBuffer.append(methodName);
131         messageBuffer.append("(");
132         if (!this.mapAcsr.isEmpty()) {
133             messageBuffer.append(this.mapAcsr);
134             messageBuffer.append(".");
135         }
136         messageBuffer.append(this.fieldAcsr);
137         if (methodContext != null) {
138             messageBuffer.append("=");
139             messageBuffer.append(getFieldString(methodContext));
140         }
141         messageBuffer.append(")]");
142     }
143 }
144
Popular Tags