相关api 实例
基础部分
1.创建一个流程实例
@Override
public Result createProcessInstance(Paramenter paramenter) {
Result result = new Result();
String processInstanceByKey=paramenter.getProcessInstanceByKey();
try {
String processInstanceId=ProcessUtils.createProcessInstance(processEngine,processInstanceByKey);
result.setResult(true);
result.setCode(ConstCode.OPERAT_SUCCESS);
result.setData(processInstanceId);
} catch (Exception e) {
logger.error("业务流程实例化失败!");
logger.error(e.getMessage());
result.setResult(false);
result.setCode(ConstCode.OPERAT_FAILURE);
result.setMessage("业务流程实例化失败!");
result.setError(e.getMessage());
}
return result;
}
2.查找任务
@Override
public Result createProcessInstance(Paramenter paramenter) {
Result result = new Result();
String processInstanceByKey=paramenter.getProcessInstanceByKey();
try {
String processInstanceId=ProcessUtils.createProcessInstance(processEngine,processInstanceByKey);
result.setResult(true);
result.setCode(ConstCode.OPERAT_SUCCESS);
result.setData(processInstanceId);
} catch (Exception e) {
logger.error("业务流程实例化失败!");
logger.error(e.getMessage());
result.setResult(false);
result.setCode(ConstCode.OPERAT_FAILURE);
result.setMessage("业务流程实例化失败!");
result.setError(e.getMessage());
}
return result;
}
3.执行处理任务
@Override
public Result completeTask(Paramenter paramenter) {
Result result = new Result();
String taskId=paramenter.getTaskId();
Object signal=paramenter.getSignal();
try {
ProcessUtils.completeTask(processEngine, taskId, signal);
result.setResult(true);
result.setCode(ConstCode.OPERAT_SUCCESS);
} catch (Exception e) {
logger.error("用户作业处理失败!");
logger.error(e.getMessage());
result.setResult(false);
result.setCode(ConstCode.OPERAT_FAILURE);
result.setMessage("用户作业处理失败!");
result.setError(e.getMessage());
}
return result;
}
4.查找历史任务
@Override
public Result historyTaskList(Paramenter paramenter) {
Result result = new Result();
String processInstanceId=paramenter.getProcessInstanceId();
List<TaskDto> taskDtoList=null;
TaskDto taskDto=null;
try {
List<HistoricTaskInstance> list=ProcessUtils.historyTaskList(processEngine, processInstanceId);
if(list!=null){
taskDtoList=new ArrayList<TaskDto>();
for (HistoricTaskInstance historicTaskInstance : list) {
taskDto=new TaskDto();
taskDto.setId(historicTaskInstance.getId());
taskDto.setExecutionId(historicTaskInstance.getExecutionId());
taskDto.setName(historicTaskInstance.getName());
taskDto.setProcessInstanceId(historicTaskInstance.getProcessInstanceId());
taskDto.setStartTime(historicTaskInstance.getStartTime());
taskDto.setEndTime(historicTaskInstance.getEndTime());
taskDtoList.add(taskDto);
}
}
result.setResult(true);
result.setCode(ConstCode.OPERAT_SUCCESS);
result.setListData(taskDtoList);
} catch (Exception e) {
logger.error("查看流转日志失败!");
logger.error(e.getMessage());
result.setResult(false);
result.setCode(ConstCode.OPERAT_FAILURE);
result.setMessage("查看流转日志失败!");
result.setError(e.getMessage());
}
return result;
}
5查找历史Activity任务
@Override
public Result historyActivityList(Paramenter paramenter) {
Result result = new Result();
String processInstanceId=paramenter.getProcessInstanceId();
List<ActivityDto> activityDtoList=null;
ActivityDto activityDto=null;
try {
List<HistoricActivityInstance> list=ProcessUtils.historyActivityList(processEngine, processInstanceId);
if(list!=null){
activityDtoList=new ArrayList<ActivityDto>();
for (HistoricActivityInstance historicActivityInstance : list) {
activityDto=new ActivityDto();
activityDto.setActivitiId(historicActivityInstance.getActivityId());
activityDto.setActivityName(historicActivityInstance.getActivityName());
activityDto.setActivityType(historicActivityInstance.getActivityType());
activityDto.setDuration(historicActivityInstance.getDurationInMillis());
activityDto.setProcessInstanceId(historicActivityInstance.getProcessInstanceId());
activityDto.setTaskId(historicActivityInstance.getTaskId());
activityDto.setStartTime(historicActivityInstance.getStartTime());
activityDto.setEndTime(historicActivityInstance.getEndTime());
activityDtoList.add(activityDto);
}
}
result.setResult(true);
result.setCode(ConstCode.OPERAT_SUCCESS);
result.setListData(activityDtoList);
} catch (Exception e) {
logger.error("查看流转日志失败!");
logger.error(e.getMessage());
result.setResult(false);
result.setCode(ConstCode.OPERAT_FAILURE);
result.setMessage("查看流转日志失败!");
result.setError(e.getMessage());
}
return result;
}
特殊部分
1.删除任务
@Override
public Result deleteProcessInstance(Paramenter paramenter) {
Result result = new Result();
String processInstanceId=paramenter.getProcessInstanceId();
String deleteReason=paramenter.getDeleteReason();
try {
ProcessUtils.deleteProcessInstance(processEngine, processInstanceId, deleteReason);
result.setResult(true);
result.setCode(ConstCode.OPERAT_SUCCESS);
} catch (Exception e) {
logger.error("删除业务流程实例失败!");
logger.error(e.getMessage());
result.setResult(false);
result.setCode(ConstCode.OPERAT_FAILURE);
result.setMessage("删除业务流程实例失败!");
result.setError(e.getMessage());
}
return result;
}
2.挂机和重启任务
@Override
public Result suspendAndActivateProcessInstance(Paramenter paramenter) {
Result result = new Result();
String processInstanceId=paramenter.getProcessInstanceId();
Boolean suspendOrActivate=paramenter.getSuspendOrActivate();
try {
ProcessUtils.suspendOrActivateProcessInstance(processEngine, processInstanceId, suspendOrActivate);
result.setResult(true);
result.setCode(ConstCode.OPERAT_SUCCESS);
} catch (Exception e) {
logger.error("流程的挂起或激活失败!");
logger.error(e.getMessage());
result.setResult(false);
result.setCode(ConstCode.OPERAT_FAILURE);
result.setMessage("流程的挂起或激活失败!");
result.setError(e.getMessage());
}
return result;
}
3.执行任务带审批内容
@Override
public Result completeTaskDetail(Paramenter paramenter) {
Result result = new Result();
String taskId=paramenter.getTaskId();
Object signal=paramenter.getSignal();
String context=paramenter.getContext();
try {
ProcessUtils.completeTaskDetail(processEngine, taskId, signal,context);
result.setResult(true);
result.setCode(ConstCode.OPERAT_SUCCESS);
} catch (Exception e) {
logger.error("用户作业处理失败!");
logger.error(e.getMessage());
result.setResult(false);
result.setCode(ConstCode.OPERAT_FAILURE);
result.setMessage("用户作业处理失败!");
result.setError(e.getMessage());
}
return result;
}
4.查找历史流程实例带审批
@Override
public Result historyTaskListDetail(Paramenter paramenter) {
Result result = new Result();
String processInstanceId=paramenter.getProcessInstanceId();
String taskId=paramenter.getTaskId();
List<TaskDto> taskDtoList=null;
TaskDto taskDto=null;
try {
List<HistoricTaskInstance> list=ProcessUtils.historyTaskList(processEngine, processInstanceId);
if(list!=null){
taskDtoList=new ArrayList<TaskDto>();
for (HistoricTaskInstance historicTaskInstance : list) {
taskDto=new TaskDto();
taskDto.setId(historicTaskInstance.getId());
taskDto.setExecutionId(historicTaskInstance.getExecutionId());
taskDto.setName(historicTaskInstance.getName());
taskDto.setProcessInstanceId(historicTaskInstance.getProcessInstanceId());
taskDto.setStartTime(historicTaskInstance.getStartTime());
taskDto.setEndTime(historicTaskInstance.getEndTime());
String executionId=taskDto.getExecutionId();
List<HistoricVariableInstance> lists = processEngine.getHistoryService()
.createHistoricVariableInstanceQuery()//创建一个历史的流程变量查询
.processInstanceId(processInstanceId)
.executionId(executionId)
.variableName("context")
//.taskId(taskDto.getId())
.list();
for(HistoricVariableInstance hiv : lists){
String context=(String) hiv.getValue();
taskDto.setContext(context);
}
taskDtoList.add(taskDto);
}
}
result.setResult(true);
result.setCode(ConstCode.OPERAT_SUCCESS);
result.setListData(taskDtoList);
} catch (Exception e) {
logger.error("查看流转日志失败!");
logger.error(e.getMessage());
result.setResult(false);
result.setCode(ConstCode.OPERAT_FAILURE);
result.setMessage("查看流转日志失败!");
result.setError(e.getMessage());
}
return result;
}
定义工具类ProcessUtils
public class ProcessUtils {
/**
* 流程操作
*
* @param taskId
* 作业Id
* @param content
* 审批内容
*/
public static void completeTask(ProcessEngine processEngine,String taskId, Object signal) {
Map<String, Object> variables = new HashMap();
variables.put("signal", signal);
processEngine.getTaskService().complete(taskId, variables);
}
/**
* 查看流转日志(某一次流程的执行经历的任务)
*
* @param processInstanceId
*/
public static List<HistoricTaskInstance> historyTaskList(ProcessEngine processEngine,String processInstanceId) {
List<HistoricTaskInstance> list = processEngine.getHistoryService().createHistoricTaskInstanceQuery()
.processInstanceId(processInstanceId)
// .finished()
.list();
return list;
}
/**
* 查看流转日志(某一次流程的执行经历的活动)
*
* @param processInstanceId
*/
public static List<HistoricActivityInstance> historyActivityList(ProcessEngine processEngine,String processInstanceId) {
List<HistoricActivityInstance> list = processEngine.getHistoryService().createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId)
// .finished()
.list();
return list;
}
/**
* 判断当前流程是否执行完 0是,1否
*/
public static String processState(ProcessEngine processEngine,String processInstanceId) {
ProcessInstance pi = processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId)
.singleResult();
String flag="1";
if (pi == null) {
flag="0";
}
return flag;
}
/**
* 删除当前流程实例
*/
public static void deleteProcessInstance(ProcessEngine processEngine,String processInstanceId,String deleteReason) {
processEngine.getRuntimeService().deleteProcessInstance(processInstanceId, deleteReason);
}
/**挂起或激活当前流程实例
*
* @param processEngine
* @param processInstanceId
* @param suspendOrActivate ture:挂起,false:激活
* @return
*/
public static Boolean suspendOrActivateProcessInstance(ProcessEngine processEngine,String processInstanceId,Boolean suspendOrActivate) {
RuntimeService ru=processEngine.getRuntimeService();
if(suspendOrActivate){
//挂起流程实例
ru.suspendProcessInstanceById(processInstanceId);
//验证是否挂起
return ru.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult().isSuspended();
}if(!suspendOrActivate){
//激活流程实例
ru.activateProcessInstanceById(processInstanceId);
//验证是否激活
return !ru.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult().isSuspended();
}
return false;
}
/**
* 返回用户流程作业
*
* @param candidateUser
* @return
*/
public static List<Task> findTask(ProcessEngine processEngine,String candidateUser) {
List<Task> taskList = processEngine.getTaskService().createTaskQuery().taskCandidateUser(candidateUser).list();
return taskList;
}
/** 查询 历史任务的用户列表 */
public static List<HistoricIdentityLink> findHistoryGroupCandidate(ProcessEngine processEngine,String processInstanceId) {
// 流程实例ID
List<HistoricIdentityLink> list = processEngine.getHistoryService()
.getHistoricIdentityLinksForProcessInstance(processInstanceId);
if (list != null && list.size() > 0) {
for (HistoricIdentityLink identityLink : list) {
System.out.println("任务ID:" + identityLink.getTaskId());
System.out.println("用户ID:" + identityLink.getUserId());
System.out.println("工作流角色ID:" + identityLink.getGroupId());
}
}
return list;
}
/**
* 创建一个流程实例task(流程作业)
*
* @throws Exception
*/
public static String createProcessInstance(ProcessEngine processEngine,String processInstanceByKey) throws Exception {
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processInstanceByKey);
return processInstance.getId();
}
/**
* 创建一个流程实例task(流程作业带业务主键)
*
* @param businessKey业务主键
* @return
* @throws Exception
*/
public static String createProcessInstance(ProcessEngine processEngine,String processInstanceByKey,String businessKey) throws Exception {
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processInstanceByKey,
businessKey);
return processInstance.getId();
}
public static void deploy(ProcessEngine processEngine,String name,String bpmn,String png) throws Exception {
// 获取仓库服务
RepositoryService repositoryService = processEngine.getRepositoryService();
// 创建发布配置对象
DeploymentBuilder builder = repositoryService.createDeployment();
// 设置发布信息
builder.name(name)// 添加部署规则的显示别名
.addClasspathResource(bpmn)// 添加规则文件
.addClasspathResource(png);// 添加规则图片
// 不添加会自动产生一个图片不推荐
// 完成发布
builder.deploy();
}
public void deployZIP(ProcessEngine processEngine,String name,String zip) throws Exception {
// 获取仓库服务
RepositoryService repositoryService = processEngine.getRepositoryService();
// 创建发布配置对象
DeploymentBuilder builder = repositoryService.createDeployment();
// 获得上传文件的输入流程
InputStream in = this.getClass().getClassLoader().getResourceAsStream(zip);
ZipInputStream zipInputStream = new ZipInputStream(in);
// 设置发布信息
builder.name(name)// 添加部署规则的显示别名
.addZipInputStream(zipInputStream);
// 完成发布
builder.deploy();
}
/**
* 流程操作
*
* @param taskId
* 作业Id
* @param content
* 审批内容
*/
public static void completeTaskDetail(ProcessEngine processEngine,String taskId, Object signal,String context) {
Map<String, Object> variables = new HashMap();
variables.put("context", context);
variables.put("signal", signal);
processEngine.getTaskService().complete(taskId, variables);
}
public static void main(String[] args) throws Exception {
// 部署
// deploy();
// 流程实例化
// String proc_inst_id=createProcessInstance();
// 当前用户作业
// findTask("aa");
// 处理
// completeTask("c8452602-08e4-11e7-8486-dc536091e18b","11");
//historyTaskList("c4c5bfff-08e0-11e7-a7bc-dc536091e18b");
}
/* public void setVariableValues() throws Exception {
TaskService taskService = processEngine.getTaskService();
String taskId = "";
String variableName = "";
taskService.setVariable("", "", "");
}
public void getVariableValues() throws Exception {
TaskService taskService = processEngine.getTaskService();
String taskId = "";
String variableName = "";
taskService.getVariable(taskId, variableName);
}
public void setRuVariableValues() throws Exception {
RuntimeService runtimeService = processEngine.getRuntimeService();
runtimeService.setVariable("", "", "");
String taskId = "";
String variableName = "";
}
public void setPVariableValues() throws Exception {
String processDefinitionKey = "varTest";
// 创建流程变量
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("请假人", "冯小刚");
// 在启动时设置流程变量
ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey,
variables);
System.out.println("pid:" + pi.getId());
}*/
}