1.实战
image.png首先我们把任务和线条都根据编号写好,这样我们测试的时候也会很方便的看到数据,然后我写下思路:
1.我们需要获取到所有的历史信息包含:当前人完成的任务信息,当前实例完成的任务信息,该实例所有的节点信息,待办信息
2.根据分类分别把完成的线条找到,完成的任务找到,待办任务找到,当前登录人完成任务找到
下面是编码:
//高亮显示历史
@GetMapping(path = "/findHighLine")
public AjaxResponse findHighLine(String processInstanceId,@AuthenticationPrincipal UserInfo userInfo) {
try {
if(GlobalConfig.Test){
securityUtil.logInAs("bajie");
}
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
//读取BPMN
BpmnModel bpmnModel = repositoryService.getBpmnModel(historicProcessInstance.getProcessDefinitionId());
Process process = bpmnModel.getProcesses().get(0);
Collection<FlowElement> elements = process.getFlowElements();
Map<String,String> map = new HashMap<>();
for(FlowElement element:elements){
if(element instanceof SequenceFlow){
SequenceFlow sequenceFlow = (SequenceFlow)element;
String sourceRef = sequenceFlow.getSourceRef();
String targetRef = sequenceFlow.getTargetRef();
map.put(sourceRef+targetRef,sequenceFlow.getId());
}
}
List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId).list();
Set<String> set = new HashSet<>();
for(HistoricActivityInstance i:list){
for(HistoricActivityInstance j:list){
if(i!=j){
set.add(i.getActivityId()+j.getActivityId());
}
}
}
//收集高亮连线数据
Set<String> highLineSet = new HashSet<>();
set.forEach(s -> {
if(map.containsKey(s)){
highLineSet.add(map.get(s));
}
});
List<HistoricActivityInstance> finishedList = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId).finished().list();
//收集高亮点的数据
Set<String> highPointSet = new HashSet<>();
finishedList.forEach(s -> highPointSet.add(s.getActivityId()));
List<HistoricActivityInstance> unfinishedList = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId).unfinished().list();
//收集待办点的数据
Set<String> highPointTodoSet = new HashSet<>();
unfinishedList.forEach(s -> highPointTodoSet.add(s.getActivityId()));
//获取当前用户完成的任务的定义key
Set<String> idoSet = new HashSet<>();
String assigneeName;
if(GlobalConfig.Test){
assigneeName = "bajie";
}else{
assigneeName = userInfo.getUsername();
}
List<HistoricTaskInstance> taskInstances = historyService.createHistoricTaskInstanceQuery()
.taskAssignee(assigneeName)
.processInstanceId(processInstanceId).finished().list();
taskInstances.forEach(s -> idoSet.add(s.getTaskDefinitionKey()));
Map<String,Object> resultMap = new HashMap<>();
resultMap.put("highPoint",highPointSet);
resultMap.put("highLine",highLineSet);
resultMap.put("highPointTodo",highPointTodoSet);
resultMap.put("ido",idoSet);
return AjaxResponse.ajaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
GlobalConfig.ResponseCode.SUCCESS.getDesc(),resultMap);
}catch (Exception e){
return AjaxResponse.ajaxData(GlobalConfig.ResponseCode.ERROR.getCode(),"高亮显示历史失败"+e.getMessage(),null);
}
}
下面我拆分代码来讲解:
第一段,我们的目的是获取历史人物的线条id,那么首先我们获取到实例,然后拿到这个实例中所有的线条,然后拼装一下(key:上一个任务id拼接下一个任务id,value:线条id)
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
//读取BPMN
BpmnModel bpmnModel = repositoryService.getBpmnModel(historicProcessInstance.getProcessDefinitionId());
Process process = bpmnModel.getProcesses().get(0);
Collection<FlowElement> elements = process.getFlowElements();
Map<String,String> map = new HashMap<>();
for(FlowElement element:elements){
if(element instanceof SequenceFlow){
SequenceFlow sequenceFlow = (SequenceFlow)element;
String sourceRef = sequenceFlow.getSourceRef();
String targetRef = sequenceFlow.getTargetRef();
map.put(sourceRef+targetRef,sequenceFlow.getId());
}
}
上一段代码我们获取到了所有完成任务的线条信息(key:上一个任务id拼接下一个任务id,value:线条id),为什么要两个for循环呢?其实就是拿到所有的拼接组合(其实我们主要用到的是正序的组合,这里图省事,反正最后跟代码一能匹配到的只有正序数据),然后这里是拿到所有任务(包含完成和待完成的),然后拼接 上一个任务id,下一个任务id 到一个set中;
在下面的for循环中,要做的就是:把属于历史任务的线条给找出来。
List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId).list();
Set<String> set = new HashSet<>();
for(HistoricActivityInstance i:list){
for(HistoricActivityInstance j:list){
if(i!=j){
set.add(i.getActivityId()+j.getActivityId());
}
}
}
//收集高亮连线数据
Set<String> highLineSet = new HashSet<>();
set.forEach(s -> {
if(map.containsKey(s)){
highLineSet.add(map.get(s));
}
});
与线条不一样,我们可以直接获取到历史任务的任务id,所以就不需要像线条一样的麻烦,直接拿就行了。因为历史任务分为 完成的任务和待办的任务,所以我们分别都需要获取
List<HistoricActivityInstance> finishedList = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId).finished().list();
//收集高亮点的数据
Set<String> highPointSet = new HashSet<>();
finishedList.forEach(s -> highPointSet.add(s.getActivityId()));
List<HistoricActivityInstance> unfinishedList = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId).unfinished().list();
//收集待办点的数据
Set<String> highPointTodoSet = new HashSet<>();
unfinishedList.forEach(s -> highPointTodoSet.add(s.getActivityId()));
最后一段代码是获取到我完成的任务,为什么会需要这样的信息呢?是这样呢,我们一般是从实例中打开历史渲染的界面,而该实例未必是由当前用户完成的,所以这里需要标注一下当前登录人是否有他完成的任务:
//获取当前用户完成的任务的定义key
Set<String> idoSet = new HashSet<>();
String assigneeName;
if(GlobalConfig.Test){
assigneeName = "bajie";
}else{
assigneeName = userInfo.getUsername();
}
List<HistoricTaskInstance> taskInstances = historyService.createHistoricTaskInstanceQuery()
.taskAssignee(assigneeName)
.processInstanceId(processInstanceId).finished().list();
taskInstances.forEach(s -> idoSet.add(s.getTaskDefinitionKey()));
网友评论