1 25 package org.ofbiz.entityext.eca; 26 27 import java.util.Map ; 28 29 import org.ofbiz.base.util.Debug; 30 import org.ofbiz.base.util.UtilMisc; 31 import org.ofbiz.base.util.UtilValidate; 32 import org.ofbiz.entity.GenericEntity; 33 import org.ofbiz.entity.GenericEntityException; 34 import org.ofbiz.entity.GenericValue; 35 import org.ofbiz.entity.transaction.TransactionUtil; 36 import org.ofbiz.service.DispatchContext; 37 import org.ofbiz.service.GenericServiceException; 38 import org.ofbiz.service.LocalDispatcher; 39 import org.ofbiz.service.ModelService; 40 import org.ofbiz.service.ServiceUtil; 41 import org.w3c.dom.Element ; 42 43 51 public class EntityEcaAction implements java.io.Serializable { 52 public static final String module = EntityEcaAction.class.getName(); 53 54 protected String serviceName = null; 55 protected String serviceMode = null; 56 protected String runAsUser = null; 57 protected String valueAttr = null; 58 protected boolean resultToValue = true; 59 protected boolean abortOnError = false; 60 protected boolean rollbackOnError = false; 61 protected boolean persist = false; 62 63 protected EntityEcaAction() {} 64 65 public EntityEcaAction(Element action) { 66 this.serviceName = action.getAttribute("service"); 67 this.serviceMode = action.getAttribute("mode"); 68 this.resultToValue = !"false".equals(action.getAttribute("result-to-value")); 70 this.abortOnError = "true".equals(action.getAttribute("abort-on-error")); 72 this.rollbackOnError = "true".equals(action.getAttribute("rollback-on-error")); 73 this.persist = "true".equals(action.getAttribute("persist")); 74 this.runAsUser = action.getAttribute("run-as-user"); 75 this.valueAttr = action.getAttribute("value-attr"); 76 } 77 78 public void runAction(DispatchContext dctx, GenericEntity value) throws GenericEntityException { 79 Map actionResult = null; 80 81 try { 82 Map actionContext = dctx.getModelService(serviceName).makeValid(value, ModelService.IN_PARAM); 84 if (valueAttr != null && valueAttr.length() > 0) { 86 actionContext.put(valueAttr, value); 87 } 88 89 92 GenericValue userLoginToRunAs = null; 94 if (UtilValidate.isNotEmpty(this.runAsUser)) { 95 userLoginToRunAs = dctx.getDelegator().findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", this.runAsUser)); 96 if (userLoginToRunAs != null) { 97 actionContext.put("userLogin", userLoginToRunAs); 98 } 99 } 100 101 LocalDispatcher dispatcher = dctx.getDispatcher(); 102 if ("sync".equals(this.serviceMode)) { 103 actionResult = dispatcher.runSync(this.serviceName, actionContext); 104 if (ServiceUtil.isError(actionResult)) { 105 throw new GenericServiceException("Error running Entity ECA action service: " + ServiceUtil.getErrorMessage(actionResult)); 106 } 107 } else if ("async".equals(this.serviceMode)) { 108 dispatcher.runAsync(serviceName, actionContext, persist); 109 } 110 } catch (GenericServiceException e) { 111 if (rollbackOnError) { 113 String errMsg = "Entity ECA action service failed and rollback-on-error is true, so setting rollback only."; 114 Debug.logError(errMsg, module); 115 TransactionUtil.setRollbackOnly(errMsg, e); 116 } 117 118 if (this.abortOnError) { 119 throw new EntityEcaException("Error running Entity ECA action service: " + e.toString(), e); 120 } else { 121 Debug.logError(e, "Error running Entity ECA action service", module); 122 } 123 } 124 125 if (resultToValue) { 127 value.setNonPKFields(actionResult); 128 } 129 } 130 } 131 | Popular Tags |