为什么要用Activiti工作流以及Acitiviti工作流的基础用法就不多说了,废话不多说,直接上代码(调用该方法,页面使用img标签即可)
package com.supermap.zzsubcorp.iPark.assetsMetaDataEngine.activiti.controller;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricDetail;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.bpmn.behavior.SubProcessActivityBehavior;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.ReadOnlyProcessDefinition;
import org.activiti.engine.impl.pvm.delegate.ActivityBehavior;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.image.ProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.supermap.zzsubcorp.iPark.assetsMetaDataEngine.activiti.ActivityUtil;
@Controller
@RequestMapping(value = "/traceProcess")
public class TraceProcessController {
@Autowired
TaskService taskService;
@Autowired
HistoryService historyService;
@Autowired
RuntimeService runtimeService;
@Autowired
RepositoryService repositoryService;
@Autowired
protected IdentityService identityService;
@Autowired
ProcessEngineConfiguration processEngineConfiguration;
/**
* 读取流程资源
*/
@RequestMapping(value = "trace/data/auto/{executionId}")
public void readResource(@PathVariable("executionId") String executionId, HttpServletResponse response)
throws Exception {
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(executionId).singleResult();
BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstance.getProcessDefinitionId()).singleResult();
List activeActivityIds = runtimeService.getActiveActivityIds(executionId);
List highLightedFlows = getHighLightedFlows(processDefinition, processInstance.getId());
ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
//InputStream imageStream =diagramGenerator.generateDiagram(bpmnModel, "png", activeActivityIds, highLightedFlows);
String activityFontName=processEngineConfiguration.getActivityFontName();
String labelFontName=processEngineConfiguration.getLabelFontName();
InputStream imageStream =diagramGenerator.generateDiagram(bpmnModel, "png", activeActivityIds, highLightedFlows,activityFontName,labelFontName,null, null, 1.0);
// 输出资源内容到相应对象
byte[] b = new byte[1024];
int len;
while ((len = imageStream.read(b, 0, 1024)) != -1) {
response.getOutputStream().write(b, 0, len);
}
}
private List getHighLightedFlows(ProcessDefinitionEntity processDefinition, String processInstanceId) {
List highLightedFlows = new ArrayList();
List historicActivityInstances = historyService
.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId)
.orderByHistoricActivityInstanceStartTime().asc().list();
List historicActivityInstanceList = new ArrayList();
for (HistoricActivityInstance hai : historicActivityInstances) {
historicActivityInstanceList.add(hai.getActivityId());
}
// add current activities to list
List highLightedActivities = runtimeService.getActiveActivityIds(processInstanceId);
historicActivityInstanceList.addAll(highLightedActivities);
// activities and their sequence-flows
for (ActivityImpl activity : processDefinition.getActivities()) {
int index = historicActivityInstanceList.indexOf(activity.getId());
if (index >= 0 && index + 1 < historicActivityInstanceList.size()) {
List pvmTransitionList = activity
.getOutgoingTransitions();
for (PvmTransition pvmTransition : pvmTransitionList) {
String destinationFlowId = pvmTransition.getDestination().getId();
if (destinationFlowId.equals(historicActivityInstanceList.get(index + 1))) {
highLightedFlows.add(pvmTransition.getId());
}
}
}
}
return highLightedFlows;
}
/**
* 读取历史数据
*
* @return
*/
@RequestMapping(value = "trace/view/{executionId}")
public ModelAndView historyDatas(@PathVariable("executionId") String executionId) {
ModelAndView mav = new ModelAndView("chapter13/trace-process");
// 查询Execution对象
Execution execution = runtimeService.createExecutionQuery().executionId(executionId).singleResult();
// 查询所有的历史活动记录
String processInstanceId = execution.getProcessInstanceId();
List activityInstances = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).list();
// 查询历史流程实例
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
// 查询流程有关的变量
List variableInstances = historyService.createHistoricVariableInstanceQuery()
.processInstanceId(processInstanceId).list();
List formProperties = historyService.createHistoricDetailQuery().processInstanceId(processInstanceId).formProperties().list();
// 查询流程定义对象
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionId(historicProcessInstance.getProcessDefinitionId()).singleResult();
// 查询运行时流程实例
ProcessInstance parentProcessInstance = runtimeService.createProcessInstanceQuery()
.subProcessInstanceId(execution.getProcessInstanceId()).singleResult();
mav.addObject("parentProcessInstance", parentProcessInstance);
mav.addObject("historicProcessInstance", historicProcessInstance);
mav.addObject("variableInstances", variableInstances);
mav.addObject("activities", activityInstances);
mav.addObject("formProperties", formProperties);
mav.addObject("processDefinition", processDefinition);
mav.addObject("executionId", executionId);
return mav;
}
/**
* 读取跟踪数据
*
* @param executionId
* @return
* @throws Exception
*/
@RequestMapping(value = "trace/data/{executionId}")
@ResponseBody
public List