KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > eca > ServiceEcaCondition


1 /*
2  * $Id: ServiceEcaCondition.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 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  */

25 package org.ofbiz.service.eca;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.ofbiz.service.DispatchContext;
33 import org.ofbiz.service.GenericServiceException;
34 import org.ofbiz.service.LocalDispatcher;
35 import org.ofbiz.service.ServiceUtil;
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.ObjectType;
38 import org.ofbiz.base.util.UtilMisc;
39
40 import org.w3c.dom.Element JavaDoc;
41
42 /**
43  * ServiceEcaCondition
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 2.0
48  */

49 public class ServiceEcaCondition implements java.io.Serializable JavaDoc {
50     
51     public static final String JavaDoc module = ServiceEcaCondition.class.getName();
52
53     protected String JavaDoc conditionService = null;
54     protected String JavaDoc lhsValueName = null;
55     protected String JavaDoc rhsValueName = null;
56     protected String JavaDoc lhsMapName = null;
57     protected String JavaDoc rhsMapName = null;
58     protected String JavaDoc operator = null;
59     protected String JavaDoc compareType = null;
60     protected String JavaDoc format = null;
61     protected boolean isConstant = false;
62     protected boolean isService = false;
63
64     protected ServiceEcaCondition() {}
65
66     public ServiceEcaCondition(Element JavaDoc condition, boolean isConstant, boolean isService) {
67         if (isService) {
68             this.isService = isService;
69             this.conditionService = condition.getAttribute("service-name");
70         } else {
71             this.lhsValueName = condition.getAttribute("field-name");
72             this.lhsMapName = condition.getAttribute("map-name");
73
74             this.isConstant = isConstant;
75             if (isConstant) {
76                 this.rhsValueName = condition.getAttribute("value");
77                 this.rhsMapName = null;
78             } else {
79                 this.rhsValueName = condition.getAttribute("to-field-name");
80                 this.rhsMapName = condition.getAttribute("to-map-name");
81             }
82
83             this.operator = condition.getAttribute("operator");
84             this.compareType = condition.getAttribute("type");
85             this.format = condition.getAttribute("format");
86
87             if (lhsValueName == null) {
88                 lhsValueName = "";
89             }
90             if (rhsValueName == null) {
91                 rhsValueName = "";
92             }
93         }
94     }
95
96     public boolean eval(String JavaDoc serviceName, DispatchContext dctx, Map JavaDoc context) throws GenericServiceException {
97         if (serviceName == null || dctx == null || context == null || dctx.getClassLoader() == null) {
98             throw new GenericServiceException("Cannot have null Service, Context or DispatchContext!");
99         }
100
101         if (Debug.verboseOn()) Debug.logVerbose(this.toString() + ", In the context: " + context, module);
102
103         // condition-service; run the service and return the reply result
104
if (isService) {
105             LocalDispatcher dispatcher = dctx.getDispatcher();
106             Map JavaDoc conditionServiceResult = dispatcher.runSync(conditionService,
107                     UtilMisc.toMap("serviceContext", context, "serviceName", serviceName,
108                             "userLogin", context.get("userLogin")));
109
110             Boolean JavaDoc conditionReply = Boolean.FALSE;
111             if (ServiceUtil.isError(conditionServiceResult)) {
112                 Debug.logError("Error in condition-service : " +
113                         ServiceUtil.getErrorMessage(conditionServiceResult), module);
114             } else {
115                 conditionReply = (Boolean JavaDoc) conditionServiceResult.get("conditionReply");
116             }
117             return conditionReply.booleanValue();
118         }
119
120         Object JavaDoc lhsValue = null;
121         Object JavaDoc rhsValue = null;
122         if (lhsMapName != null && lhsMapName.length() > 0) {
123             try {
124                 if (context.containsKey(lhsMapName)) {
125                     Map JavaDoc envMap = (Map JavaDoc) context.get(lhsMapName);
126                     lhsValue = envMap.get(lhsValueName);
127                 } else {
128                     Debug.logWarning("From Map (" + lhsMapName + ") not found in context, defaulting to null.", module);
129                 }
130             } catch (ClassCastException JavaDoc e) {
131                 throw new GenericServiceException("From Map field [" + lhsMapName + "] is not a Map.", e);
132             }
133         } else {
134             if (context.containsKey(lhsValueName)) {
135                 lhsValue = context.get(lhsValueName);
136             } else {
137                 Debug.logWarning("From Field (" + lhsValueName + ") is not found in context for " + serviceName + ", defaulting to null.", module);
138             }
139         }
140
141         if (isConstant) {
142             rhsValue = rhsValueName;
143         } else if (rhsMapName != null && rhsMapName.length() > 0) {
144             try {
145                 if (context.containsKey(rhsMapName)) {
146                     Map JavaDoc envMap = (Map JavaDoc) context.get(rhsMapName);
147                     rhsValue = envMap.get(rhsValueName);
148                 } else {
149                     Debug.logWarning("To Map (" + rhsMapName + ") not found in context for " + serviceName + ", defaulting to null.", module);
150                 }
151             } catch (ClassCastException JavaDoc e) {
152                 throw new GenericServiceException("To Map field [" + rhsMapName + "] is not a Map.", e);
153             }
154         } else {
155             if (context.containsKey(rhsValueName)) {
156                 rhsValue = context.get(rhsValueName);
157             } else {
158                 Debug.logInfo("To Field (" + rhsValueName + ") is not found in context for " + serviceName + ", defaulting to null.", module);
159             }
160         }
161
162         if (Debug.verboseOn()) Debug.logVerbose("Comparing : " + lhsValue + " " + operator + " " + rhsValue, module);
163
164         // evaluate the condition & invoke the action(s)
165
List JavaDoc messages = new LinkedList JavaDoc();
166         Boolean JavaDoc cond = ObjectType.doRealCompare(lhsValue, rhsValue, operator, compareType, format, messages, null, dctx.getClassLoader());
167
168         // if any messages were returned send them out
169
if (messages.size() > 0) {
170             Iterator JavaDoc m = messages.iterator();
171             while (m.hasNext()) {
172                 Debug.logWarning((String JavaDoc) m.next(), module);
173             }
174         }
175         if (cond != null) {
176             return cond.booleanValue();
177         } else {
178             Debug.logWarning("doRealCompare returned null, returning false", module);
179             return false;
180         }
181     }
182
183     public String JavaDoc toString() {
184         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
185
186         buf.append("[" + conditionService + "]");
187         buf.append("[" + lhsMapName + "]");
188         buf.append("[" + lhsValueName + "]");
189         buf.append("[" + operator + "]");
190         buf.append("[" + rhsMapName + "]");
191         buf.append("[" + rhsValueName + "]");
192         buf.append("[" + isConstant + "]");
193         buf.append("[" + compareType + "]");
194         buf.append("[" + format + "]");
195         return buf.toString();
196     }
197 }
198
Popular Tags