Activiti7工作流引擎:基础篇 基本查询

Posted vbirdbest

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activiti7工作流引擎:基础篇 基本查询相关的知识,希望对你有一定的参考价值。

查询流程定义

@Test
public void testQueryProcessDefinition() 
    List<ProcessDefinition> list = ProcessEngines.getDefaultProcessEngine()
            .getRepositoryService()
            .createProcessDefinitionQuery()
            .processDefinitionKey("helloworld-var")
            .list();

select 
	distinct RES.*
FROM ACT_RE_PROCDEF RES
WHERE RES.KEY_ = 'helloworld-var' 
order by RES.ID_ asc
LIMIT 2147483647 OFFSET 0;

查询正在进行的任务

@Test
public void testQueryRuTask() 
	// 查询哪个工作流下的哪个负责人的待办任务
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    List<Task> list = processEngine.getTaskService()
            .createTaskQuery()
            .processDefinitionKey("helloworld-var")
            .taskAssignee("zhangsan")
            .list();
    // [Task[id=2508, name=请假申请]]
    System.out.println(list);

select 
	distinct RES.*
FROM ACT_RU_TASK RES
INNER JOIN ACT_RE_PROCDEF D ON RES.PROC_DEF_ID_ = D.ID_
WHERE RES.ASSIGNEE_ = 'zhangsan' and D.KEY_ = 'helloworld-var' 
order by RES.ID_ asc
LIMIT 2147483647 OFFSET 0;

查询历史活动

@Test
public void queryHistoryActivity() 
    List<HistoricActivityInstance> activityInstanceList = ProcessEngines.getDefaultProcessEngine()
            .getHistoryService()
            .createHistoricActivityInstanceQuery()
            .processDefinitionId("helloworld-var:1:4")
            .list();

select 
	RES.*
FROM ACT_HI_ACTINST RES
WHERE RES.PROC_DEF_ID_ = 'helloworld-var:1:4' 
order by RES.ID_ asc
LIMIT 2147483647 OFFSET 0;

查询历史流程实例

@Test
public void testQueryHistoryProcessInstance() 
    ProcessEngines.getDefaultProcessEngine()
            .getHistoryService()
            .createHistoricProcessInstanceQuery()
            .processInstanceId("2501")
            .list();

select 
	distinct RES.* , 
	DEF.KEY_ as PROC_DEF_KEY_, 
	DEF.NAME_ as PROC_DEF_NAME_, 
	DEF.VERSION_ as PROC_DEF_VERSION_, 
	DEF.DEPLOYMENT_ID_ as DEPLOYMENT_ID_
FROM ACT_HI_PROCINST RES 
left outer join ACT_RE_PROCDEF DEF ON RES.PROC_DEF_ID_ = DEF.ID_
WHERE RES.PROC_INST_ID_ = '2501' 
order by RES.ID_ asc
LIMIT 2147483647 
OFFSET 0;

挂起和激活流程实例

流程实例挂起相当于暂停了,不会被继续执行,也不允许启动新的实例。主要是设置数据库字段挂起状态字段 SUSPENSION_STATE_ 挂起状态 2:挂起,1:激活。

挂起流程定义就是挂起该流程下的所有流程实例。

@Test
public void testSuspendProcessDefinition() 
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RepositoryService repositoryService = processEngine.getRepositoryService();
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
            .processDefinitionKey("helloworld-var")
            .singleResult();
    if (processDefinition.isSuspended()) 
        repositoryService.activateProcessDefinitionById(processDefinition.getId());
     else 
        repositoryService.suspendProcessDefinitionById(processDefinition.getId());
    

update ACT_RE_PROCDEF 
set 
	REV_ = 2, 
	SUSPENSION_STATE_ = 2
WHERE ID_ = 'helloworld-var:1:4' and REV_ = 1;

挂起单个流程实例:分别设置ACT_RU_TASKACT_RU_EXECUTION的SUSPENSION_STATE_ = 2

@Test
public void testSuspendedProcessInstance() 
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
            .processInstanceId("2501")
            .singleResult();
    boolean suspended = processInstance.isSuspended();
    if (suspended) 
        runtimeService.activateProcessInstanceById(processInstance.getId());
     else 
        runtimeService.suspendProcessInstanceById(processInstance.getId());
    

-- ACT_RU_TASK.SUSPENSION_STATE_ = 2
update ACT_RU_TASK 
SET 
	SUSPENSION_STATE_ = 2, 
WHERE ID_= '2508' and REV_ = 1;

-- ACT_RU_EXECUTION.SUSPENSION_STATE_ = 2
-- 流程实例
update ACT_RU_EXECUTION 
set 
	SUSPENSION_STATE_ = 2
WHERE ID_ = '2501' and REV_ = 1;

-- 该流程实例下的其它执行流
update ACT_RU_EXECUTION 
set 
	SUSPENSION_STATE_ = 2
WHERE ID_ = '2505' and REV_ = 1;

删除部署

删除部署会删除和此次部署相关的所有数据,包括资源表ACT_RE_PROCDEF和ACT_GE_BYTEARRAY。一般情况下很少删除。

/**
* ACT_RE_PROCDEF
* ACT_GE_BYTEARRAY
* ACT_RE_DEPLOYMENT
* ACT_RU_IDENTITYLINK
*/
@Test
public void deleteDeployment() 
    String deploymentId = "1";
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    processEngine.getRepositoryService()
            .deleteDeployment(deploymentId, true);


/**
* ACT_RE_DEPLOYMENT
* ACT_RE_PROCDEF
* ACT_GE_BYTEARRAY
* ACT_RU_EXECUTION
* ACT_RU_TASK
* ACT_RU_IDENTITYLINK
* ACT_RU_VARIABLE
* ACT_HI_COMMENT
* ACT_HI_PROCINST
* ACT_HI_TASKINST
* ACT_HI_IDENTITYLINK
* ACT_HI_ACTINST
* ACT_HI_VARINST
*/
@Test
public void deleteDeployment() 
    String deploymentId = "1";
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    // true:表示级联删除
    processEngine.getRepositoryService()
            .deleteDeployment(deploymentId, true);

获取.bpmn、.png文件

public void  saveBpmnFile() throws IOException 
    //        1、得到引擎
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    //        2、获取repositoryService
    RepositoryService repositoryService = processEngine.getRepositoryService();
    //        3、得到查询器:ProcessDefinitionQuery,设置查询条件,得到想要的流程定义
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
            .processDefinitionKey("helloworld-var")
            .singleResult();
    //        4、通过流程定义信息,得到部署ID
    String deploymentId = processDefinition.getDeploymentId();
    //        5、通过repositoryService的方法,实现读取图片信息和bpmn信息
    //        png图片的流
    InputStream pngInput = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName());
    //        bpmn文件的流
    InputStream bpmnInput = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());
    //        6、构造OutputStream流
    File file_png = new File("d:/helloworld-var.png");
    File file_bpmn = new File("d:/helloworld-var.bpmn");
    FileOutputStream bpmnOut = new FileOutputStream(file_bpmn);
    FileOutputStream pngOut = new FileOutputStream(file_png);
    //        7、输入流,输出流的转换
    IOUtils.copy(pngInput,pngOut);
    IOUtils.copy(bpmnInput,bpmnOut);
    //        8、关闭流
    pngOut.close();
    bpmnOut.close();
    pngInput.close();
    bpmnInput.close();

候选人CandidateUsers

候选人就是不指定任务节点的负责人,而是在实际审批的过程中由候选人自己将任务的负责人设置为自己或者候选人中的其他人,然后再由负责人去完成审批。

经理审批不指定Assignee复制人了,而是指定候选人列表。

@Test
public void startProcessInstance() 
    String businessKey = "666";

    Map<String, Object> variables = new HashMap<>();
    variables.put("creator", "zhangsan");
    // 指定候选人列表
    variables.put("pmCandidateUsers", "狗经理,猪经理");

    ProcessInstance processInstance = ProcessEngines.getDefaultProcessEngine()
            .getRuntimeService()
            .startProcessInstanceByKey("candidateUsers", businessKey, variables);


@Test
public void completeTask() 
    TaskService taskService = ProcessEngines.getDefaultProcessEngine().getTaskService();
    Task task = taskService
            .createTaskQuery()
            .processDefinitionKey("candidateUsers")
            .taskAssignee("zhangsan")
            .singleResult();
    taskService.complete(task.getId());

当发起人zhangsan完成自己的审批后到底下一个任务节点经理审批时,此时的ASSIGNEE_字段是空的。

/**
* 猪经理拾取任务,即将任务负责人设置为猪经理
*/
@Test
public void testTaskClaim() 
   String currentUserId = "猪经理";
   TaskService taskService = ProcessEngines.getDefaultProcessEngine().getTaskService();
   Task task = taskService.createTaskQuery()
           .processDefinitionKey("candidateUsers")
           .taskCandidateUser(currentUserId)
           .singleResult();
   // update ACT_RU_TASK SET ASSIGNEE_ = '猪经理' WHERE ID_= '5002' and REV_ = 1;
   // update ACT_HI_ACTINST set ASSIGNEE_ = '猪经理' WHERE ID_ = '5001';
   // update ACT_HI_TASKINST set ASSIGNEE_ = '猪经理' WHERE ID_ = '5002';
 WHERE ID_ = '5001';
 WHERE ID_= '5002' and REV_ = 1;
   taskService.claim(task.getId(), currentUserId);


// 如果当前任务的负责人是自己,而自己却不想审批,可以直接将任务负责人设置为null或者交接给其他候选人
@Test
public void testSetAssignee() 
    TaskService taskService = ProcessEngines.getDefaultProcessEngine().getTaskService();
    Task task = taskService
            .createTaskQuery()
            .processDefinitionKey("candidateUsers")
            .taskAssignee("猪经理")
            .singleResult();
    taskService.setAssignee(task.getId(), null);
    taskService.setAssignee(task.getId(), "狗经理");

update ACT_RU_TASK SET ASSIGNEE_ = null WHERE ID_= '5002' and REV_ = 2;
-- -----------------------------------------------------------------------------------------------
update ACT_HI_ACTINST set ASSIGNEE_ = null WHERE ID_ = '5001';
-- -----------------------------------------------------------------------------------------------
update ACT_HI_TASKINST set ASSIGNEE_ = null WHERE ID_ = '5002';

以上是关于Activiti7工作流引擎:基础篇 基本查询的主要内容,如果未能解决你的问题,请参考以下文章

Activiti7工作流引擎:基础篇 Annotation

Activiti7工作流引擎:基础篇 Annotation

Activiti7工作流引擎:基础篇 Hello World

Activiti7工作流引擎:基础篇 网关Gateway

Activiti7工作流引擎:基础篇 流程变量

Activiti7工作流引擎:基础篇 数据库表结构