KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ServiceEcaRule.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  */

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 import java.util.Set JavaDoc;
32
33 import org.ofbiz.service.DispatchContext;
34 import org.ofbiz.service.GenericServiceException;
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.UtilXml;
37 import org.w3c.dom.Element JavaDoc;
38
39 /**
40  * ServiceEcaRule
41  *
42  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
43  * @version $Rev: 5462 $
44  * @since 2.0
45  */

46 public class ServiceEcaRule implements java.io.Serializable JavaDoc {
47
48     public static final String JavaDoc module = ServiceEcaRule.class.getName();
49
50     protected String JavaDoc serviceName = null;
51     protected String JavaDoc eventName = null;
52     protected boolean runOnFailure = false;
53     protected boolean runOnError = false;
54     protected List JavaDoc conditions = new LinkedList JavaDoc();
55     protected List JavaDoc actions = new LinkedList JavaDoc();
56     protected boolean enabled = true;
57
58     protected ServiceEcaRule() {}
59
60     public ServiceEcaRule(Element JavaDoc eca) {
61         this.serviceName = eca.getAttribute("service");
62         this.eventName = eca.getAttribute("event");
63         this.runOnFailure = "true".equals(eca.getAttribute("run-on-failure"));
64         this.runOnError = "true".equals(eca.getAttribute("run-on-error"));
65
66         List JavaDoc condList = UtilXml.childElementList(eca, "condition");
67         Iterator JavaDoc ci = condList.iterator();
68
69         while (ci.hasNext()) {
70             conditions.add(new ServiceEcaCondition((Element JavaDoc) ci.next(), true, false));
71         }
72
73         List JavaDoc condFList = UtilXml.childElementList(eca, "condition-field");
74         Iterator JavaDoc cfi = condFList.iterator();
75
76         while (cfi.hasNext()) {
77             conditions.add(new ServiceEcaCondition((Element JavaDoc) cfi.next(), false, false));
78         }
79
80         List JavaDoc condSList = UtilXml.childElementList(eca, "condition-service");
81         Iterator JavaDoc sfi = condSList.iterator();
82
83         while (sfi.hasNext()) {
84             conditions.add(new ServiceEcaCondition((Element JavaDoc) sfi.next(), false, true));
85         }
86
87         if (Debug.verboseOn()) Debug.logVerbose("Conditions: " + conditions, module);
88
89         List JavaDoc actList = UtilXml.childElementList(eca, "action");
90         Iterator JavaDoc ai = actList.iterator();
91
92         while (ai.hasNext()) {
93             Element JavaDoc actionElement = (Element JavaDoc) ai.next();
94             actions.add(new ServiceEcaAction(actionElement, eventName));
95         }
96
97         if (Debug.verboseOn()) Debug.logVerbose("Actions: " + actions, module);
98     }
99
100     public void eval(String JavaDoc serviceName, DispatchContext dctx, Map JavaDoc context, Map JavaDoc result, boolean isError, boolean isFailure, Set JavaDoc actionsRun) throws GenericServiceException {
101         if (!enabled) {
102             Debug.logInfo("Service ECA [" + this.serviceName + "] on [" + this.eventName + "] is disabled; not running.", module);
103             return;
104         }
105         if (isFailure && !this.runOnFailure) {
106             return;
107         }
108         if (isError && !this.runOnError) {
109             return;
110         }
111
112         boolean allCondTrue = true;
113         Iterator JavaDoc c = conditions.iterator();
114
115         while (c.hasNext()) {
116             ServiceEcaCondition ec = (ServiceEcaCondition) c.next();
117             if (!ec.eval(serviceName, dctx, context)) {
118                 if (Debug.infoOn()) Debug.logInfo("Got false for condition: " + ec, module);
119                 allCondTrue = false;
120                 break;
121             } else {
122                 if (Debug.verboseOn()) Debug.logVerbose("Got true for condition: " + ec, module);
123             }
124         }
125
126         if (allCondTrue) {
127             Iterator JavaDoc a = actions.iterator();
128             boolean allOkay = true;
129             while (a.hasNext() && allOkay) {
130                 ServiceEcaAction ea = (ServiceEcaAction) a.next();
131                 // in order to enable OR logic without multiple calls to the given service,
132
// only execute a given service name once per service call phase
133
if (!actionsRun.contains(ea.serviceName)) {
134                     if (Debug.infoOn()) Debug.logInfo("Running Service ECA Service: " + ea.serviceName + ", triggered by rule on Service: " + serviceName, module);
135                     if (ea.runAction(serviceName, dctx, context, result)) {
136                         actionsRun.add(ea.serviceName);
137                     } else {
138                         allOkay = false;
139                     }
140                 }
141             }
142         }
143     }
144
145     public void setEnabled(boolean enabled) {
146         this.enabled = enabled;
147     }
148
149     public boolean isEnabled() {
150         return this.enabled;
151     }
152 }
153
Popular Tags