Flowable常用API

该博客详细介绍了如何使用工作流引擎创建和管理业务流程。首先,创建BpmnModel,包括设置用户任务、序列流和事件。接着,发布流程到仓库服务,并启动流程实例。流程中的签核操作,如通过和驳回,都有对应的处理逻辑。此外,还提供了查询签核历史记录、检查流程是否结束、获取流程签核节点以及分页查询待办和已办任务的方法。整个流程管理实现覆盖了流程的创建、执行和监控等关键环节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 创建BpmnModel
private final String USERTASKID="UserTaskId";

	public BpmnModel createBpmnModel(CustomsFlowDefinition customsFlowDefinition) {
		BpmnModel bpmnModel = new BpmnModel();
		Process process = new Process();
		process.setId(customsFlowDefinition.getFlowName());
		createSequenceFlow(process,customsFlowDefinition);
		createUserTask(process,customsFlowDefinition);
		bpmnModel.addProcess(process);
		return bpmnModel;
	}

	private Process createEvent(Process process, String flowName, LinkedList<SequenceFlow> linkedList) {
		StartEvent startEvent = new StartEvent();
		startEvent.setName("StartEventName");
		startEvent.setId("StartEventId");
		startEvent.setOutgoingFlows(Arrays.asList(linkedList.getFirst()));
		process.addFlowElement(startEvent);
		EndEvent endEvent = new EndEvent();
		endEvent.setName( "EndEventName");
		endEvent.setId("EndEventId");
		endEvent.setOutgoingFlows(Arrays
			.asList(linkedList.getLast()));
		process.addFlowElement(endEvent);
		return process;

	}


	private Process createSequenceFlow(Process process, CustomsFlowDefinition customsFlowDefinition) {
		LinkedList<SequenceFlow> linkedList = new LinkedList<>();
		for (int i = 0; i < customsFlowDefinition.getNodes().size() + 1; i++) {
			SequenceFlow sequenceFlow = new SequenceFlow();
			sequenceFlow.setId("SeqId" + i);
			sequenceFlow.setName("SeqName" + i);
			if (i == 0) {
				sequenceFlow.setSourceRef("StartEventId");
				sequenceFlow.setTargetRef(customsFlowDefinition.getNodes().get(0).getNodeKey());
			}else if (i == customsFlowDefinition.getNodes().size()) {
				sequenceFlow.setSourceRef(customsFlowDefinition.getNodes().get(customsFlowDefinition.getNodes().size()-1).getNodeKey());
				sequenceFlow.setTargetRef("EndEventId");
			}else {
				sequenceFlow.setSourceRef(customsFlowDefinition.getNodes().get((i-1)).getNodeKey());
				sequenceFlow.setTargetRef(customsFlowDefinition.getNodes().get((i)).getNodeKey());
			}
			sequenceFlow.setConditionExpression("${outcome=='agree'}");
			process.addFlowElement(sequenceFlow);
			linkedList.add(sequenceFlow);
		}
		createEvent(process,customsFlowDefinition.getFlowName(),linkedList);
		return process;
	}


	private UserTask createUserTask(String taskId, String taskName,
	                                SequenceFlow inSequenceFlow, SequenceFlow outSequenceFlow,
	                                String userId) {
		UserTask userTask = new UserTask();
		userTask.setAssignee(userId);
		userTask.setName(taskName);
		userTask.setId(taskId);
		return userTask;
	}


	private Process createUserTask(Process process, CustomsFlowDefinition customsFlowDefinition) {
		for (int i = 0; i < customsFlowDefinition.getNodes().size(); i++) {
			List<String> users = new ArrayList<String>();
			users.add("${userIds}");
			UserTask userTask = new UserTask();
			String userTaskName=customsFlowDefinition.getNodes().get(i).getNodeName();
			String userTaskId=customsFlowDefinition.getNodes().get(i).getNodeKey();
			Assert.state(!StringUtils.isEmpty(userTaskId),"节点key不能为空");
			userTask.setName(userTaskName==null?("UserTaskName"+i):userTaskName);
			userTask.setId(userTaskId);
			userTask.setCandidateUsers(users);
			process.addFlowElement(userTask);
		}
		return process;
	}
  1. 发布流程
RepositoryService repositoryService = processEngine
				.getRepositoryService();
			Deployment deployment = repositoryService.createDeployment()
				.addBpmnModel(businessKey + ".bpmn", bpmnModel)
				.name(flowName).deploy();
			ProcessDefinition processDefinition = repositoryService
				.createProcessDefinitionQuery().deploymentId(deployment.getId())
				.singleResult();
			String id = processDefinition.getId();
  1. 开始一个流程实例
ProcessInstance processInstance = processEngine.getRuntimeService()
			.startProcessInstanceById(flowId, businessKey, variables);
  1. 签核通过
Task task = taskService
			.createTaskQuery()
			.processInstanceId(processId).active().singleResult();
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("outcome", "agree");
		variables.put("userIds", users);
		taskService.addComment(task.getId(), processId, remarks);
		taskService.setAssignee(task.getId(), userId);
		taskService.complete(task.getId(), variables);
  1. 签核驳回

 TaskService taskService = processEngine.getTaskService();
		Task task = taskService.createTaskQuery()
		.processInstanceId(processInstanceId).active().singleResult();
		List<CustomsFlowHistory> list = queryHistoryData(processInstanceId);
		if (null == targetNodeId) {
			targetNodeId = list.get(0).getNodeId();
		}
		List<String> key = new ArrayList<String>();
		key.add(task.getTaskDefinitionKey());
		taskService.setAssignee(task.getId(), currentOptUserId);
		taskService.addComment(task.getId(), task.getProcessInstanceId(),
			rejectionReason);
RuntimeService runtimeService = processEngine.getRuntimeService();
		runtimeService.setVariable(processInstanceId, "userIds", targetUserid);
		runtimeService.createChangeActivityStateBuilder()
			.processInstanceId(processInstanceId)
	.moveActivityIdsToSingleActivityId(currentActivityId,
				newActivityId)
			.changeState();

6.查询签核历史记录

 HistoryService historyService = processEngine.getHistoryService();
		TaskService taskService = processEngine.getTaskService();
		List<HistoricActivityInstance> activities = historyService
			.createHistoricActivityInstanceQuery()
			.processInstanceId(processInstanceId).activityType("userTask")
			.orderByHistoricActivityInstanceEndTime().asc().list();

7.查询流程是否结束

public boolean isFlowEnd(String processInstanceId) {

		// TODO Auto-generated method stub
		boolean result = false;
		TaskService taskService = processEngine.getTaskService();
		RepositoryService repositoryService = processEngine.getRepositoryService();
		Task task = taskService.createTaskQuery()
			.processInstanceId(processInstanceId).singleResult();
		if(task==null){
			return true;
		}
		String definitionId = processEngine.getHistoryService()
			.createProcessInstanceHistoryLogQuery(processInstanceId).singleResult()
			.getProcessDefinitionId();
		BpmnModel bpmnModel = repositoryService.getBpmnModel(definitionId);
		FlowNode flowNode = (FlowNode) bpmnModel
			.getFlowElement(task.getTaskDefinitionKey());
		List<SequenceFlow> outgoingFlows = flowNode.getOutgoingFlows();
		for (SequenceFlow outgoingFlow : outgoingFlows) {
			FlowElement targetFlowElement = outgoingFlow.getTargetFlowElement();
			if (targetFlowElement instanceof EndEvent) {
				result = true;
				break;
			}
		}
		return result;
	}

8.查询流程签核节点

public List<CustomsFlowNode> queryFlowNode(String processId) {
				ProcessInstance processInstance = processEngine.getRuntimeService()
					.createProcessInstanceQuery().processInstanceId(processId).singleResult();
				List<UserTask> list = allFlowNode(processInstance.getProcessDefinitionId());
				if (list != null) {
					return list.stream().map(userTask -> {
						CustomsFlowNode customsFlowNode = new CustomsFlowNode();
						customsFlowNode.setNodeName(userTask.getName());
						return customsFlowNode;
					}).collect(Collectors.toList());
				}
				return null;
			}
public List<UserTask> allFlowNode(String flowId) {
		RepositoryService repositoryService = processEngine.getRepositoryService();
		StringBuffer buffer = new StringBuffer();
		BpmnModel bpmnModel = repositoryService.getBpmnModel(flowId);
		Process process = bpmnModel.getProcesses().get(0);
		Collection<FlowElement> flowElements = process.getFlowElements();
		return flowElements.stream().filter(element -> element instanceof UserTask).map(e -> (UserTask) e).collect(Collectors.toList());
	}

9.查询所有的流程

 List<ProcessDefinition> list = processEngine.getRepositoryService()
			.createProcessDefinitionQuery()
			.orderByProcessDefinitionVersion().asc()
			.list();

10.删除流程

processEngine.getRuntimeService().deleteProcessInstance(processId, reason);

11.分页查询代办任务

List<Task> list = processEngine.getTaskService()
			.createTaskQuery()
			.taskCandidateOrAssigned(userId).orderByTaskDueDate().desc().includeProcessVariables()
			.active().orderByProcessInstanceId().desc()
			.listPage((firstResult-1)*maxResults, firstResult*maxResults-1);
	

12.分页查询已办任务

HistoryService historyService = processEngine.getHistoryService();
historyService.createHistoricTaskInstanceQuery()
				.processVariableValueEquals(BUSINESSTYPE, documentType)
				.taskAssignee(userId).includeProcessVariables().orderByProcessInstanceId().desc()
				.finished()
				.listPage((firstResult-1)*maxResults, firstResult*maxResults-1);

13.获取当前流程实例在整个流程图片

public InputStream getImageoutputStream(String processInstanceId,
	                                        String flowId) {
		RuntimeService runtimeService = processEngine.getRuntimeService();
		HistoryService historyService = processEngine.getHistoryService();
		RepositoryService repositoryService = processEngine.getRepositoryService();
		List<String> highLightedActivities = runtimeService
			.getActiveActivityIds(processInstanceId);
		if (highLightedActivities != null) {
			ProcessDiagramGenerator defaultProcessDiagramGenerator = new DefaultProcessDiagramGenerator();
			List<String> highLightedFlows = new ArrayList<String>();
			HistoricProcessInstance his = historyService
				.createHistoricProcessInstanceQuery()
				.processInstanceId(processInstanceId).singleResult();
			BpmnModel model = repositoryService.getBpmnModel(flowId);
			InputStream ips = defaultProcessDiagramGenerator.generateDiagram(
				model, "png", highLightedActivities, highLightedFlows, "宋体",
				"宋体", "宋体", null, 1.0);
			return ips;
		}
		return null;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值