最近项目,用到流程。第一次用,就自己做了一下小结:
1.
===================================下面这段代码是获取流程线信息=============================
//
//processDefinitionId 对应表ACT_RE_PROCDEF主键信息
String processDefinitionId="出差:1:2549";
//获取bpmnModel对象
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
//因为我们这里只定义了一个Process 所以获取集合中的第一个即可
Process process = bpmnModel.getProcesses().get(0);
//获取所有的FlowElement信息
Collection
for (FlowElement flowElement : flowElements) {
//如果是任务节点
if (flowElement instanceof UserTask) {
UserTask userTask=(UserTask) flowElement;
//获取入线信息
List
for (SequenceFlow sequenceFlow : incomingFlows) {
System.out.println(sequenceFlow.getId() +"=="+sequenceFlow.getName()+"-"+sequenceFlow.getConditionExpression()+"--"+sequenceFlow.getDocumentation()+"-"
+sequenceFlow.getSourceRef()+"--"+sequenceFlow.getTargetRef()+"-");
}
}
}
====================================================================================
2.
=========================================获取流程节点===========================================
ProcessDefinitionEntity processDefinitionEntity=(ProcessDefinitionEntity)repositoryService.getProcessDefinition("请病假:1:9");
List
for(ActivityImpl activiti:activityList) {
System.out.println("节点ID:"+activiti.getId());
System.out.println("节点名称:"+activiti.getProperty("name"));
}
====================================================================================
3.
=======================================这个方法是获取流程图,节点数据=============================================
Model modelD = repositoryService.getModel(modelId);
BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
//获取节点信息
byte[] arg0 = repositoryService.getModelEditorSource(modelD.getId());
JsonNode editorNode = new ObjectMapper().readTree(arg0);
//将节点信息转换为xml
BpmnModel bpmnModel = jsonConverter.convertToBpmnModel(editorNode);
BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
byte[] bpmn = xmlConverter.convertToXML(bpmnModel);
String s = new String(bpmn);
System.out.println(s);
ByteArrayInputStream in = new ByteArrayInputStream(bpmn);
IOUtils.copy(in, response.getOutputStream());
====================================================================================
4.
========================================这个方法是终止流程============================================
/**
*taskId:任务id
*opinion:意见
*
*/
public void endTask(String taskId,String opinion){
//根据taskID查询任务
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
ActivityImpl endActivity = findActivitiImpl(taskId, "end");
if(endActivity==null){
return;
}
// 获取当前活动节点ID
//String activityId = task.getTaskDefinitionKey();
Map
if (!StringUtils.isEmpty(opinion)) { //审核意见
variables.put("驳回意见", opinion);
}
// 跳转节点为空,默认提交操作
if (StringUtils.isEmpty(endActivity.getId())) {
if(!StringUtils.isEmpty(task.getOwner())){
// 被委派人处理完成任务
taskService.resolveTask(task.getId(),variables);
}
taskService.complete(taskId, variables);
} else {// 流程转向操作
//turnTransition(taskId, activityId, variables);
// 当前节点
ActivityImpl currActivity = findActivitiImpl(taskId, null);
// 清空当前流向
// 存储当前节点所有流向临时变量
List
// 获取当前节点所有流向,存储到临时变量,然后清空
List
.getOutgoingTransitions();
for (PvmTransition pvmTransition : pvmTransitionList) {
oriPvmTransitionList.add(pvmTransition);
}
pvmTransitionList.clear();
// 创建新流向
TransitionImpl newTransition = currActivity.createOutgoingTransition();
// 目标节点
ActivityImpl pointActivity = findActivitiImpl(taskId, endActivity.getId());
// 设置新流向的目标节点
newTransition.setDestination(pointActivity);
if(!StringUtils.isEmpty(task.getOwner())){
// 被委派人处理完成任务
taskService.resolveTask(task.getId(),variables);
}
// 执行转向任务
taskService.complete(taskId, variables);
// 删除目标节点新流入
pointActivity.getIncomingTransitions().remove(newTransition);
// 还原以前流向
//restoreTransition(currActivity, oriPvmTransitionList);
// 清空现有流向
List
pvmTransitionListC.clear();
// 还原以前流向
for (PvmTransition pvmTransition : oriPvmTransitionList) {
pvmTransitionListC.add(pvmTransition);
}
}
}
/**
* 根据任务ID和节点ID获取活动节点
*
* @param taskId
* 任务ID
* @param activityId
* 活动节点ID
* 如果为null或"",则默认查询当前活动节点
* 如果为"end",则查询结束节点
*
* @return
* @throws Exception
*/
private ActivityImpl findActivitiImpl(String taskId, String activityId){
TaskEntity task = (TaskEntity) taskService.createTaskQuery().taskId(
taskId).singleResult();
if (task == null) {
return null;
}
// 取得流程定义
ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
.getDeployedProcessDefinition(task.getProcessDefinitionId());
if (processDefinition == null) {
//throw new Exception("流程定义未找到!");
return null;
}
// 获取当前活动节点ID
if (StringUtils.isEmpty(activityId)) {
activityId = task.getTaskDefinitionKey();
}
// 根据流程定义,获取该流程实例的结束节点
if (activityId.toUpperCase().equals("END")) {
for (ActivityImpl activityImpl : processDefinition.getActivities()) {
List
.getOutgoingTransitions();
if (pvmTransitionList.isEmpty()) {
return activityImpl;
}
}
}
// 根据节点ID,获取对应的活动节点
ActivityImpl activityImpl = ((ProcessDefinitionImpl) processDefinition)
.findActivity(activityId);
return activityImpl;
}
====================================================================================
5.
===================================根据当然任务id获取上一个或下一个节点名称=================================================
TaskEntity task = (TaskEntity) taskService.createTaskQuery().taskId(
"5010").singleResult();
// 取得流程定义
ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
.getDeployedProcessDefinition(task.getProcessDefinitionId());
List
String excId = task.getExecutionId();
ExecutionEntity execution = (ExecutionEntity) runtimeService.createExecutionQuery().executionId(excId).singleResult();
String activitiId = execution.getActivityId();
for(ActivityImpl activityImpl:activitiList){
String id = activityImpl.getId();
if(activitiId.equals(id)){
System.out.println("当前任务:"+activityImpl.getProperty("name")); //输出某个节点的某种属性
/*List
for(PvmTransition tr:outTransitions){
PvmActivity ac = tr.getDestination(); //获取线路的终点节点
System.out.println("下一步任务任务:"+ac.getProperty("name"));
}*/
List
for(PvmTransition tr:outTransitions){
PvmActivity ac = tr.getSource(); //获取线路的起点节点
System.out.println("上一步任务任务:"+ac.getProperty("name"));
}
break;
}
}
====================================================================================
6.
===========================================回退到上一级=========================================
UeEntry findByIdEntry = ueEntryService.findByIdEntry(ueid);
String taskId = findByIdEntry.getTaskId();
Map
// 取得当前任务
HistoricTaskInstance currTask = historyService
.createHistoricTaskInstanceQuery().taskId(taskId)
.singleResult();
// 取得流程实例
ProcessInstance instance = runtimeService
.createProcessInstanceQuery()
.processInstanceId(currTask.getProcessInstanceId())
.singleResult();
if (instance == null) {
//流程结束
}
variables = instance.getProcessVariables();
// 取得流程定义
ProcessDefinitionEntity definition = (ProcessDefinitionEntity) (repositoryService.getProcessDefinition(currTask
.getProcessDefinitionId()));
if (definition == null) {
//log.error("流程定义未找到");
return null;
}
// 取得上一步活动
ActivityImpl currActivity = ((ProcessDefinitionImpl) definition)
.findActivity(currTask.getTaskDefinitionKey());
//也就是节点间的连线
List
.getIncomingTransitions();
// 清除当前活动的出口
List
List
.getOutgoingTransitions();
for (PvmTransition pvmTransition : pvmTransitionList) {
oriPvmTransitionList.add(pvmTransition);
}
pvmTransitionList.clear();
//得到前一个activiId;
String preId="";
// 建立新出口
List
for (PvmTransition nextTransition : nextTransitionList) {
PvmActivity nextActivity = nextTransition.getSource();
preId = nextActivity.getId();
ActivityImpl nextActivityImpl = ((ProcessDefinitionImpl) definition)
.findActivity(nextActivity.getId());
TransitionImpl newTransition = currActivity
.createOutgoingTransition();
newTransition.setDestination(nextActivityImpl);
newTransitions.add(newTransition);
}
// 完成任务
List
.processInstanceId(instance.getId())
.taskDefinitionKey(currTask.getTaskDefinitionKey()).list();
for (Task task : tasks) {
taskService.complete(task.getId(), variables);
historyService.deleteHistoricTaskInstance(task.getId());
}
// 恢复方向
for (TransitionImpl transitionImpl : newTransitions) {
currActivity.getOutgoingTransitions().remove(transitionImpl);
}
for (PvmTransition pvmTransition : oriPvmTransitionList) {
pvmTransitionList.add(pvmTransition);
}
====================================================================================
7.
===========================================查看流程图=========================================
/**
* 获取所有模型列表
* @return
*/
@RequestMapping("getActiviImg.do")
@ResponseBody
public void getActiviImg(String deploymentId,String modelId,HttpServletResponse resp){
Model model = repositoryService.createModelQuery().modelId(modelId).singleResult();
String imageName=null;
if(model!=null){
imageName = model.getName()+"."+model.getKey()+".png";
try{
InputStream in = repositoryService.getResourceAsStream(deploymentId,imageName);//此处方法实际项目应该放在service里面
OutputStream out = null;
resp.setHeader("Content-type", "image/jpeg;charset=UTF-8");
try {
out = resp.getOutputStream();
// 把图片的输入流程写入resp的输出流中
byte[] b = new byte[1024];
for (int len = -1; (len= in.read(b))!=-1; ) {
out.write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}catch(ActivitiObjectNotFoundException ex){
}
}
}
前端用src链接这个action
====================================================================================