|
@@ -0,0 +1,290 @@
|
|
|
+package com.activiti6.controller.image;
|
|
|
+
|
|
|
+
|
|
|
+import com.activiti6.controller.ModelerController;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
+import org.activiti.bpmn.model.BpmnModel;
|
|
|
+import org.activiti.bpmn.model.FlowNode;
|
|
|
+import org.activiti.bpmn.model.SequenceFlow;
|
|
|
+import org.activiti.engine.HistoryService;
|
|
|
+import org.activiti.engine.ProcessEngine;
|
|
|
+import org.activiti.engine.RepositoryService;
|
|
|
+import org.activiti.engine.RuntimeService;
|
|
|
+import org.activiti.engine.history.HistoricActivityInstance;
|
|
|
+import org.activiti.engine.history.HistoricProcessInstance;
|
|
|
+import org.activiti.engine.impl.RepositoryServiceImpl;
|
|
|
+import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
|
|
|
+import org.activiti.engine.repository.ProcessDefinition;
|
|
|
+import org.activiti.engine.repository.ProcessDefinitionQuery;
|
|
|
+import org.activiti.engine.runtime.ProcessInstance;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 请假业务Controller
|
|
|
+ *
|
|
|
+ * @author Xianlu Tech
|
|
|
+ * @date 2019-10-11
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/activiti")
|
|
|
+public class LeaveNewController {
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(LeaveNewController.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private HistoryService historyService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RepositoryService repositoryService;
|
|
|
+
|
|
|
+ protected ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RuntimeService runtimeService;
|
|
|
+
|
|
|
+ @RequestMapping(value = "/process-instance/{processInstanceId}/highlights")
|
|
|
+ public ObjectNode readResource(@PathVariable String processInstanceId)
|
|
|
+ throws Exception {
|
|
|
+
|
|
|
+ return showImage(processInstanceId);
|
|
|
+ }
|
|
|
+
|
|
|
+ private ObjectNode showImage(String processInstanceId) throws IOException {
|
|
|
+
|
|
|
+ ObjectNode responseJSON = objectMapper.createObjectNode();
|
|
|
+
|
|
|
+ responseJSON.put("processInstanceId", processInstanceId);
|
|
|
+
|
|
|
+ ArrayNode activitiesArray = objectMapper.createArrayNode();
|
|
|
+ ArrayNode flowsArray = objectMapper.createArrayNode();
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
|
|
|
+ .processInstanceId(processInstanceId).singleResult();
|
|
|
+ ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(historicProcessInstance.getProcessDefinitionId());
|
|
|
+ responseJSON.put("processDefinitionId", historicProcessInstance.getProcessDefinitionId());
|
|
|
+
|
|
|
+ List<HistoricActivityInstance> historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery()
|
|
|
+ .processInstanceId(processInstanceId).orderByHistoricActivityInstanceId().asc().list();
|
|
|
+
|
|
|
+ List<String> highLightedActivities = new ArrayList<String>();
|
|
|
+
|
|
|
+ for (HistoricActivityInstance activityInstance : historicActivityInstanceList) {
|
|
|
+ highLightedActivities.add(activityInstance.getActivityId());
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> flowIds = new ArrayList<>();
|
|
|
+
|
|
|
+ BpmnModel bpmnModel = repositoryService.getBpmnModel(historicProcessInstance.getProcessDefinitionId());
|
|
|
+
|
|
|
+ List<String> highLightedFlows = executedFlowIdList(bpmnModel, processDefinition, historicActivityInstanceList);
|
|
|
+
|
|
|
+ for (String activityId : highLightedActivities) {
|
|
|
+ activitiesArray.add(activityId);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (String flow : highLightedFlows) {
|
|
|
+ flowsArray.add(flow);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ responseJSON.put("activities", activitiesArray);
|
|
|
+ responseJSON.put("flows", flowsArray);
|
|
|
+
|
|
|
+ return responseJSON;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void image(String processInstanceId) {
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
|
|
|
+ .processInstanceId(processInstanceId).singleResult();
|
|
|
+
|
|
|
+ if (historicProcessInstance == null) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
|
|
|
+ .getDeployedProcessDefinition(historicProcessInstance.getProcessDefinitionId());
|
|
|
+
|
|
|
+
|
|
|
+ List<HistoricActivityInstance> historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery()
|
|
|
+ .processInstanceId(processInstanceId).orderByHistoricActivityInstanceId().asc().list();
|
|
|
+
|
|
|
+ List<String> executedActivityIdList = new ArrayList<String>();
|
|
|
+
|
|
|
+ for (HistoricActivityInstance activityInstance : historicActivityInstanceList) {
|
|
|
+ executedActivityIdList.add(activityInstance.getActivityId());
|
|
|
+ }
|
|
|
+ BpmnModel bpmnModel = repositoryService.getBpmnModel(historicProcessInstance.getProcessDefinitionId());
|
|
|
+
|
|
|
+
|
|
|
+ List<String> flowIds = new ArrayList<>();
|
|
|
+
|
|
|
+ flowIds = executedFlowIdList(bpmnModel, processDefinition, historicActivityInstanceList);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ logger.error("【异常】-获取流程图失败!" + e.getMessage());
|
|
|
+ throw new RuntimeException("获取流程图失败!" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static List<String> executedFlowIdList(BpmnModel bpmnModel, ProcessDefinitionEntity processDefinitionEntity,
|
|
|
+ List<HistoricActivityInstance> historicActivityInstanceList) {
|
|
|
+
|
|
|
+ List<String> executedFlowIdList = new ArrayList<>();
|
|
|
+ Set<String> ids = new HashSet<>();
|
|
|
+ Map<String, SequenceFlow> flowHashMap = new HashMap<>();
|
|
|
+ List<String> activityIds = historicActivityInstanceList.stream().map(X -> X.getActivityId()).collect(Collectors.toList());
|
|
|
+ for (int i = 0; i < historicActivityInstanceList.size(); i++) {
|
|
|
+ HistoricActivityInstance hai = historicActivityInstanceList.get(i);
|
|
|
+ String activityId = hai.getActivityId();
|
|
|
+ FlowNode flowNode = (FlowNode) bpmnModel.getFlowElement(activityId);
|
|
|
+ List<SequenceFlow> sequenceFlows = flowNode.getOutgoingFlows();
|
|
|
+ for (SequenceFlow sequenceFlow : sequenceFlows) {
|
|
|
+ String sourceRef = sequenceFlow.getSourceRef();
|
|
|
+ String targetRef = sequenceFlow.getTargetRef();
|
|
|
+ flowHashMap.put(targetRef, sequenceFlow);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (String activityId : activityIds) {
|
|
|
+ for (String s : flowHashMap.keySet()) {
|
|
|
+ if (s.contains(activityId)) {
|
|
|
+ SequenceFlow sequenceFlow = flowHashMap.get(s);
|
|
|
+ ids.add(sequenceFlow.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ executedFlowIdList.addAll(ids);
|
|
|
+ return executedFlowIdList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 获取流程图像,已执行节点和流程线高亮显示
|
|
|
+ */
|
|
|
+ public void getActivitiProccessImage(String processInstanceId, HttpServletResponse response) {
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
|
|
|
+ .processInstanceId(processInstanceId).singleResult();
|
|
|
+
|
|
|
+ if (historicProcessInstance == null) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
|
|
|
+ .getDeployedProcessDefinition(historicProcessInstance.getProcessDefinitionId());
|
|
|
+
|
|
|
+
|
|
|
+ List<HistoricActivityInstance> historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery()
|
|
|
+ .processInstanceId(processInstanceId).orderByHistoricActivityInstanceId().asc().list();
|
|
|
+
|
|
|
+
|
|
|
+ List<String> executedActivityIdList = new ArrayList<String>();
|
|
|
+ int index = 1;
|
|
|
+
|
|
|
+ for (HistoricActivityInstance activityInstance : historicActivityInstanceList) {
|
|
|
+ executedActivityIdList.add(activityInstance.getActivityId());
|
|
|
+
|
|
|
+
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+
|
|
|
+ BpmnModel bpmnModel = repositoryService.getBpmnModel(historicProcessInstance.getProcessDefinitionId());
|
|
|
+
|
|
|
+
|
|
|
+ List<String> flowIds = new ArrayList<String>();
|
|
|
+
|
|
|
+ flowIds = getHighLightedFlows(bpmnModel, processDefinition, historicActivityInstanceList);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> getHighLightedFlows(BpmnModel bpmnModel, ProcessDefinitionEntity processDefinitionEntity, List<HistoricActivityInstance> historicActivityInstances) {
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ List<String> highFlows = new ArrayList<String>();
|
|
|
+
|
|
|
+ for (int i = 0; i < historicActivityInstances.size() - 1; i++) {
|
|
|
+
|
|
|
+
|
|
|
+ FlowNode activityImpl = (FlowNode) bpmnModel.getMainProcess().getFlowElement(historicActivityInstances.get(i).getActivityId());
|
|
|
+
|
|
|
+
|
|
|
+ List<FlowNode> sameStartTimeNodes = new ArrayList<FlowNode>();
|
|
|
+ FlowNode sameActivityImpl1 = null;
|
|
|
+
|
|
|
+ HistoricActivityInstance activityImpl_ = historicActivityInstances.get(i);
|
|
|
+ HistoricActivityInstance activityImp2_;
|
|
|
+
|
|
|
+ for (int k = i + 1; k <= historicActivityInstances.size() - 1; k++) {
|
|
|
+ activityImp2_ = historicActivityInstances.get(k);
|
|
|
+
|
|
|
+ if (activityImpl_.getActivityType().equals("userTask") && activityImp2_.getActivityType().equals("userTask") &&
|
|
|
+ df.format(activityImpl_.getStartTime()).equals(df.format(activityImp2_.getStartTime())))
|
|
|
+ {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ sameActivityImpl1 = (FlowNode) bpmnModel.getMainProcess().getFlowElement(historicActivityInstances.get(k).getActivityId());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ sameStartTimeNodes.add(sameActivityImpl1);
|
|
|
+ for (int j = i + 1; j < historicActivityInstances.size() - 1; j++) {
|
|
|
+ HistoricActivityInstance activityImpl1 = historicActivityInstances.get(j);
|
|
|
+ HistoricActivityInstance activityImpl2 = historicActivityInstances.get(j + 1);
|
|
|
+
|
|
|
+ if (df.format(activityImpl1.getStartTime()).equals(df.format(activityImpl2.getStartTime()))) {
|
|
|
+ FlowNode sameActivityImpl2 = (FlowNode) bpmnModel.getMainProcess().getFlowElement(activityImpl2.getActivityId());
|
|
|
+ sameStartTimeNodes.add(sameActivityImpl2);
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<SequenceFlow> pvmTransitions = activityImpl.getOutgoingFlows();
|
|
|
+
|
|
|
+ for (SequenceFlow pvmTransition : pvmTransitions) {
|
|
|
+ FlowNode pvmActivityImpl = (FlowNode) bpmnModel.getMainProcess().getFlowElement(pvmTransition.getTargetRef());
|
|
|
+ if (sameStartTimeNodes.contains(pvmActivityImpl)) {
|
|
|
+ highFlows.add(pvmTransition.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return highFlows;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|