Commit f8d6f1e2c44bd26189ab121258e800c9643e90d8
1 parent
0723e457
【代码评审】Bpm:触发器的实现
Showing
4 changed files
with
24 additions
and
14 deletions
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/controller/admin/definition/vo/model/simple/BpmSimpleModelNodeVO.java
| @@ -158,6 +158,7 @@ public class BpmSimpleModelNodeVO { | @@ -158,6 +158,7 @@ public class BpmSimpleModelNodeVO { | ||
| 158 | @Schema(description = "值", example = "xxx") | 158 | @Schema(description = "值", example = "xxx") |
| 159 | @NotEmpty(message = "值不能为空") | 159 | @NotEmpty(message = "值不能为空") |
| 160 | private String value; | 160 | private String value; |
| 161 | + | ||
| 161 | } | 162 | } |
| 162 | 163 | ||
| 163 | @Schema(description = "审批节点拒绝处理策略") | 164 | @Schema(description = "审批节点拒绝处理策略") |
| @@ -362,12 +363,14 @@ public class BpmSimpleModelNodeVO { | @@ -362,12 +363,14 @@ public class BpmSimpleModelNodeVO { | ||
| 362 | private List<HttpRequestParam> body; | 363 | private List<HttpRequestParam> body; |
| 363 | 364 | ||
| 364 | /** | 365 | /** |
| 365 | - * 请求返回处理设置。 用于修改流程表单值 | ||
| 366 | - * key: 表示要修改的流程表单字段 Id. | ||
| 367 | - * value: 接口返回的字段名 | 366 | + * 请求返回处理设置,用于修改流程表单值 |
| 367 | + * | ||
| 368 | + * key:表示要修改的流程表单字段名(name) | ||
| 369 | + * value:接口返回的字段名 | ||
| 368 | */ | 370 | */ |
| 369 | @Schema(description = "请求返回处理设置", example = "[]") | 371 | @Schema(description = "请求返回处理设置", example = "[]") |
| 370 | - private List<KeyValue<String,String>> response; | 372 | + private List<KeyValue<String, String>> response; |
| 373 | + | ||
| 371 | } | 374 | } |
| 372 | 375 | ||
| 373 | } | 376 | } |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmProcessInstanceService.java
| @@ -160,9 +160,9 @@ public interface BpmProcessInstanceService { | @@ -160,9 +160,9 @@ public interface BpmProcessInstanceService { | ||
| 160 | /** | 160 | /** |
| 161 | * 更新 ProcessInstance 的变量 | 161 | * 更新 ProcessInstance 的变量 |
| 162 | * | 162 | * |
| 163 | - * @param processInstanceId 流程编号 | 163 | + * @param id 流程编号 |
| 164 | * @param variables 流程变量 | 164 | * @param variables 流程变量 |
| 165 | */ | 165 | */ |
| 166 | - void updateProcessInstanceVariables(String processInstanceId, Map<String, Object> variables); | 166 | + void updateProcessInstanceVariables(String id, Map<String, Object> variables); |
| 167 | 167 | ||
| 168 | } | 168 | } |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmProcessInstanceServiceImpl.java
| @@ -758,7 +758,8 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService | @@ -758,7 +758,8 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService | ||
| 758 | } | 758 | } |
| 759 | 759 | ||
| 760 | @Override | 760 | @Override |
| 761 | - public void updateProcessInstanceVariables(String processInstanceId, Map<String, Object> variables) { | ||
| 762 | - runtimeService.setVariables(processInstanceId, variables); | 761 | + public void updateProcessInstanceVariables(String id, Map<String, Object> variables) { |
| 762 | + runtimeService.setVariables(id, variables); | ||
| 763 | } | 763 | } |
| 764 | + | ||
| 764 | } | 765 | } |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/trigger/BpmHttpRequestTrigger.java
| @@ -76,7 +76,10 @@ public class BpmHttpRequestTrigger implements BpmTrigger { | @@ -76,7 +76,10 @@ public class BpmHttpRequestTrigger implements BpmTrigger { | ||
| 76 | ResponseEntity<String> responseEntity = restTemplate.exchange(setting.getUrl(), HttpMethod.POST, | 76 | ResponseEntity<String> responseEntity = restTemplate.exchange(setting.getUrl(), HttpMethod.POST, |
| 77 | requestEntity, String.class); | 77 | requestEntity, String.class); |
| 78 | log.info("[execute][HTTP 触发器,请求头:{},请求体:{},响应结果:{}]", headers, body, responseEntity); | 78 | log.info("[execute][HTTP 触发器,请求头:{},请求体:{},响应结果:{}]", headers, body, responseEntity); |
| 79 | + | ||
| 80 | + // TODO @jason:建议把请求和失败,放在两个 try catch 里处理。 | ||
| 79 | // 4. 处理请求返回 | 81 | // 4. 处理请求返回 |
| 82 | + // TODO @jason:返回结果,要不统一用 CommonResult,符合这个规范。这样,就可以验证 code 为 0 了。必须符合这个规范~~ | ||
| 80 | if (CollUtil.isNotEmpty(setting.getResponse()) && responseEntity.getStatusCode().is2xxSuccessful() | 83 | if (CollUtil.isNotEmpty(setting.getResponse()) && responseEntity.getStatusCode().is2xxSuccessful() |
| 81 | && StrUtil.isNotEmpty(responseEntity.getBody())) { | 84 | && StrUtil.isNotEmpty(responseEntity.getBody())) { |
| 82 | // 4.1 获取需要更新的流程变量 | 85 | // 4.1 获取需要更新的流程变量 |
| @@ -86,15 +89,15 @@ public class BpmHttpRequestTrigger implements BpmTrigger { | @@ -86,15 +89,15 @@ public class BpmHttpRequestTrigger implements BpmTrigger { | ||
| 86 | processInstanceService.updateProcessInstanceVariables(processInstanceId, updateVariables); | 89 | processInstanceService.updateProcessInstanceVariables(processInstanceId, updateVariables); |
| 87 | } | 90 | } |
| 88 | } | 91 | } |
| 89 | - | ||
| 90 | } catch (RestClientException e) { | 92 | } catch (RestClientException e) { |
| 91 | log.error("[execute][HTTP 触发器,请求头:{},请求体:{},请求出错:{}]", headers, body, e.getMessage()); | 93 | log.error("[execute][HTTP 触发器,请求头:{},请求体:{},请求出错:{}]", headers, body, e.getMessage()); |
| 92 | } | 94 | } |
| 93 | } | 95 | } |
| 94 | 96 | ||
| 95 | - | ||
| 96 | /** | 97 | /** |
| 97 | - * 从请求返回值获取需要更新的流程变量。优先从 data 字段获取,如果 data 字段不存在,从根节点获取。 | 98 | + * 从请求返回值获取需要更新的流程变量 |
| 99 | + * | ||
| 100 | + * 优先从 data 字段获取,如果 data 字段不存在,从根节点获取 | ||
| 98 | * | 101 | * |
| 99 | * @param responseBody 请求返回报文体 | 102 | * @param responseBody 请求返回报文体 |
| 100 | * @param responseSettings 返回设置 | 103 | * @param responseSettings 返回设置 |
| @@ -103,18 +106,21 @@ public class BpmHttpRequestTrigger implements BpmTrigger { | @@ -103,18 +106,21 @@ public class BpmHttpRequestTrigger implements BpmTrigger { | ||
| 103 | private Map<String, Object> getNeedUpdatedVariablesFromResponse(String responseBody, | 106 | private Map<String, Object> getNeedUpdatedVariablesFromResponse(String responseBody, |
| 104 | List<KeyValue<String, String>> responseSettings) { | 107 | List<KeyValue<String, String>> responseSettings) { |
| 105 | Map<String, Object> updateVariables = new HashMap<>(); | 108 | Map<String, Object> updateVariables = new HashMap<>(); |
| 109 | + // TODO @jason:这里 if return 更简洁一点; | ||
| 110 | + // TODO @jason:JSONUtil => JsonUtils,尽量包一层 | ||
| 106 | if (JSONUtil.isTypeJSONObject(responseBody)) { | 111 | if (JSONUtil.isTypeJSONObject(responseBody)) { |
| 107 | JSONObject dataObj = null; | 112 | JSONObject dataObj = null; |
| 108 | if (JSONUtil.parseObj(responseBody).getObj(PARSE_RESPONSE_FIELD) instanceof JSONObject) { | 113 | if (JSONUtil.parseObj(responseBody).getObj(PARSE_RESPONSE_FIELD) instanceof JSONObject) { |
| 109 | dataObj = (JSONObject) JSONUtil.parseObj(responseBody).getObj(PARSE_RESPONSE_FIELD); | 114 | dataObj = (JSONObject) JSONUtil.parseObj(responseBody).getObj(PARSE_RESPONSE_FIELD); |
| 110 | } | 115 | } |
| 111 | JSONObject updateObj = dataObj == null ? JSONUtil.parseObj(responseBody) : dataObj; | 116 | JSONObject updateObj = dataObj == null ? JSONUtil.parseObj(responseBody) : dataObj; |
| 112 | - responseSettings.forEach(respSetting -> { | ||
| 113 | - if (StrUtil.isNotEmpty(respSetting.getKey()) && updateObj.containsKey(respSetting.getValue())) { | ||
| 114 | - updateVariables.put(respSetting.getKey(), updateObj.get(respSetting.getValue())); | 117 | + responseSettings.forEach(responseSetting -> { |
| 118 | + if (StrUtil.isNotEmpty(responseSetting.getKey()) && updateObj.containsKey(responseSetting.getValue())) { | ||
| 119 | + updateVariables.put(responseSetting.getKey(), updateObj.get(responseSetting.getValue())); | ||
| 115 | } | 120 | } |
| 116 | }); | 121 | }); |
| 117 | } | 122 | } |
| 118 | return updateVariables; | 123 | return updateVariables; |
| 119 | } | 124 | } |
| 125 | + | ||
| 120 | } | 126 | } |