001: /*
002: * $Id: ServiceEcaRule.java,v 1.3 2003/11/13 21:13:45 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.service.eca;
026:
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.Set;
032:
033: import org.ofbiz.service.DispatchContext;
034: import org.ofbiz.service.GenericServiceException;
035: import org.ofbiz.base.util.Debug;
036: import org.ofbiz.base.util.UtilXml;
037: import org.w3c.dom.Element;
038:
039: /**
040: * ServiceEcaRule
041: *
042: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
043: * @version $Revision: 1.3 $
044: * @since 2.0
045: */
046: public class ServiceEcaRule {
047:
048: public static final String module = ServiceEcaRule.class.getName();
049:
050: protected String serviceName = null;
051: protected String eventName = null;
052: protected boolean runOnError = false;
053: protected List conditions = new LinkedList();
054: protected List actions = new LinkedList();
055:
056: protected ServiceEcaRule() {
057: }
058:
059: public ServiceEcaRule(Element eca) {
060: this .serviceName = eca.getAttribute("service");
061: this .eventName = eca.getAttribute("event");
062: this .runOnError = "true".equals(eca
063: .getAttribute("run-on-error"));
064:
065: List condList = UtilXml.childElementList(eca, "condition");
066: Iterator ci = condList.iterator();
067:
068: while (ci.hasNext()) {
069: conditions.add(new ServiceEcaCondition((Element) ci.next(),
070: true));
071: }
072:
073: List condFList = UtilXml.childElementList(eca,
074: "condition-field");
075: Iterator cfi = condFList.iterator();
076:
077: while (cfi.hasNext()) {
078: conditions.add(new ServiceEcaCondition(
079: (Element) cfi.next(), false));
080: }
081:
082: if (Debug.verboseOn())
083: Debug.logVerbose("Conditions: " + conditions, module);
084:
085: List actList = UtilXml.childElementList(eca, "action");
086: Iterator ai = actList.iterator();
087:
088: // since it is the action class which handle mode; we will set the mode for
089: // the special case "global-rollback" and "global-commit" events here.
090: while (ai.hasNext()) {
091: Element actionElement = (Element) ai.next();
092: if ("global-rollback".equalsIgnoreCase(eventName)) {
093: actionElement.setAttribute("mode", "_rollback");
094: } else if ("global-commit".equalsIgnoreCase(eventName)) {
095: actionElement.setAttribute("mode", "_commit");
096: }
097: actions.add(new ServiceEcaAction(actionElement));
098: }
099:
100: if (Debug.verboseOn())
101: Debug.logVerbose("Actions: " + actions, module);
102: }
103:
104: public void eval(String serviceName, DispatchContext dctx,
105: Map context, Map result, boolean isError, Set actionsRun)
106: throws GenericServiceException {
107: if (isError && !this .runOnError) {
108: return;
109: }
110:
111: boolean allCondTrue = true;
112: Iterator c = conditions.iterator();
113:
114: while (c.hasNext()) {
115: ServiceEcaCondition ec = (ServiceEcaCondition) c.next();
116: if (!ec.eval(serviceName, dctx, context)) {
117: if (Debug.verboseOn())
118: Debug.logVerbose("Got false for condition: " + ec,
119: module);
120: allCondTrue = false;
121: break;
122: } else {
123: if (Debug.verboseOn())
124: Debug.logVerbose("Got true for condition: " + ec,
125: module);
126: }
127: }
128:
129: if (allCondTrue) {
130: Iterator a = actions.iterator();
131: while (a.hasNext()) {
132: ServiceEcaAction ea = (ServiceEcaAction) a.next();
133: // in order to enable OR logic without multiple calls to the given service,
134: // only execute a given service name once per service call phase
135: if (!actionsRun.contains(ea.serviceName)) {
136: if (Debug.infoOn())
137: Debug.logInfo("Running ECA Service: "
138: + ea.serviceName
139: + ", triggered by rule on Service: "
140: + serviceName, module);
141: ea.runAction(serviceName, dctx, context, result);
142: actionsRun.add(ea.serviceName);
143: }
144: }
145: }
146: }
147: }
|