xiejun
2023-08-12 a19d26e88360c9760b2286bac4dfb1710fd2fa21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.vci.ubcs.flow.engine.envent;
 
import com.vci.ubcs.flow.core.dto.FlowStatusDTO;
import com.vci.ubcs.flow.engine.constant.FlowEngineConstant;
import com.vci.ubcs.flow.engine.utils.FlowableUtils;
import com.vci.ubcs.starter.exception.VciBaseException;
import com.vci.ubcs.starter.web.util.LangBaseUtil;
import com.vci.ubcs.starter.web.util.VciBaseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.HistoryService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;
import org.springblade.core.jwt.JwtUtil;
import org.springblade.core.launch.constant.TokenConstant;
import org.springblade.core.tool.utils.WebUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
 
import java.util.List;
import java.util.Map;
 
@Slf4j
@Component
public class FlowExecutionEndListener implements ExecutionListener, ApplicationContextAware {
 
    /**
     * 远程调用地址。切记:名称要与流程中定义的一样
     */
    private FixedValue remoteMethod;
 
    /**
     * 状态值。切记:名称要与流程中定义的一样
     */
    private FixedValue statusValue;
 
    private static ApplicationContext applicationContext;
 
    @Override
    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
        applicationContext = arg0;
    }
 
    @Override
    public void notify(DelegateExecution execution) {
        Map variables = execution.getVariables();
        String restURL = remoteMethod.getExpressionText();
        String status = statusValue.getExpressionText();
        //获取业务数据信息
        List<String> oids = (List<String>) variables.get(FlowEngineConstant.OIDS);
        String btmType = (String) variables.get(FlowEngineConstant.BTMTYPE);
 
        variables.put(FlowEngineConstant.REMOTE_METHOD,restURL);
        variables.put(FlowEngineConstant.STATUS_VALUE,status);
 
        if(CollectionUtils.isEmpty(oids)){
            throw new VciBaseException("执行状态修改事件时,业务数据oid为空!");
        }
        if(StringUtils.isEmpty(btmType)){
            throw new VciBaseException("执行状态修改事件时,业务类型btmType为空!");
        }
        if(StringUtils.isEmpty(restURL)){
            throw new VciBaseException("执行状态修改事件时,远程调用地址为空!");
        }
        if(StringUtils.isEmpty(status)){
            throw new VciBaseException("执行状态修改事件时,状态为空!");
        }
 
        HistoryService historyService = applicationContext.getBean(HistoryService.class);
        TaskService taskService = applicationContext.getBean(TaskService.class);
 
        FlowStatusDTO flowStatusDTO = new FlowStatusDTO();
        flowStatusDTO.setBtmType(btmType);
        flowStatusDTO.setOids(oids);
        flowStatusDTO.setVariableMap(variables);
        flowStatusDTO.setTaskHisVOList(FlowableUtils.listTaskHistory(execution.getProcessInstanceId(),historyService,taskService));
 
        String token = JwtUtil.getToken(WebUtil.getRequest().getHeader(TokenConstant.HEADER));
 
        HttpComponentsClientHttpRequestFactory requestFactory=new HttpComponentsClientHttpRequestFactory();
        requestFactory.setReadTimeout(300000);
        requestFactory.setConnectionRequestTimeout(300000);
        requestFactory.setConnectTimeout(300000);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        HttpHeaders headers = new HttpHeaders();
        headers.add(TokenConstant.HEADER,token);
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = new HttpEntity<>(flowStatusDTO,headers);
        Map<String, Object> result = null;
        try {
            result = restTemplate.postForObject(restURL, httpEntity, Map.class);
        } catch (HttpClientErrorException e) {
            throw new VciBaseException(LangBaseUtil.getErrorMsg(e),new String[]{},e);
        }catch (Throwable e){
            throw new VciBaseException(LangBaseUtil.getErrorMsg(e),new String[]{},e);
        }
        if(result == null){
            throw new VciBaseException("业务事件时候没有返回值,不确定是否执行成功");
        }
        if(CollectionUtils.isEmpty(result) && !(Boolean) result.get("success")){
            throw new VciBaseException((String) result.get("message"));
        }
    }
}