KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entityext > eca > EntityEcaRule


1 /*
2  * $Id: EntityEcaRule.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-2004 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.entityext.eca;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.UtilXml;
34 import org.ofbiz.entity.GenericEntity;
35 import org.ofbiz.entity.GenericEntityException;
36 import org.ofbiz.service.DispatchContext;
37 import org.w3c.dom.Element JavaDoc;
38
39 /**
40  * EntityEcaRule
41  *
42  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
43  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
44  * @version $Rev: 5462 $
45  * @since 2.1
46  */

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