KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > ifops > IfValidateMethod


1 /*
2  * $Id: IfValidateMethod.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 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.ifops;
25
26 import java.util.*;
27 import java.lang.reflect.*;
28
29 import org.w3c.dom.*;
30 import org.ofbiz.base.util.*;
31 import org.ofbiz.minilang.*;
32 import org.ofbiz.minilang.method.*;
33
34 /**
35  * Iff the validate method returns true with the specified field process sub-operations
36  *
37  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
38  * @version $Rev: 5462 $
39  * @since 2.0
40  */

41 public class IfValidateMethod extends MethodOperation {
42     
43     public static final String JavaDoc module = IfValidateMethod.class.getName();
44
45     List subOps = new LinkedList();
46     List elseSubOps = null;
47
48     ContextAccessor mapAcsr;
49     ContextAccessor fieldAcsr;
50     String JavaDoc methodName;
51     String JavaDoc className;
52
53     public IfValidateMethod(Element element, SimpleMethod simpleMethod) {
54         super(element, simpleMethod);
55         this.mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
56         this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
57         this.methodName = element.getAttribute("method");
58         this.className = element.getAttribute("class");
59
60         SimpleMethod.readOperations(element, subOps, simpleMethod);
61
62         Element elseElement = UtilXml.firstChildElement(element, "else");
63         if (elseElement != null) {
64             elseSubOps = new LinkedList();
65             SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod);
66         }
67     }
68
69     public boolean exec(MethodContext methodContext) {
70         // if conditions fails, always return true; if a sub-op returns false
71
// return false and stop, otherwise return true
72

73         String JavaDoc methodName = methodContext.expandString(this.methodName);
74         String JavaDoc className = methodContext.expandString(this.className);
75
76         String JavaDoc fieldString = null;
77         Object JavaDoc fieldVal = null;
78
79         if (!mapAcsr.isEmpty()) {
80             Map fromMap = (Map) mapAcsr.get(methodContext);
81             if (fromMap == null) {
82                 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", using empty string for comparison", module);
83             } else {
84                 fieldVal = fieldAcsr.get(fromMap, methodContext);
85             }
86         } else {
87             // no map name, try the env
88
fieldVal = fieldAcsr.get(methodContext);
89         }
90
91         if (fieldVal != null) {
92             try {
93                 fieldString = (String JavaDoc) ObjectType.simpleTypeConvert(fieldVal, "String", null, null);
94             } catch (GeneralException e) {
95                 Debug.logError(e, "Could not convert object to String, using empty String", module);
96             }
97         }
98
99         // always use an empty string by default
100
if (fieldString == null) fieldString = "";
101
102         Class JavaDoc[] paramTypes = new Class JavaDoc[] {String JavaDoc.class};
103         Object JavaDoc[] params = new Object JavaDoc[] {fieldString};
104
105         Class JavaDoc valClass;
106         try {
107             valClass = methodContext.getLoader().loadClass(className);
108         } catch (ClassNotFoundException JavaDoc cnfe) {
109             Debug.logError("Could not find validation class: " + className, module);
110             return false;
111         }
112
113         Method valMethod;
114         try {
115             valMethod = valClass.getMethod(methodName, paramTypes);
116         } catch (NoSuchMethodException JavaDoc cnfe) {
117             Debug.logError("Could not find validation method: " + methodName + " of class " + className, module);
118             return false;
119         }
120
121         Boolean JavaDoc resultBool = Boolean.FALSE;
122         try {
123             resultBool = (Boolean JavaDoc) valMethod.invoke(null, params);
124         } catch (Exception JavaDoc e) {
125             Debug.logError(e, "Error in IfValidationMethod " + methodName + " of class " + className + ", not processing sub-ops ", module);
126         }
127
128         if (resultBool.booleanValue()) {
129             return SimpleMethod.runSubOps(subOps, methodContext);
130         } else {
131             if (elseSubOps != null) {
132                 return SimpleMethod.runSubOps(elseSubOps, methodContext);
133             } else {
134                 return true;
135             }
136         }
137     }
138
139     public String JavaDoc rawString() {
140         // TODO: add all attributes and other info
141
return "<if-validate-method field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
142     }
143     public String JavaDoc expandedString(MethodContext methodContext) {
144         // TODO: something more than a stub/dummy
145
return this.rawString();
146     }
147 }
148
Popular Tags