KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ServiceEcaAction.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002 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.HashMap JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.transaction.xa.XAException JavaDoc;
32
33 import org.ofbiz.base.util.UtilValidate;
34 import org.ofbiz.service.DispatchContext;
35 import org.ofbiz.service.GenericServiceException;
36 import org.ofbiz.service.LocalDispatcher;
37 import org.ofbiz.service.ModelService;
38 import org.ofbiz.service.ServiceXaWrapper;
39 import org.ofbiz.service.ServiceUtil;
40 import org.w3c.dom.Element JavaDoc;
41
42 /**
43  * ServiceEcaAction
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 2.0
48  */

49 public class ServiceEcaAction implements java.io.Serializable JavaDoc {
50
51     public static final String JavaDoc module = ServiceEcaAction.class.getName();
52
53     protected String JavaDoc eventName = null;
54     protected String JavaDoc serviceName = null;
55     protected String JavaDoc serviceMode = null;
56     protected String JavaDoc resultMapName = null;
57     protected String JavaDoc runAsUser = null;
58
59     protected boolean resultToContext = true;
60     protected boolean ignoreFailure = false;
61     protected boolean ignoreError = false;
62     protected boolean persist = false;
63
64     protected ServiceEcaAction() {}
65
66     public ServiceEcaAction(Element JavaDoc action, String JavaDoc event) {
67         this.eventName = event;
68         this.serviceName = action.getAttribute("service");
69         this.serviceMode = action.getAttribute("mode");
70         this.runAsUser = action.getAttribute("runAsUser");
71         this.resultMapName = action.getAttribute("result-map-name");
72         // default is true, so anything but false is true
73
this.resultToContext = !"false".equals(action.getAttribute("result-to-context"));
74         this.ignoreFailure = !"false".equals(action.getAttribute("ignore-failure"));
75         this.ignoreError = !"false".equals(action.getAttribute("ignore-error"));
76         this.persist = "true".equals(action.getAttribute("persist"));
77     }
78
79     public boolean runAction(String JavaDoc selfService, DispatchContext dctx, Map JavaDoc context, Map JavaDoc result) throws GenericServiceException {
80         if (serviceName.equals(selfService)) {
81             throw new GenericServiceException("Cannot invoke self on ECA.");
82         }
83
84         // pull out context parameters needed for this service.
85
Map JavaDoc actionContext = dctx.getModelService(serviceName).makeValid(context, ModelService.IN_PARAM);
86
87         // set the userLogin object in the context
88
actionContext.put("userLogin", ServiceUtil.getUserLogin(dctx, actionContext, runAsUser));
89         
90         Map JavaDoc actionResult = null;
91         LocalDispatcher dispatcher = dctx.getDispatcher();
92
93         if (eventName.startsWith("global-")) {
94             // XA resource ECA
95
ServiceXaWrapper xaw = new ServiceXaWrapper(dctx);
96             if (eventName.equals("global-rollback")) {
97                 xaw.setRollbackService(serviceName, context, "async".equals(serviceMode), persist); // using the actual context so we get updates
98
} else if (eventName.equals("global-commit")) {
99                 xaw.setCommitService(serviceName, context, "async".equals(serviceMode), persist); // using the actual context so we get updates
100
}
101             try {
102                 xaw.enlist();
103             } catch (XAException JavaDoc e) {
104                 throw new GenericServiceException("Unable to enlist ServiceXaWrapper with transaction", e);
105             }
106         } else {
107             // standard ECA
108
if (serviceMode.equals("sync")) {
109                 actionResult = dispatcher.runSync(serviceName, actionContext);
110             } else if (serviceMode.equals("async")) {
111                 dispatcher.runAsync(serviceName, actionContext, persist);
112             }
113         }
114
115         // put the results in to the defined map
116
if (resultMapName != null && resultMapName.length() > 0) {
117             Map JavaDoc resultMap = (Map JavaDoc) context.get(resultMapName);
118             if (resultMap == null) {
119                 resultMap = new HashMap JavaDoc();
120             }
121             resultMap.putAll(dctx.getModelService(this.serviceName).makeValid(actionResult, ModelService.OUT_PARAM, false, null));
122             context.put(resultMapName, resultMap);
123         }
124
125         // use the result to update the context fields.
126
if (resultToContext) {
127             context.putAll(dctx.getModelService(this.serviceName).makeValid(actionResult, ModelService.OUT_PARAM, false, null));
128         }
129
130         // if we aren't ignoring errors check it here...
131
boolean success = true;
132         if (actionResult != null) {
133             if (!ignoreFailure) {
134                 if (ModelService.RESPOND_FAIL.equals(actionResult.get(ModelService.RESPONSE_MESSAGE))) {
135                     if (result != null) {
136                         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_FAIL);
137                     }
138                     success = false;
139                 }
140             }
141             if (!ignoreError) {
142                 if (ModelService.RESPOND_ERROR.equals(actionResult.get(ModelService.RESPONSE_MESSAGE))) {
143                     if (result != null) {
144                         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
145                     }
146                     success = false;
147                 }
148             }
149         }
150
151         if (result != null && !success) {
152             String JavaDoc errorMessage = (String JavaDoc) actionResult.get(ModelService.ERROR_MESSAGE);
153             List JavaDoc errorMessageList = (List JavaDoc) actionResult.get(ModelService.ERROR_MESSAGE_LIST);
154             Map JavaDoc errorMessageMap = (Map JavaDoc) actionResult.get(ModelService.ERROR_MESSAGE_MAP);
155
156             // do something with the errorMessage
157
if (UtilValidate.isNotEmpty(errorMessage)) {
158                 if (UtilValidate.isEmpty((String JavaDoc) result.get(ModelService.ERROR_MESSAGE))) {
159                     result.put(ModelService.ERROR_MESSAGE, errorMessage);
160                 } else {
161                     if (errorMessageList == null) errorMessageList = new LinkedList JavaDoc();
162                     errorMessageList.add(0, errorMessage);
163                 }
164             }
165             // do something with the errorMessageList
166
if (errorMessageList != null && errorMessageList.size() > 0) {
167                 List JavaDoc origErrorMessageList = (List JavaDoc) result.get(ModelService.ERROR_MESSAGE_LIST);
168                 if (origErrorMessageList == null) {
169                     result.put(ModelService.ERROR_MESSAGE_LIST, errorMessageList);
170                 } else {
171                     origErrorMessageList.addAll(errorMessageList);
172                 }
173             }
174             // do something with the errorMessageMap
175
if (errorMessageMap != null && errorMessageMap.size() > 0) {
176                 Map JavaDoc origErrorMessageMap = (Map JavaDoc) result.get(ModelService.ERROR_MESSAGE_MAP);
177                 if (origErrorMessageMap == null) {
178                     result.put(ModelService.ERROR_MESSAGE_MAP, errorMessageMap);
179                 } else {
180                     origErrorMessageMap.putAll(errorMessageMap);
181                 }
182             }
183         }
184
185         return success;
186     }
187 }
188
Popular Tags