KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: CompareCondition.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
28 import org.w3c.dom.*;
29 import org.ofbiz.base.util.*;
30 import org.ofbiz.minilang.*;
31 import org.ofbiz.minilang.method.*;
32 import org.ofbiz.minilang.operation.*;
33
34 /**
35  * Implements compare to a constant condition.
36  *
37  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
38  * @version $Rev: 5462 $
39  * @since 2.1
40  */

41 public class CompareCondition implements Conditional {
42     
43     public static final String JavaDoc module = CompareCondition.class.getName();
44     
45     SimpleMethod simpleMethod;
46     
47     ContextAccessor mapAcsr;
48     ContextAccessor fieldAcsr;
49     String JavaDoc value;
50
51     String JavaDoc operator;
52     String JavaDoc type;
53     String JavaDoc format;
54     
55     public CompareCondition(Element element, SimpleMethod simpleMethod) {
56         this.simpleMethod = simpleMethod;
57         
58         this.mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
59         this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
60         this.value = element.getAttribute("value");
61
62         this.operator = element.getAttribute("operator");
63         this.type = element.getAttribute("type");
64         this.format = element.getAttribute("format");
65     }
66
67     public boolean checkCondition(MethodContext methodContext) {
68         String JavaDoc value = methodContext.expandString(this.value);
69         String JavaDoc operator = methodContext.expandString(this.operator);
70         String JavaDoc type = methodContext.expandString(this.type);
71         String JavaDoc format = methodContext.expandString(this.format);
72         
73         Object JavaDoc fieldVal = getFieldVal(methodContext);
74
75         List messages = new LinkedList();
76         Boolean JavaDoc resultBool = BaseCompare.doRealCompare(fieldVal, value, operator, type, format, messages, null, methodContext.getLoader());
77         if (messages.size() > 0) {
78             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 + "]: ");
79             if (methodContext.getMethodType() == MethodContext.EVENT) {
80                 StringBuffer JavaDoc fullString = new StringBuffer JavaDoc();
81                 
82                 Iterator miter = messages.iterator();
83                 while (miter.hasNext()) {
84                     fullString.append((String JavaDoc) miter.next());
85                 }
86                 Debug.logWarning(fullString.toString(), module);
87
88                 methodContext.putEnv(simpleMethod.getEventErrorMessageName(), fullString.toString());
89                 methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
90             } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
91                 methodContext.putEnv(simpleMethod.getServiceErrorMessageListName(), messages);
92                 methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
93             }
94             return false;
95         }
96         
97         if (resultBool != null) return resultBool.booleanValue();
98         
99         return false;
100     }
101     
102     protected Object JavaDoc getFieldVal(MethodContext methodContext) {
103         Object JavaDoc fieldVal = null;
104         if (!mapAcsr.isEmpty()) {
105             Map fromMap = (Map) mapAcsr.get(methodContext);
106             if (fromMap == null) {
107                 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", using empty string for comparison", module);
108             } else {
109                 fieldVal = fieldAcsr.get(fromMap, methodContext);
110             }
111         } else {
112             // no map name, try the env
113
fieldVal = fieldAcsr.get(methodContext);
114         }
115
116         // always use an empty string by default
117
if (fieldVal == null) {
118             fieldVal = "";
119         }
120         return fieldVal;
121     }
122
123     public void prettyPrint(StringBuffer JavaDoc messageBuffer, MethodContext methodContext) {
124         String JavaDoc value = methodContext.expandString(this.value);
125         String JavaDoc operator = methodContext.expandString(this.operator);
126         String JavaDoc type = methodContext.expandString(this.type);
127         String JavaDoc format = methodContext.expandString(this.format);
128         Object JavaDoc fieldVal = getFieldVal(methodContext);
129         
130         messageBuffer.append("[");
131         if (!this.mapAcsr.isEmpty()) {
132             messageBuffer.append(this.mapAcsr);
133             messageBuffer.append(".");
134         }
135         messageBuffer.append(this.fieldAcsr);
136         messageBuffer.append("=");
137         messageBuffer.append(fieldVal);
138         messageBuffer.append("] ");
139
140         messageBuffer.append(operator);
141
142         messageBuffer.append(" ");
143         messageBuffer.append(value);
144         messageBuffer.append(" as ");
145         messageBuffer.append(type);
146         if (UtilValidate.isNotEmpty(format)) {
147             messageBuffer.append(":");
148             messageBuffer.append(format);
149         }
150     }
151 }
152
Popular Tags