1 25 package org.ofbiz.service.eca; 26 27 import java.util.HashMap ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Map ; 31 import javax.transaction.xa.XAException ; 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 ; 41 42 49 public class ServiceEcaAction implements java.io.Serializable { 50 51 public static final String module = ServiceEcaAction.class.getName(); 52 53 protected String eventName = null; 54 protected String serviceName = null; 55 protected String serviceMode = null; 56 protected String resultMapName = null; 57 protected String 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 action, String 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 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 selfService, DispatchContext dctx, Map context, Map result) throws GenericServiceException { 80 if (serviceName.equals(selfService)) { 81 throw new GenericServiceException("Cannot invoke self on ECA."); 82 } 83 84 Map actionContext = dctx.getModelService(serviceName).makeValid(context, ModelService.IN_PARAM); 86 87 actionContext.put("userLogin", ServiceUtil.getUserLogin(dctx, actionContext, runAsUser)); 89 90 Map actionResult = null; 91 LocalDispatcher dispatcher = dctx.getDispatcher(); 92 93 if (eventName.startsWith("global-")) { 94 ServiceXaWrapper xaw = new ServiceXaWrapper(dctx); 96 if (eventName.equals("global-rollback")) { 97 xaw.setRollbackService(serviceName, context, "async".equals(serviceMode), persist); } else if (eventName.equals("global-commit")) { 99 xaw.setCommitService(serviceName, context, "async".equals(serviceMode), persist); } 101 try { 102 xaw.enlist(); 103 } catch (XAException e) { 104 throw new GenericServiceException("Unable to enlist ServiceXaWrapper with transaction", e); 105 } 106 } else { 107 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 if (resultMapName != null && resultMapName.length() > 0) { 117 Map resultMap = (Map ) context.get(resultMapName); 118 if (resultMap == null) { 119 resultMap = new HashMap (); 120 } 121 resultMap.putAll(dctx.getModelService(this.serviceName).makeValid(actionResult, ModelService.OUT_PARAM, false, null)); 122 context.put(resultMapName, resultMap); 123 } 124 125 if (resultToContext) { 127 context.putAll(dctx.getModelService(this.serviceName).makeValid(actionResult, ModelService.OUT_PARAM, false, null)); 128 } 129 130 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 errorMessage = (String ) actionResult.get(ModelService.ERROR_MESSAGE); 153 List errorMessageList = (List ) actionResult.get(ModelService.ERROR_MESSAGE_LIST); 154 Map errorMessageMap = (Map ) actionResult.get(ModelService.ERROR_MESSAGE_MAP); 155 156 if (UtilValidate.isNotEmpty(errorMessage)) { 158 if (UtilValidate.isEmpty((String ) result.get(ModelService.ERROR_MESSAGE))) { 159 result.put(ModelService.ERROR_MESSAGE, errorMessage); 160 } else { 161 if (errorMessageList == null) errorMessageList = new LinkedList (); 162 errorMessageList.add(0, errorMessage); 163 } 164 } 165 if (errorMessageList != null && errorMessageList.size() > 0) { 167 List origErrorMessageList = (List ) 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 if (errorMessageMap != null && errorMessageMap.size() > 0) { 176 Map origErrorMessageMap = (Map ) 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 |