001: /*
002: * $Id: ServiceEcaAction.java,v 1.2 2003/11/13 21:13:45 ajzeneski Exp $
003: *
004: * Copyright (c) 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.HashMap;
028: import java.util.LinkedList;
029: import java.util.List;
030: import java.util.Map;
031:
032: import org.ofbiz.service.*;
033: import org.ofbiz.base.util.UtilValidate;
034: import org.w3c.dom.Element;
035:
036: import javax.transaction.xa.XAException;
037:
038: /**
039: * ServiceEcaAction
040: *
041: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
042: * @version $Revision: 1.2 $
043: * @since 2.0
044: */
045: public class ServiceEcaAction {
046:
047: protected String serviceName;
048: protected String serviceMode;
049: protected String resultMapName;
050: protected boolean resultToContext;
051: protected boolean ignoreError;
052: protected boolean persist;
053:
054: protected ServiceEcaAction() {
055: }
056:
057: public ServiceEcaAction(Element action) {
058: this .serviceName = action.getAttribute("service");
059: this .serviceMode = action.getAttribute("mode");
060: this .resultMapName = action.getAttribute("result-map-name");
061: // default is true, so anything but false is true
062: this .resultToContext = !"false".equals(action
063: .getAttribute("result-to-context"));
064: this .ignoreError = !"false".equals(action
065: .getAttribute("ignore-error"));
066: this .persist = "true".equals(action.getAttribute("persist"));
067: }
068:
069: public void runAction(String selfService, DispatchContext dctx,
070: Map context, Map result) throws GenericServiceException {
071: if (this .serviceName.equals(selfService)) {
072: throw new GenericServiceException(
073: "Cannot invoke self on ECA.");
074: }
075:
076: // pull out context parameters needed for this service.
077: Map actionContext = dctx.getModelService(serviceName)
078: .makeValid(context, ModelService.IN_PARAM);
079: Map actionResult = null;
080: LocalDispatcher dispatcher = dctx.getDispatcher();
081:
082: if (serviceMode.equals("sync")) {
083: actionResult = dispatcher.runSync(this .serviceName,
084: actionContext);
085: } else if (serviceMode.equals("async")) {
086: dispatcher.runAsync(serviceName, actionContext, persist);
087: } else if (serviceMode.equals("_rollback")
088: || serviceMode.equals("_commit")) {
089: ServiceXaWrapper xaw = new ServiceXaWrapper(dctx);
090: if (serviceMode.equals("_rollback")) {
091: xaw.setRollbackService(this .serviceName, context); // using the actual context so we get updates
092: } else if (serviceMode.equals("_commit")) {
093: xaw.setCommitService(this .serviceName, context); // using the actual context so we get updates
094: }
095: try {
096: xaw.enlist();
097: } catch (XAException e) {
098: throw new GenericServiceException(
099: "Unable to enlist ServiceXaWrapper with transaction",
100: e);
101: }
102: }
103:
104: // put the results in to the defined map
105: if (resultMapName != null && resultMapName.length() > 0) {
106: Map resultMap = (Map) context.get(resultMapName);
107: if (resultMap == null) {
108: resultMap = new HashMap();
109: }
110: resultMap.putAll(dctx.getModelService(this .serviceName)
111: .makeValid(actionResult, ModelService.OUT_PARAM,
112: false));
113: context.put(resultMapName, resultMap);
114: }
115:
116: // use the result to update the context fields.
117: if (resultToContext) {
118: context.putAll(dctx.getModelService(this .serviceName)
119: .makeValid(actionResult, ModelService.OUT_PARAM,
120: false));
121: }
122:
123: // if we aren't ignoring errors check it here...
124: if (!ignoreError && result != null) {
125: if (ModelService.RESPOND_ERROR.equals(actionResult
126: .get(ModelService.RESPONSE_MESSAGE))) {
127: result.put(ModelService.RESPONSE_MESSAGE,
128: ModelService.RESPOND_ERROR);
129: String errorMessage = (String) actionResult
130: .get(ModelService.ERROR_MESSAGE);
131: List errorMessageList = (List) actionResult
132: .get(ModelService.ERROR_MESSAGE_LIST);
133: Map errorMessageMap = (Map) actionResult
134: .get(ModelService.ERROR_MESSAGE_MAP);
135:
136: // do something with the errorMessage
137: if (UtilValidate.isNotEmpty(errorMessage)) {
138: if (UtilValidate.isEmpty((String) result
139: .get(ModelService.ERROR_MESSAGE))) {
140: result.put(ModelService.ERROR_MESSAGE,
141: errorMessage);
142: } else {
143: if (errorMessageList == null)
144: errorMessageList = new LinkedList();
145: errorMessageList.add(0, errorMessage);
146: }
147: }
148: // do something with the errorMessageList
149: if (errorMessageList != null
150: && errorMessageList.size() > 0) {
151: List origErrorMessageList = (List) result
152: .get(ModelService.ERROR_MESSAGE_LIST);
153: if (origErrorMessageList == null) {
154: result.put(ModelService.ERROR_MESSAGE_LIST,
155: errorMessageList);
156: } else {
157: origErrorMessageList.addAll(errorMessageList);
158: }
159: }
160: // do something with the errorMessageMap
161: if (errorMessageMap != null
162: && errorMessageMap.size() > 0) {
163: Map origErrorMessageMap = (Map) result
164: .get(ModelService.ERROR_MESSAGE_MAP);
165: if (origErrorMessageMap == null) {
166: result.put(ModelService.ERROR_MESSAGE_MAP,
167: errorMessageMap);
168: } else {
169: origErrorMessageMap.putAll(errorMessageMap);
170: }
171: }
172: }
173: }
174: }
175: }
|