KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: IfCompareField.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 specified field and the other 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 IfCompareField extends MethodOperation {
43     
44     public static final String JavaDoc module = IfCompareField.class.getName();
45
46     List subOps = new LinkedList();
47     List elseSubOps = null;
48
49     ContextAccessor mapAcsr;
50     ContextAccessor fieldAcsr;
51     ContextAccessor toMapAcsr;
52     ContextAccessor toFieldAcsr;
53
54     String JavaDoc operator;
55     String JavaDoc type;
56     String JavaDoc format;
57
58     public IfCompareField(Element element, SimpleMethod simpleMethod) {
59         super(element, simpleMethod);
60         this.mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
61         this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
62         
63         this.toMapAcsr = new ContextAccessor(element.getAttribute("to-map-name"));
64         // set fieldAcsr to their defualt value of fieldAcsr if empty
65
this.toFieldAcsr = new ContextAccessor(element.getAttribute("to-field-name"), element.getAttribute("field-name"));
66
67         // do NOT default the to-map-name to the map-name because that
68
//would make it impossible to compare from a map field to an
69
//environment field
70

71         this.operator = element.getAttribute("operator");
72         this.type = element.getAttribute("type");
73         this.format = element.getAttribute("format");
74
75         SimpleMethod.readOperations(element, subOps, simpleMethod);
76         Element elseElement = UtilXml.firstChildElement(element, "else");
77         if (elseElement != null) {
78             elseSubOps = new LinkedList();
79             SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod);
80         }
81     }
82
83     public boolean exec(MethodContext methodContext) {
84         // if conditions fails, always return true; if a sub-op returns false
85
// return false and stop, otherwise return true
86

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