KickJava   Java API By Example, From Geeks To Geeks.

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


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

40 public class CompareFieldCondition implements Conditional {
41     
42     public static final String JavaDoc module = CompareFieldCondition.class.getName();
43     
44     SimpleMethod simpleMethod;
45     
46     ContextAccessor mapAcsr;
47     ContextAccessor fieldAcsr;
48     ContextAccessor toMapAcsr;
49     ContextAccessor toFieldAcsr;
50
51     String JavaDoc operator;
52     String JavaDoc type;
53     String JavaDoc format;
54     
55     public CompareFieldCondition(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         
61         this.toMapAcsr = new ContextAccessor(element.getAttribute("to-map-name"));
62         // set fieldAcsr to their defualt value of fieldAcsr if empty
63
this.toFieldAcsr = new ContextAccessor(element.getAttribute("to-field-name"), this.fieldAcsr.toString());
64
65         // do NOT default the to-map-name to the map-name because that
66
//would make it impossible to compare from a map field to an
67
//environment field
68

69         this.operator = element.getAttribute("operator");
70         this.type = element.getAttribute("type");
71         this.format = element.getAttribute("format");
72     }
73
74     public boolean checkCondition(MethodContext methodContext) {
75         String JavaDoc operator = methodContext.expandString(this.operator);
76         String JavaDoc type = methodContext.expandString(this.type);
77         String JavaDoc format = methodContext.expandString(this.format);
78
79         Object JavaDoc fieldVal1 = getFieldVal1(methodContext);
80         Object JavaDoc fieldVal2 = getFieldVal2(methodContext);
81
82         List messages = new LinkedList();
83         Boolean JavaDoc resultBool = BaseCompare.doRealCompare(fieldVal1, fieldVal2, operator, type, format, messages, null, methodContext.getLoader());
84
85         if (messages.size() > 0) {
86             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 + "]: ");
87             if (methodContext.getMethodType() == MethodContext.EVENT) {
88                 StringBuffer JavaDoc fullString = new StringBuffer JavaDoc();
89
90                 Iterator miter = messages.iterator();
91                 while (miter.hasNext()) {
92                     fullString.append((String JavaDoc) miter.next());
93                 }
94                 Debug.logWarning(fullString.toString(), module);
95
96                 methodContext.putEnv(simpleMethod.getEventErrorMessageName(), fullString.toString());
97                 methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
98             } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
99                 methodContext.putEnv(simpleMethod.getServiceErrorMessageListName(), messages);
100                 methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
101             }
102             return false;
103         }
104         
105         if (resultBool != null) return resultBool.booleanValue();
106         
107         return false;
108     }
109     
110     protected Object JavaDoc getFieldVal1(MethodContext methodContext) {
111         Object JavaDoc fieldVal1 = null;
112         if (!mapAcsr.isEmpty()) {
113             Map fromMap = (Map) mapAcsr.get(methodContext);
114             if (fromMap == null) {
115                 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", using null for comparison", module);
116             } else {
117                 fieldVal1 = fieldAcsr.get(fromMap, methodContext);
118             }
119         } else {
120             // no map name, try the env
121
fieldVal1 = fieldAcsr.get(methodContext);
122         }
123         return fieldVal1;
124     }
125
126     protected Object JavaDoc getFieldVal2(MethodContext methodContext) {
127         Object JavaDoc fieldVal2 = null;
128         if (!toMapAcsr.isEmpty()) {
129             Map toMap = (Map) toMapAcsr.get(methodContext);
130             if (toMap == null) {
131                 if (Debug.infoOn()) Debug.logInfo("To Map not found with name " + toMapAcsr + ", using null for comparison", module);
132             } else {
133                 fieldVal2 = toFieldAcsr.get(toMap, methodContext);
134             }
135         } else {
136             // no map name, try the env
137
fieldVal2 = toFieldAcsr.get(methodContext);
138         }
139         return fieldVal2;
140     }
141     
142     public void prettyPrint(StringBuffer JavaDoc messageBuffer, MethodContext methodContext) {
143         String JavaDoc operator = methodContext.expandString(this.operator);
144         String JavaDoc type = methodContext.expandString(this.type);
145         String JavaDoc format = methodContext.expandString(this.format);
146
147         Object JavaDoc fieldVal1 = getFieldVal1(methodContext);
148         Object JavaDoc fieldVal2 = getFieldVal2(methodContext);
149         
150         messageBuffer.append("[");
151         if (!this.mapAcsr.isEmpty()) {
152             messageBuffer.append(this.mapAcsr);
153             messageBuffer.append(".");
154         }
155         messageBuffer.append(this.fieldAcsr);
156         messageBuffer.append("=");
157         messageBuffer.append(fieldVal1);
158         messageBuffer.append("] ");
159
160         messageBuffer.append(operator);
161
162         messageBuffer.append(" [");
163         if (!this.toMapAcsr.isEmpty()) {
164             messageBuffer.append(this.toMapAcsr);
165             messageBuffer.append(".");
166         }
167         messageBuffer.append(this.toFieldAcsr);
168         messageBuffer.append("=");
169         messageBuffer.append(fieldVal2);
170         messageBuffer.append("] ");
171
172         messageBuffer.append(" as ");
173         messageBuffer.append(type);
174         if (UtilValidate.isNotEmpty(format)) {
175             messageBuffer.append(":");
176             messageBuffer.append(format);
177         }
178     }
179 }
180
Popular Tags