001: /*
002: * $Id: EntityEcaAction.java,v 1.2 2003/10/26 11:07:05 jonesde Exp $
003: *
004: * Copyright (c) 2002-2003 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.entityext.eca;
026:
027: import java.util.Map;
028:
029: import org.ofbiz.base.util.Debug;
030: import org.ofbiz.entity.GenericEntity;
031: import org.ofbiz.entity.GenericEntityException;
032: import org.ofbiz.entity.transaction.TransactionUtil;
033: import org.ofbiz.service.DispatchContext;
034: import org.ofbiz.service.GenericServiceException;
035: import org.ofbiz.service.LocalDispatcher;
036: import org.ofbiz.service.ModelService;
037: import org.w3c.dom.Element;
038:
039: /**
040: * EntityEcaAction
041: *
042: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
043: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
044: * @version $Revision: 1.2 $
045: * @since 2.1
046: */
047: public class EntityEcaAction {
048: public static final String module = EntityEcaAction.class.getName();
049:
050: protected String serviceName;
051: protected String serviceMode;
052: protected boolean resultToValue;
053: protected boolean abortOnError;
054: protected boolean rollbackOnError;
055: protected boolean persist;
056: protected String valueAttr;
057:
058: protected EntityEcaAction() {
059: }
060:
061: public EntityEcaAction(Element action) {
062: this .serviceName = action.getAttribute("service");
063: this .serviceMode = action.getAttribute("mode");
064: // default is true, so anything but false is true
065: this .resultToValue = !"false".equals(action
066: .getAttribute("result-to-value"));
067: // default is false, so anything but true is false
068: this .abortOnError = "true".equals(action
069: .getAttribute("abort-on-error"));
070: this .rollbackOnError = "true".equals(action
071: .getAttribute("rollback-on-error"));
072: this .persist = "true".equals(action.getAttribute("persist"));
073: this .valueAttr = action.getAttribute("value-attr");
074: }
075:
076: public void runAction(DispatchContext dctx, GenericEntity value)
077: throws GenericEntityException {
078: Map actionResult = null;
079:
080: try {
081: // pull out context parameters needed for this service.
082: Map actionContext = dctx.getModelService(serviceName)
083: .makeValid(value, ModelService.IN_PARAM);
084: // if value-attr is specified, insert the value object in that attr name
085: if (valueAttr != null && valueAttr.length() > 0) {
086: actionContext.put(valueAttr, value);
087: }
088:
089: Debug.logInfo("Running Entity ECA action service "
090: + this .serviceName + " triggered by entity: "
091: + value.getEntityName(), module);
092: //Debug.logInfo("Running Entity ECA action service " + this.serviceName + "; value=" + value + "; actionContext=" + actionContext, module);
093:
094: LocalDispatcher dispatcher = dctx.getDispatcher();
095: if ("sync".equals(this .serviceMode)) {
096: actionResult = dispatcher.runSync(this .serviceName,
097: actionContext);
098: } else if ("async".equals(this .serviceMode)) {
099: dispatcher
100: .runAsync(serviceName, actionContext, persist);
101: }
102: } catch (GenericServiceException e) {
103: if (this .abortOnError) {
104: throw new EntityEcaException(
105: "Error running Entity ECA action service", e);
106: } else {
107: Debug.logError(e,
108: "Error running Entity ECA action service",
109: module);
110: }
111: }
112:
113: // use the result to update the context fields.
114: if (resultToValue) {
115: value.setNonPKFields(actionResult);
116: }
117:
118: // check abortOnError and rollbackOnError
119: if (rollbackOnError) {
120: Debug
121: .logError(
122: "Entity ECA action service failed and rollback-on-error is true, so setting rollback only.",
123: module);
124: TransactionUtil.setRollbackOnly();
125: }
126: }
127: }
|