<?xml version="1.0" encoding="UTF-8"?>
<process name="historyPT" xmlns="http://jbpm.org/4.4/jpdl">
<start g="244,63,48,48" name="start1">
<transition g="-59,-17" name="to state1" to="state1"/>
</start>
<task assignee="#{user}" g="225,160,92,52" name="task1">
<transition g="-89,-17" name="to secondState" to="secondState"/>
</task>
<task assignee="#{user}" g="228,373,92,52" name="task2">
<transition g="-59,-17" name="to state2" to="state2"/>
</task>
<end g="251,481,48,48" name="end1"/>
<state g="444,126,92,52" name="state1">
<transition g="-53,-17" name="to task1" to="task1"/>
</state>
<state g="446,283,92,52" name="secondState">
<transition g="-53,-17" name="to task2" to="task2"/>
</state>
<state g="434,452,92,52" name="state2">
<transition g="-47,-17" name="to end1" to="end1"/>
</state>
</process>
package ex.primary.unitTest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import junit.framework.TestCase;
import org.jbpm.api.*;
import org.jbpm.api.history.HistoryProcessInstance;
import org.jbpm.api.history.HistoryProcessInstanceQuery;
import org.jbpm.api.history.HistoryTask;
import org.jbpm.api.task.Task;
import org.jbpm.test.JbpmTestCase;
public class HistoryProcessTest extends TestCase {
String deploymentId;
Map<String, Object> varMap;
public void testHistoryProcess(){
//ProcessEngine是jBPM 所有Service API 之源 所有的Service API都从ProcessEngine中获得
ProcessEngine processService = Configuration.getProcessEngine();
//流程资源服务接口 、提供流程定义的部署、查询和删除。
RepositoryService repositoryService = processService.getRepositoryService();
//流程执行服务接口 提供启动流程实例、执行流程推进、设置流程变量。
ExecutionService executionService = processService.getExecutionService();
//人工服务管理接口 提供对任务的创建、提交、查询、保存、删除。
TaskService taskService = processService.getTaskService();
//历史任务
HistoryService historyService = processService.getHistoryService();
ProcessDefinition processDefinition = null;
ProcessInstance processInstance = null;
Task currentTask = null;
//部署流程定义
deploymentId = repositoryService.createDeployment().addResourceFromClasspath("ex/primary/unitTest/historyPT.jpdl.xml").deploy();
System.out.println(deploymentId + "流程定义ID");
//查询所有的流程定义
List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().list();
for (ProcessDefinition pd : processDefinitionList) {
processDefinition = pd;
System.out.println(pd.getId() + "-" + pd.getDeploymentId() + "-" + pd.getName() + "-" + pd.getKey() + "-" + pd.toString() + "-" + pd.getDescription());
}
//启动流程
varMap = new HashMap<String, Object>();
varMap.put("user", "zhangsan");
processInstance = executionService.startProcessInstanceById(processDefinition.getId(), varMap);
System.out.println(processInstance.getId() + "-" + processInstance.getKey() + "-" + processInstance.getName() + "-" + processInstance.toString());
List<ProcessInstance> piList = executionService.createProcessInstanceQuery().list();
for (ProcessInstance pi : piList){
processInstance = pi;
System.out.println(pi.getId() + "-" + pi.getKey() + "-" + pi.getName() + "-" + pi.toString());
}
//判断停留位置
System.out.println("是否位于state1节点:"+processInstance.isActive("state1"));
//推进流程
processInstance = executionService.signalExecutionById(processInstance.getId());
//判断停留位置
System.out.println("是否位于state1节点:"+processInstance.isActive("state1"));
//查询zhangsan 的所有任务
List<Task> taskListZ = taskService.findPersonalTasks("zhangsan");
System.out.println("zhangsan任务:");
for (Task task : taskListZ){
currentTask = task;
System.out.println(task.getActivityName() + "-" + task.getId() + "-" + task.getName() + "-" + task.getProgress());
}
System.out.println(currentTask.getActivityName() + "-" + currentTask.getId() + "-" + currentTask.getName() + "-" + currentTask.getProgress());
//判断停留位置
System.out.println("是否位于task1节点:"+processInstance.isActive("task1"));
//推进流程
varMap = new HashMap<String, Object>();
varMap.put("user", "wangwu");
taskService.completeTask(currentTask.getId(), "to secondState", varMap);
System.out.println("任务完成情况------>"+currentTask.STATE_COMPLETED);
//使用执行服务: 从持久化层中获取“最新”的流程实例对象
processInstance = executionService.findProcessInstanceById(processInstance.getId());
//判断停留位置
System.out.println("是否位于task1节点:"+processInstance.isActive("task1"));
List<Task> taskListZ_ = taskService.findPersonalTasks("zhangsan");
System.out.println("zhangsan任务:" + taskListZ_.size());
List<HistoryTask> htListZ = historyService.createHistoryTaskQuery().taskId(currentTask.getId()).list();
System.out.println("task历史信息");
System.out.println(htListZ.size());
for (HistoryTask hpi : htListZ) {
System.out.println(hpi.getAssignee() + "-" + hpi.getId() + "-" + hpi.getState() + "-" + hpi.toString());
}
Set<String> strSet = processInstance.findActiveActivityNames();
System.out.println("所有state");
for (String str : strSet){
System.out.println(str);
}
System.out.println("是否位于secondState节点:"+processInstance.isActive("secondState"));
//推进流程
processInstance = executionService.signalExecutionById(processInstance.getId());
System.out.println("是否位于secondState节点:"+processInstance.isActive("secondState"));
System.out.println("判断流程是否结束:"+processInstance.isEnded());
List<Task> _taskListW = taskService.findPersonalTasks("wangwu");
System.out.println("wangwu任务");
for (Task task : _taskListW){
currentTask = task;
System.out.println(task.getActivityName() + "-" + task.getId() + "-" + task.getName() + "-" + task.getProgress());
}
System.out.println("是否位于task2节点:"+processInstance.isActive("task2"));
taskService.completeTask(currentTask.getId());
System.out.println("任务完成情况------>"+currentTask.STATE_COMPLETED);
//使用执行服务: 从持久化层中获取“最新”的流程实例对象
processInstance = executionService.findProcessInstanceById(processInstance.getId());
System.out.println("是否位于task2节点:"+processInstance.isActive("task2"));
System.out.println("判断流程是否结束:"+processInstance.isEnded());
// List<HistoryProcessInstance> hpiList = historyService.createHistoryProcessInstanceQuery().processDefinitionId(processDefinition.getId()).orderAsc(HistoryProcessInstanceQuery.PROPERTY_STARTTIME).list();
// System.out.println("历史信息");
// for (HistoryProcessInstance hpi : hpiList) {
// System.out.println(hpi.getKey() + "-" + hpi.getProcessDefinitionId() + "-" + hpi.getProcessInstanceId() + "-" + hpi.getState());
// }
System.out.println("是否位于state2节点:"+processInstance.isActive("state2"));
//推进流程
processInstance = executionService.signalExecutionById(processInstance.getId());
System.out.println("是否位于state2节点:"+processInstance.isActive("state2"));
System.out.println("判断流程是否结束:"+processInstance.isEnded());
List<HistoryTask> htList = historyService.createHistoryTaskQuery().taskId(currentTask.getId()).list();
System.out.println("task历史信息");
System.out.println(htList.size());
for (HistoryTask hpi : htList) {
System.out.println(hpi.getAssignee() + "-" + hpi.getId() + "-" + hpi.getState() + "-" + hpi.toString());
}
List<HistoryProcessInstance> hpiList = historyService.createHistoryProcessInstanceQuery().processDefinitionId(processDefinition.getId()).orderAsc(HistoryProcessInstanceQuery.PROPERTY_STARTTIME).list();
System.out.println("历史信息");
System.out.println(hpiList.size());
for (HistoryProcessInstance hpi : hpiList) {
System.out.println(hpi.getKey() + "-" + hpi.getProcessDefinitionId() + "-" + hpi.getProcessInstanceId() + "-" + hpi.getState());
}
List<ProcessInstance> _piList = executionService.createProcessInstanceQuery().list();
System.out.println(_piList.size());
}
}