Skip to content
This repository has been archived by the owner on May 9, 2020. It is now read-only.

Commit

Permalink
Small enhancements to Entity ECA classes: improve thread safety, elim…
Browse files Browse the repository at this point in the history
…inate null checks on fields that are never null, add some accessor methods, small code cleanups.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1447112 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
adrian-crum committed Feb 18, 2013
1 parent 1facfbc commit 99bb547
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 37 deletions.
Expand Up @@ -22,7 +22,6 @@

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.GenericEntity;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
Expand All @@ -38,19 +37,17 @@
* EntityEcaAction
*/
@SuppressWarnings("serial")
public class EntityEcaAction implements java.io.Serializable {
public final class EntityEcaAction implements java.io.Serializable {
public static final String module = EntityEcaAction.class.getName();

protected String serviceName = null;
protected String serviceMode = null;
protected String runAsUser = null;
protected String valueAttr = null;
protected boolean resultToValue = true;
protected boolean abortOnError = false;
protected boolean rollbackOnError = false;
protected boolean persist = false;

protected EntityEcaAction() {}
private final String serviceName;
private final String serviceMode;
private final String runAsUser;
private final String valueAttr;
private final boolean resultToValue;
private final boolean abortOnError;
private final boolean rollbackOnError;
private final boolean persist;

public EntityEcaAction(Element action) {
this.serviceName = action.getAttribute("service");
Expand All @@ -65,14 +62,16 @@ public EntityEcaAction(Element action) {
this.valueAttr = action.getAttribute("value-attr");
}

public void runAction(DispatchContext dctx, Map<String, ? extends Object> context, GenericEntity newValue) throws GenericEntityException {
Map<String, Object> actionResult = null;
public String getServiceName() {
return this.serviceName;
}

public void runAction(DispatchContext dctx, Map<String, ? extends Object> context, GenericEntity newValue) throws GenericEntityException {
try {
// pull out context parameters needed for this service.
Map<String, Object> actionContext = dctx.getModelService(serviceName).makeValid(context, ModelService.IN_PARAM);
// if value-attr is specified, insert the value object in that attr name
if (UtilValidate.isNotEmpty(valueAttr)) {
if (!valueAttr.isEmpty()) {
actionContext.put(valueAttr, newValue);
}

Expand All @@ -81,7 +80,7 @@ public void runAction(DispatchContext dctx, Map<String, ? extends Object> contex

// setup the run-as-user
GenericValue userLoginToRunAs = null;
if (UtilValidate.isNotEmpty(this.runAsUser)) {
if (!this.runAsUser.isEmpty()) {
userLoginToRunAs = dctx.getDelegator().findOne("UserLogin", UtilMisc.toMap("userLoginId", this.runAsUser), true);
if (userLoginToRunAs != null) {
actionContext.put("userLogin", userLoginToRunAs);
Expand All @@ -90,10 +89,14 @@ public void runAction(DispatchContext dctx, Map<String, ? extends Object> contex

LocalDispatcher dispatcher = dctx.getDispatcher();
if ("sync".equals(this.serviceMode)) {
actionResult = dispatcher.runSync(this.serviceName, actionContext);
Map<String, Object> actionResult = dispatcher.runSync(this.serviceName, actionContext);
if (ServiceUtil.isError(actionResult)) {
throw new GenericServiceException("Error running Entity ECA action service: " + ServiceUtil.getErrorMessage(actionResult));
}
// use the result to update the context fields.
if (resultToValue) {
newValue.setNonPKFields(actionResult);
}
} else if ("async".equals(this.serviceMode)) {
dispatcher.runAsync(serviceName, actionContext, persist);
}
Expand All @@ -111,10 +114,5 @@ public void runAction(DispatchContext dctx, Map<String, ? extends Object> contex
Debug.logError(e, "Error running Entity ECA action service", module);
}
}

// use the result to update the context fields.
if (resultToValue) {
newValue.setNonPKFields(actionResult);
}
}
}
Expand Up @@ -33,36 +33,27 @@
* EntityEcaCondition
*/
@SuppressWarnings("serial")
public class EntityEcaCondition implements java.io.Serializable {
public final class EntityEcaCondition implements java.io.Serializable {

public static final String module = EntityEcaCondition.class.getName();

protected String lhsValueName, rhsValueName;
protected String operator;
protected String compareType;
protected String format;
protected boolean constant = false;

protected EntityEcaCondition() {}
private final String lhsValueName, rhsValueName;
private final String operator;
private final String compareType;
private final String format;
private final boolean constant;

public EntityEcaCondition(Element condition, boolean constant) {
this.lhsValueName = condition.getAttribute("field-name");

this.constant = constant;
if (constant) {
this.rhsValueName = condition.getAttribute("value");
} else {
this.rhsValueName = condition.getAttribute("to-field-name");
}

this.operator = condition.getAttribute("operator");
this.compareType = condition.getAttribute("type");
this.format = condition.getAttribute("format");

if (lhsValueName == null)
lhsValueName = "";
if (rhsValueName == null)
rhsValueName = "";
}

public boolean eval(DispatchContext dctx, GenericEntity value) throws GenericEntityException {
Expand Down Expand Up @@ -100,6 +91,21 @@ public boolean eval(DispatchContext dctx, GenericEntity value) throws GenericEnt
}
}

public String getLValue() {
return this.lhsValueName;
}

public String getRValue() {
if (constant) {
return "\"".concat(this.rhsValueName).concat("\"");
}
return this.rhsValueName;
}

public String getOperator() {
return this.operator;
}

@Override
public String toString() {
StringBuilder buf = new StringBuilder();
Expand Down
Expand Up @@ -104,4 +104,15 @@ private Object format(String s, Map<String, ? extends Object> c) {
Debug.logWarning("Format function not found [" + format + "] return string unchanged - " + s, module);
return s;
}

public String getFieldName() {
return this.fieldName;
}

public String getRValue() {
if (!this.value.isEmpty()) {
return "\"".concat(this.value).concat("\"");
}
return this.envName;
}
}

0 comments on commit 99bb547

Please sign in to comment.