首页>代码>activiti开源工作流深入浅出代码学习实例>/activiti-demo/src/main/java/org/activiti/demo/engine/ActivitiEngine.java
package org.activiti.demo.engine;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ActivitiEngine {
	public ActivitiEngine() {
		context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
		repositoryService = (RepositoryService) context.getBean("repositoryService");
		runtimeService = (RuntimeService) context.getBean("runtimeService");
		taskService = (TaskService) context.getBean("taskService");
		historyService = (HistoryService) context.getBean("historyService");
		processEngine = (ProcessEngine) context.getBean("processEngine");
		logger.info("engine init success!");
	}

	/**************** 日志对象 ****************/
	private static final Logger logger = LoggerFactory.getLogger(SimpleProcessEngine.class);

	protected static RepositoryService repositoryService;

	protected static RuntimeService runtimeService;

	protected static TaskService taskService;

	protected static HistoryService historyService;

	protected static ProcessEngine processEngine;

	private ApplicationContext context;

	/**
	 * 部署流程实例
	 * 
	 * @param processFilePath
	 *            流程文件路径
	 * @throws FileNotFoundException
	 */
	public void deploymentInstance(String processFilePath) throws FileNotFoundException {
		InputStream in = new FileInputStream(new File(processFilePath));
		// 从文件流部署流程
		Deployment deployment = repositoryService.createDeployment().addInputStream(processFilePath.substring(processFilePath.lastIndexOf(File.separator) + 1, processFilePath.length()), in).deploy();
		logger.info("process deployment success & processID is " + deployment.getId() + ",createTime is " + deployment.getDeploymentTime());
	}

	/**
	 * 启动流程实例
	 * 
	 * @param processInstanceByKey
	 *            流程部署KEY
	 * @param proMap
	 *            流程变量
	 */
	public ProcessInstance startInstance(String processInstanceByKey, Map<String, Object> proMap) {
		// 启动流程实例 ,注意这里的key是我们流程文件中的process id="myProcess"
		ProcessInstance processInstance = null;
		if (proMap != null) {
			processInstance = runtimeService.startProcessInstanceByKey(processInstanceByKey, proMap);
		} else {
			processInstance = runtimeService.startProcessInstanceByKey(processInstanceByKey);
		}
		logger.info("process start success  key [" + processInstance.getBusinessKey() + "]");
		return processInstance;
	}

	/**
	 * 根据executionId查询task
	 * 
	 * @param executionId
	 * @return
	 */
	public Task queryByExecutionIdSingle(String executionId) {
		Task task = taskService.createTaskQuery().executionId(executionId).singleResult();
		return task;
	}

	/**
	 * 完成UserTask
	 * 
	 * @param taskId
	 *            任务ID
	 * @param proMap
	 *            流程变量
	 */
	public void completeUserTask(String taskId, Map<String, Object> proMap) {
		if (proMap != null) {
			taskService.complete(taskId, proMap);
		} else {
			taskService.complete(taskId);
		}
	}

	/**
	 * 获取下一节点审批人
	 * 
	 * @param processEngineCore
	 * @param userTask
	 * @return
	 */
	public String getNextAppersonal(String executionId) {
		List<Task> taskList = queryByExecutionId(executionId);
		String nextApp = "";
		for (int i = 0; i < taskList.size(); i++) {
			nextApp += taskList.get(i).getAssignee() + (i == taskList.size() - 1 ? "" : ",");
		}
		return nextApp;
	}

	/**
	 * 根据executionId查询task
	 * 
	 * @param executionId
	 * @return
	 */
	public List<Task> queryByExecutionId(String executionId) {
		List<Task> taskList = taskService.createTaskQuery().executionId(executionId).list();
		return taskList;
	}

	/**
	 * 查询UserTask列表
	 * 
	 * @param userName
	 * @return
	 */
	public List<Task> queryUserTaskList(String userName) {
		// 查询当前用户任务列表
		List<Task> taskList = taskService.createTaskQuery().taskAssignee(userName).list();
		return taskList;
	}

	/**
	 * 根据流程ID查看流程是否结束
	 * 
	 * @param processInstanceId
	 *            流程ID
	 */
	public boolean queryProcessIsEnd(String processInstanceId) {
		HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
		if (historicProcessInstance != null && historicProcessInstance.getStartTime() != null && historicProcessInstance.getEndTime() != null) {
			return true;
		}
		return false;
	}
}
最近下载更多
dapeng0011  LV13 2023年7月3日
氼乚-----------  LV5 2022年3月17日
574198962  LV5 2022年2月25日
laishaofe  LV7 2021年12月22日
mafangnu  LV8 2021年10月8日
250250000  LV5 2021年8月3日
46900596  LV12 2021年7月4日
tiansitong  LV14 2021年6月27日
tiashishiwo  LV5 2021年6月18日
ShuSuU  LV5 2021年4月22日
最近浏览更多
denglu123321  LV4 4月11日
云破月  LV8 2月2日
wubz2008  LV5 2023年9月15日
F丶S丶H  LV7 2023年7月5日
dapeng0011  LV13 2023年7月3日
z875152686  LV8 2023年6月28日
康日澜  LV9 2023年4月14日
微信网友_5992582549164032  LV6 2023年2月16日
felixsxf  LV5 2023年1月31日
cooper1 2022年12月14日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友