修改action模板方法,解决潜在的线程不安全问题

main
youHong.ai 2022-12-13 17:38:15 +08:00
parent 8b55a4bd64
commit 4170596ba1
4 changed files with 331 additions and 19 deletions

View File

@ -19,6 +19,7 @@ import java.util.*;
*
* @author EBU7-dev-1 aiyh
*/
@Deprecated
public abstract class CusBaseAction implements Action {
@ -36,13 +37,13 @@ public abstract class CusBaseAction implements Action {
* <h2></h2>
*/
private void initHandleMethod() {
// 提交
// 提交
actionHandleMethod.put(ActionRunType.SUBMIT, this::doSubmit);
// 退回
// 退回
actionHandleMethod.put(ActionRunType.REJECT, this::doReject);
// 撤回
// 撤回
actionHandleMethod.put(ActionRunType.WITHDRAW, this::doWithdraw);
// 强制收回
// 强制收回
actionHandleMethod.put(ActionRunType.DRAW_BACK, this::doDrawBack);
}
@ -55,7 +56,7 @@ public abstract class CusBaseAction implements Action {
String requestId = requestInfo.getRequestid();
User user = requestInfo.getRequestManager().getUser();
int workflowId = requestManager.getWorkflowid();
// 操作类型 submit - 提交 reject - 退回 等
// 操作类型 submit - 提交 reject - 退回 等
String src = requestManager.getSrc();
if ("".equals(billTable)) {
WorkflowComInfo workflowComInfo = new WorkflowComInfo();
@ -68,13 +69,13 @@ public abstract class CusBaseAction implements Action {
if (StringUtils.isEmpty(src)) {
src = "submit";
}
// 初始化默认的流程处理方法
// 初始化默认的流程处理方法
initHandleMethod();
// 提供自定义注册处理方法
// 提供自定义注册处理方法
registerHandler(actionHandleMethod);
// 获取流程对应的处理方法
// 获取流程对应的处理方法
CusBaseActionHandleFunction cusBaseActionHandleFunction = actionHandleMethod.get(src);
// 默认没有直接成功不做拦截
// 默认没有直接成功不做拦截
if (null == cusBaseActionHandleFunction) {
return Action.SUCCESS;
}
@ -124,8 +125,7 @@ public abstract class CusBaseAction implements Action {
/**
* <h2>action </h2>
* <p>
* log
* Util.actionFailException(requestManager,"error msg"); action
* log Util.actionFailException(requestManager,"error msg"); action
* </p>
*
* @param requestId ID
@ -142,8 +142,7 @@ public abstract class CusBaseAction implements Action {
/**
* <h2>action 退</h2>
* <p>
* log
* Util.actionFailException(requestManager,"error msg"); action
* log Util.actionFailException(requestManager,"error msg"); action
* </p>
*
* @param requestId ID
@ -159,8 +158,7 @@ public abstract class CusBaseAction implements Action {
/**
* <h2>action </h2>
* <p>
* log
* Util.actionFailException(requestManager,"error msg"); action
* log Util.actionFailException(requestManager,"error msg"); action
* </p>
*
* @param requestId ID
@ -177,8 +175,7 @@ public abstract class CusBaseAction implements Action {
/**
* <h2>action </h2>
* <p>
* log
* Util.actionFailException(requestManager,"error msg"); action
* log Util.actionFailException(requestManager,"error msg"); action
* </p>
*
* @param requestId ID
@ -199,7 +196,7 @@ public abstract class CusBaseAction implements Action {
*/
@Deprecated
protected Map<String, String> getMainTableValue() {
// 获取主表数据
// 获取主表数据
Property[] propertyArr = globalRequestInfo.getMainTableInfo().getProperty();
return getStringMap(propertyArr);
}
@ -210,7 +207,7 @@ public abstract class CusBaseAction implements Action {
* @return
*/
protected Map<String, String> getMainTableValue(RequestInfo requestInfo) {
// 获取主表数据
// 获取主表数据
Property[] propertyArr = requestInfo.getMainTableInfo().getProperty();
return getStringMap(propertyArr);
}

View File

@ -10,6 +10,7 @@ import weaver.workflow.request.RequestManager;
* <p>create: 2022-07-23 18:05</p>
*/
@Deprecated
@FunctionalInterface
public interface CusBaseActionHandleFunction {
/**

View File

@ -0,0 +1,288 @@
package aiyh.utils.action;
import aiyh.utils.Util;
import aiyh.utils.excention.CustomerException;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;
import weaver.workflow.request.RequestManager;
import weaver.workflow.workflow.WorkflowBillComInfo;
import weaver.workflow.workflow.WorkflowComInfo;
import java.util.*;
/**
* <h1>action</h1>
*
* @author EBU7-dev-1 aiyh
*/
public abstract class SafeCusBaseAction implements Action {
/**
*
*/
protected final Logger log = Util.getLogger();
private final Map<String, SafeCusBaseActionHandleFunction> actionHandleMethod = new HashMap<>();
/**
* <h2></h2>
*/
private void initHandleMethod() {
// 提交
actionHandleMethod.put(ActionRunType.SUBMIT, this::doSubmit);
// 退回
actionHandleMethod.put(ActionRunType.REJECT, this::doReject);
// 撤回
actionHandleMethod.put(ActionRunType.WITHDRAW, this::doWithdraw);
// 强制收回
actionHandleMethod.put(ActionRunType.DRAW_BACK, this::doDrawBack);
}
@Override
public final String execute(RequestInfo requestInfo) {
RequestManager requestManager = requestInfo.getRequestManager();
String billTable = requestManager.getBillTableName();
String requestId = requestInfo.getRequestid();
User user = requestInfo.getRequestManager().getUser();
int workflowId = requestManager.getWorkflowid();
// 操作类型 submit - 提交 reject - 退回 等
String src = requestManager.getSrc();
if ("".equals(billTable)) {
WorkflowComInfo workflowComInfo = new WorkflowComInfo();
String formId = workflowComInfo.getFormId(String.valueOf(workflowId));
WorkflowBillComInfo workflowBillComInfo = new WorkflowBillComInfo();
billTable = workflowBillComInfo.getTablename(formId);
}
try {
Util.verifyRequiredField(this);
if (StringUtils.isEmpty(src)) {
src = "submit";
}
// 初始化默认的流程处理方法
initHandleMethod();
// 提供自定义注册处理方法
registerHandler(actionHandleMethod);
// 获取流程对应的处理方法
SafeCusBaseActionHandleFunction cusBaseActionHandleFunction = actionHandleMethod.get(src);
// 默认没有直接成功不做拦截
if (null == cusBaseActionHandleFunction) {
return Action.SUCCESS;
}
cusBaseActionHandleFunction.handle(requestId, billTable, workflowId, user, requestInfo);
} catch (CustomerException e) {
if (e.getCode() != null && e.getCode() == 500) {
Util.actionFail(requestManager, e.getMessage());
return Action.FAILURE_AND_CONTINUE;
}
if (this.exceptionCallback(e, requestManager)) {
return Action.FAILURE_AND_CONTINUE;
}
} catch (Exception e) {
if (this.exceptionCallback(e, requestManager)) {
return Action.FAILURE_AND_CONTINUE;
}
}
return Action.SUCCESS;
}
/**
* <h2></h2>
*
* @param e
* @param requestManager requestManager
* @return actiontrue- false-
*/
public boolean exceptionCallback(Exception e, RequestManager requestManager) {
e.printStackTrace();
log.error(Util.logStr("getDataId action fail, exception message is [{}], error stack trace msg is: \n{}",
e.getMessage(), Util.getErrString(e)));
Util.actionFail(requestManager, e.getMessage());
return true;
}
/**
* <h2></h2>
*
* @param actionHandleMethod map
*/
public void registerHandler(Map<String, SafeCusBaseActionHandleFunction> actionHandleMethod) {
}
/**
* <h2>action </h2>
* <p>
* log Util.actionFailException(requestManager,"error msg"); action
* </p>
*
* @param requestId ID
* @param billTable
* @param workflowId ID
* @param user
* @param requestInfo
*/
public abstract void doSubmit(String requestId, String billTable, int workflowId,
User user, RequestInfo requestInfo);
/**
* <h2>action 退</h2>
* <p>
* log Util.actionFailException(requestManager,"error msg"); action
* </p>
*
* @param requestId ID
* @param billTable
* @param workflowId ID
* @param user
* @param requestInfo
*/
public void doReject(String requestId, String billTable, int workflowId,
User user, RequestInfo requestInfo) {
}
/**
* <h2>action </h2>
* <p>
* log Util.actionFailException(requestManager,"error msg"); action
* </p>
*
* @param requestId ID
* @param billTable
* @param workflowId ID
* @param user
* @param requestInfo
*/
public void doWithdraw(String requestId, String billTable, int workflowId,
User user, RequestInfo requestInfo) {
}
/**
* <h2>action </h2>
* <p>
* log Util.actionFailException(requestManager,"error msg"); action
* </p>
*
* @param requestId ID
* @param billTable
* @param workflowId ID
* @param user
* @param requestInfo
*/
public void doDrawBack(String requestId, String billTable, int workflowId,
User user, RequestInfo requestInfo) {
}
/**
* <h2></h2>
*
* @return
*/
protected Map<String, String> getMainTableValue(RequestInfo requestInfo) {
// 获取主表数据
Property[] propertyArr = requestInfo.getMainTableInfo().getProperty();
return getStringMap(propertyArr);
}
@NotNull
private Map<String, String> getStringMap(Property[] propertyArr) {
if (null == propertyArr) {
return Collections.emptyMap();
}
Map<String, String> mainTable = new HashMap<>((int) Math.ceil(propertyArr.length * 1.4));
for (Property property : propertyArr) {
String fieldName = property.getName();
String value = property.getValue();
mainTable.put(fieldName, value);
}
return mainTable;
}
/**
* <h2></h2>
*
* @return
*/
protected Map<String, List<Map<String, String>>> getDetailTableValue(RequestInfo requestInfo) {
DetailTable[] detailTableArr = requestInfo.getDetailTableInfo().getDetailTable();
return getListMap(detailTableArr);
}
@NotNull
private Map<String, List<Map<String, String>>> getListMap(DetailTable[] detailTableArr) {
Map<String, List<Map<String, String>>> detailDataList = new HashMap<>((int) Math.ceil(detailTableArr.length * 1.4));
for (DetailTable detailTable : detailTableArr) {
List<Map<String, String>> detailData = getDetailValue(detailTable);
detailDataList.put(detailTable.getId(), detailData);
}
return detailDataList;
}
/**
* <h2></h2>
*
* @param detailNo
* @return
*/
protected List<Map<String, String>> getDetailTableValueByDetailNo(int detailNo, RequestInfo requestInfo) {
DetailTable detailTable = requestInfo.getDetailTableInfo().getDetailTable(detailNo);
return getDetailValue(detailTable);
}
/**
* <h2></h2>
*
* @param detailTable
* @return
*/
@NotNull
private List<Map<String, String>> getDetailValue(DetailTable detailTable) {
Row[] rowArr = detailTable.getRow();
List<Map<String, String>> detailData = new ArrayList<>(rowArr.length);
for (Row row : rowArr) {
Cell[] cellArr = row.getCell();
Map<String, String> rowData = new HashMap<>((int) Math.ceil(cellArr.length * (1 + 0.4)));
rowData.put("id", row.getId());
for (Cell cell : cellArr) {
String fieldName = cell.getName();
String value = cell.getValue();
rowData.put(fieldName, value);
}
detailData.add(rowData);
}
return detailData;
}
public static final class ActionRunType {
/**
* 退
*/
public static final String REJECT = "reject";
/**
*
*/
public static final String WITHDRAW = "withdraw";
/**
*
*/
public static final String DRAW_BACK = "drawBack";
/**
*
*/
public static final String SUBMIT = "submit";
}
}

View File

@ -0,0 +1,26 @@
package aiyh.utils.action;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;
/**
* <h1></h1>
*
* @author EBU7-dev-1 aiyh
* <p>create: 2022-07-23 18:05</p>
*/
@FunctionalInterface
public interface SafeCusBaseActionHandleFunction {
/**
* <h2>action</h2>
*
* @param requestId ID
* @param billTable
* @param workflowId ID
* @param user
* @param requestInfo
*/
void handle(String requestId, String billTable, int workflowId,
User user, RequestInfo requestInfo);
}