KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: IfCompare.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
28 import org.w3c.dom.*;
29 import org.ofbiz.base.util.*;
30 import org.ofbiz.minilang.*;
31 import org.ofbiz.minilang.method.*;
32
33 import org.ofbiz.minilang.operation.*;
34
35 /**
36  * Iff the comparison between the constant and the specified field is true process sub-operations
37  *
38  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
39  * @version $Rev: 5462 $
40  * @since 2.0
41  */

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

79         String JavaDoc value = methodContext.expandString(this.value);
80         String JavaDoc operator = methodContext.expandString(this.operator);
81         String JavaDoc type = methodContext.expandString(this.type);
82         String JavaDoc format = methodContext.expandString(this.format);
83         
84         Object JavaDoc fieldVal = null;
85         if (!mapAcsr.isEmpty()) {
86             Map fromMap = (Map) mapAcsr.get(methodContext);
87             if (fromMap == null) {
88                 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", using empty string for comparison", module);
89             } else {
90                 fieldVal = fieldAcsr.get(fromMap, methodContext);
91             }
92         } else {
93             // no map name, try the env
94
fieldVal = fieldAcsr.get(methodContext);
95         }
96
97         // always use an empty string by default
98
if (fieldVal == null) {
99             fieldVal = "";
100         }
101
102         List messages = new LinkedList();
103         Boolean JavaDoc resultBool = BaseCompare.doRealCompare(fieldVal, value, operator, type, format, messages, null, methodContext.getLoader());
104         if (messages.size() > 0) {
105             messages.add(0, "Error with comparison in if-compare between field [" + mapAcsr.toString() + "." + fieldAcsr.toString() + "] with value [" + fieldVal + "] and value [" + value + "] with operator [" + operator + "] and type [" + type + "]: ");
106             if (methodContext.getMethodType() == MethodContext.EVENT) {
107                 StringBuffer JavaDoc fullString = new StringBuffer JavaDoc();
108                 
109                 Iterator miter = messages.iterator();
110                 while (miter.hasNext()) {
111                     fullString.append((String JavaDoc) miter.next());
112                 }
113                 Debug.logWarning(fullString.toString(), module);
114
115                 methodContext.putEnv(simpleMethod.getEventErrorMessageName(), fullString.toString());
116                 methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
117             } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
118                 methodContext.putEnv(simpleMethod.getServiceErrorMessageListName(), messages);
119                 methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
120             }
121             return false;
122         }
123
124         if (resultBool != null && resultBool.booleanValue()) {
125             return SimpleMethod.runSubOps(subOps, methodContext);
126         } else {
127             if (elseSubOps != null) {
128                 return SimpleMethod.runSubOps(elseSubOps, methodContext);
129             } else {
130                 return true;
131             }
132         }
133     }
134
135     public String JavaDoc rawString() {
136         // TODO: add all attributes and other info
137
return "<if-compare field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\" value=\"" + value + "\"/>";
138     }
139     public String JavaDoc expandedString(MethodContext methodContext) {
140         // TODO: something more than a stub/dummy
141
return this.rawString();
142     }
143 }
144
Popular Tags