【代码评审】BPM:流程前后置通知
This commit is contained in:
parent
6f2e538927
commit
4364ef09c5
|
@ -82,9 +82,11 @@ public class BpmModelMetaInfoVO {
|
|||
@Schema(description = "摘要设置", example = "{}")
|
||||
private SummarySetting summarySetting;
|
||||
|
||||
// TODO @lesan:processBeforeTriggerSetting;要不叫这个?主要考虑,notify 留给后续的站内信、短信、邮件这种 notify 通知哈。
|
||||
@Schema(description = "流程前置通知设置", example = "{}")
|
||||
private HttpRequestSetting PreProcessNotifySetting;
|
||||
|
||||
// TODO @lesan:processAfterTriggerSetting
|
||||
@Schema(description = "流程后置通知设置", example = "{}")
|
||||
private HttpRequestSetting PostProcessNotifySetting;
|
||||
|
||||
|
|
|
@ -182,23 +182,22 @@ public class BpmProcessDefinitionInfoDO extends BaseDO {
|
|||
*/
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
private BpmModelMetaInfoVO.TitleSetting titleSetting;
|
||||
|
||||
/**
|
||||
* 摘要设置
|
||||
*/
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
private BpmModelMetaInfoVO.SummarySetting summarySetting;
|
||||
|
||||
// TODO @lesan:processBeforeTriggerSetting;要不叫这个?主要考虑,notify 留给后续的站内信、短信、邮件这种 notify 通知哈。
|
||||
/**
|
||||
* 流程前置通知设置
|
||||
*/
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
@TableField(typeHandler = JacksonTypeHandler.class, exist = false) // TODO @芋艿:临时注释 exist,因为要合并 master-jdk17
|
||||
private BpmModelMetaInfoVO.HttpRequestSetting PreProcessNotifySetting;
|
||||
|
||||
/**
|
||||
* 流程后置通知设置
|
||||
*/
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
@TableField(typeHandler = JacksonTypeHandler.class, exist = false) // TODO @芋艿:临时注释 exist,因为要合并 master-jdk17
|
||||
private BpmModelMetaInfoVO.HttpRequestSetting PostProcessNotifySetting;
|
||||
|
||||
}
|
||||
|
|
|
@ -37,51 +37,52 @@ public class BpmHttpRequestUtils {
|
|||
|
||||
public static void executeBpmHttpRequest(ProcessInstance processInstance,
|
||||
String url,
|
||||
List<BpmSimpleModelNodeVO.HttpRequestParam> headerParam,
|
||||
List<BpmSimpleModelNodeVO.HttpRequestParam> bodyParam,
|
||||
List<BpmSimpleModelNodeVO.HttpRequestParam> headerParams,
|
||||
List<BpmSimpleModelNodeVO.HttpRequestParam> bodyParams,
|
||||
Boolean handleResponse,
|
||||
List<KeyValue<String, String>> response,
|
||||
// TODO @lesan:RestTemplate 直接通过 springUtil 获取好咧;
|
||||
RestTemplate restTemplate,
|
||||
// TODO @lesan:processInstanceService 直接通过 springUtil 获取好咧;
|
||||
BpmProcessInstanceService processInstanceService) {
|
||||
|
||||
// 1.1 设置请求头
|
||||
MultiValueMap<String, String> headers = BpmHttpRequestUtils.buildHttpHeaders(processInstance, headerParam);
|
||||
MultiValueMap<String, String> headers = buildHttpHeaders(processInstance, headerParams);
|
||||
// 1.2 设置请求体
|
||||
MultiValueMap<String, String> body = BpmHttpRequestUtils.buildHttpBody(processInstance, bodyParam);
|
||||
MultiValueMap<String, String> body = buildHttpBody(processInstance, bodyParams);
|
||||
|
||||
// 2. 发起请求
|
||||
ResponseEntity<String> responseEntity = BpmHttpRequestUtils.sendHttpRequest(url, headers, body, restTemplate);
|
||||
ResponseEntity<String> responseEntity = sendHttpRequest(url, headers, body, restTemplate);
|
||||
|
||||
// 3. 处理返回
|
||||
// TODO @lesan:可以用 if return,让括号小点
|
||||
if (Boolean.TRUE.equals(handleResponse)) {
|
||||
// 3.1 判断是否需要解析返回值
|
||||
if (responseEntity == null || StrUtil.isEmpty(responseEntity.getBody())
|
||||
if (responseEntity == null
|
||||
|| StrUtil.isEmpty(responseEntity.getBody())
|
||||
|| !responseEntity.getStatusCode().is2xxSuccessful()
|
||||
|| CollUtil.isEmpty(response)) {
|
||||
return;
|
||||
}
|
||||
// 3.2 解析返回值, 返回值必须符合 CommonResult 规范。
|
||||
CommonResult<Map<String, Object>> respResult = JsonUtils.parseObjectQuietly(
|
||||
responseEntity.getBody(), new TypeReference<>() {
|
||||
});
|
||||
CommonResult<Map<String, Object>> respResult = JsonUtils.parseObjectQuietly(responseEntity.getBody(),
|
||||
new TypeReference<>() {});
|
||||
if (respResult == null || !respResult.isSuccess()) {
|
||||
return;
|
||||
}
|
||||
// 3.3 获取需要更新的流程变量
|
||||
Map<String, Object> updateVariables = BpmHttpRequestUtils.getNeedUpdatedVariablesFromResponse(respResult.getData(), response);
|
||||
Map<String, Object> updateVariables = getNeedUpdatedVariablesFromResponse(respResult.getData(), response);
|
||||
// 3.4 更新流程变量
|
||||
if (CollUtil.isNotEmpty(updateVariables)) {
|
||||
processInstanceService.updateProcessInstanceVariables(processInstance.getId(), updateVariables);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static ResponseEntity<String> sendHttpRequest(String url,
|
||||
MultiValueMap<String, String> headers,
|
||||
MultiValueMap<String, String> body,
|
||||
RestTemplate restTemplate) {
|
||||
// 3. 发起请求
|
||||
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(body, headers);
|
||||
ResponseEntity<String> responseEntity;
|
||||
try {
|
||||
|
@ -95,7 +96,7 @@ public class BpmHttpRequestUtils {
|
|||
}
|
||||
|
||||
public static MultiValueMap<String, String> buildHttpHeaders(ProcessInstance processInstance,
|
||||
List<BpmSimpleModelNodeVO.HttpRequestParam> headerSettings) {
|
||||
List<BpmSimpleModelNodeVO.HttpRequestParam> headerSettings) {
|
||||
Map<String, Object> processVariables = processInstance.getProcessVariables();
|
||||
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
||||
headers.add(HEADER_TENANT_ID, processInstance.getTenantId());
|
||||
|
@ -104,7 +105,7 @@ public class BpmHttpRequestUtils {
|
|||
}
|
||||
|
||||
public static MultiValueMap<String, String> buildHttpBody(ProcessInstance processInstance,
|
||||
List<BpmSimpleModelNodeVO.HttpRequestParam> bodySettings) {
|
||||
List<BpmSimpleModelNodeVO.HttpRequestParam> bodySettings) {
|
||||
Map<String, Object> processVariables = processInstance.getProcessVariables();
|
||||
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
|
||||
addHttpRequestParam(body, bodySettings, processVariables);
|
||||
|
@ -120,7 +121,7 @@ public class BpmHttpRequestUtils {
|
|||
* @return 需要更新的流程变量
|
||||
*/
|
||||
public static Map<String, Object> getNeedUpdatedVariablesFromResponse(Map<String, Object> result,
|
||||
List<KeyValue<String, String>> responseSettings) {
|
||||
List<KeyValue<String, String>> responseSettings) {
|
||||
Map<String, Object> updateVariables = new HashMap<>();
|
||||
if (CollUtil.isEmpty(result)) {
|
||||
return updateVariables;
|
||||
|
|
|
@ -935,10 +935,10 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
|
|||
// 流程前置通知
|
||||
BpmProcessDefinitionInfoDO processDefinitionInfo = processDefinitionService.
|
||||
getProcessDefinitionInfo(instance.getProcessDefinitionId());
|
||||
// TODO @lesan:if return 哈。减少括号。
|
||||
if (ObjUtil.isNotNull(processDefinitionInfo) &&
|
||||
ObjUtil.isNotNull(processDefinitionInfo.getPreProcessNotifySetting())) {
|
||||
BpmModelMetaInfoVO.HttpRequestSetting setting = processDefinitionInfo.getPreProcessNotifySetting();
|
||||
|
||||
BpmHttpRequestUtils.executeBpmHttpRequest(instance,
|
||||
setting.getUrl(),
|
||||
setting.getHeader(),
|
||||
|
@ -949,4 +949,5 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -46,22 +46,14 @@ public class BpmUserTaskListener implements TaskListener {
|
|||
|
||||
// 2. 发起请求
|
||||
// TODO @芋艿:哪些默认参数,后续再调研下;感觉可以搞个 task 字段,把整个 delegateTask 放进去;
|
||||
listenerHandler.getBody()
|
||||
.add(new BpmSimpleModelNodeVO.HttpRequestParam().setKey("processInstanceId")
|
||||
.setType(BpmHttpRequestParamTypeEnum.FIXED_VALUE.getType())
|
||||
.setValue(delegateTask.getProcessInstanceId()));
|
||||
listenerHandler.getBody()
|
||||
.add(new BpmSimpleModelNodeVO.HttpRequestParam().setKey("assignee")
|
||||
.setType(BpmHttpRequestParamTypeEnum.FIXED_VALUE.getType())
|
||||
.setValue(delegateTask.getAssignee()));
|
||||
listenerHandler.getBody()
|
||||
.add(new BpmSimpleModelNodeVO.HttpRequestParam().setKey("taskDefinitionKey")
|
||||
.setType(BpmHttpRequestParamTypeEnum.FIXED_VALUE.getType())
|
||||
.setValue(delegateTask.getTaskDefinitionKey()));
|
||||
listenerHandler.getBody()
|
||||
.add(new BpmSimpleModelNodeVO.HttpRequestParam().setKey("taskId")
|
||||
.setType(BpmHttpRequestParamTypeEnum.FIXED_VALUE.getType())
|
||||
.setValue(delegateTask.getId()));
|
||||
listenerHandler.getBody().add(new BpmSimpleModelNodeVO.HttpRequestParam().setKey("processInstanceId")
|
||||
.setType(BpmHttpRequestParamTypeEnum.FIXED_VALUE.getType()).setValue(delegateTask.getProcessInstanceId()));
|
||||
listenerHandler.getBody().add(new BpmSimpleModelNodeVO.HttpRequestParam().setKey("assignee")
|
||||
.setType(BpmHttpRequestParamTypeEnum.FIXED_VALUE.getType()).setValue(delegateTask.getAssignee()));
|
||||
listenerHandler.getBody().add(new BpmSimpleModelNodeVO.HttpRequestParam().setKey("taskDefinitionKey")
|
||||
.setType(BpmHttpRequestParamTypeEnum.FIXED_VALUE.getType()).setValue(delegateTask.getTaskDefinitionKey()));
|
||||
listenerHandler.getBody().add(new BpmSimpleModelNodeVO.HttpRequestParam().setKey("taskId")
|
||||
.setType(BpmHttpRequestParamTypeEnum.FIXED_VALUE.getType()).setValue(delegateTask.getId()));
|
||||
BpmHttpRequestUtils.executeBpmHttpRequest(processInstance,
|
||||
listenerHandler.getPath(),
|
||||
listenerHandler.getHeader(),
|
||||
|
|
|
@ -41,13 +41,12 @@ public class BpmHttpCallbackTrigger extends BpmAbstractHttpRequestTrigger {
|
|||
log.error("[execute][流程({}) HTTP 回调触发器配置为空]", processInstanceId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 发起请求
|
||||
ProcessInstance processInstance = processInstanceService.getProcessInstance(processInstanceId);
|
||||
// 重要:回调请求 taskDefineKey 需要传给被调用方,用于回调执行
|
||||
setting.getBody().add(new BpmSimpleModelNodeVO.HttpRequestParam()
|
||||
.setKey("taskDefineKey")
|
||||
.setType(BpmHttpRequestParamTypeEnum.FIXED_VALUE.getType())
|
||||
.setValue(setting.getCallbackTaskDefineKey()));
|
||||
.setKey("taskDefineKey") // 重要:回调请求 taskDefineKey 需要传给被调用方,用于回调执行
|
||||
.setType(BpmHttpRequestParamTypeEnum.FIXED_VALUE.getType()).setValue(setting.getCallbackTaskDefineKey()));
|
||||
BpmHttpRequestUtils.executeBpmHttpRequest(processInstance,
|
||||
setting.getUrl(),
|
||||
setting.getHeader(),
|
||||
|
@ -56,4 +55,5 @@ public class BpmHttpCallbackTrigger extends BpmAbstractHttpRequestTrigger {
|
|||
restTemplate,
|
||||
processInstanceService);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -39,6 +39,7 @@ public class BpmSyncHttpRequestTrigger extends BpmAbstractHttpRequestTrigger {
|
|||
log.error("[execute][流程({}) HTTP 触发器请求配置为空]", processInstanceId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 发起请求
|
||||
ProcessInstance processInstance = processInstanceService.getProcessInstance(processInstanceId);
|
||||
BpmHttpRequestUtils.executeBpmHttpRequest(processInstance,
|
||||
|
|
Loading…
Reference in New Issue