assertNotNull(processInstanceId);
//暂停1000毫秒,用来验证order by
Thread.sleep(1000);
}
//流程实例查询
ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
//根据流程定义key查询
List<ProcessInstance> processInstances = processInstanceQuery.processDefinitionKey("Process_TaskServiceTest").list();
//验证是10条流程实例
assertEquals(10, processInstances.size());
//重置流程实例查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//流程实例key的集合
List<String> definitionKeys = new ArrayList<String>();
definitionKeys.add("Process_TaskServiceTest");
//根据key的集合查询实例
processInstances = processInstanceQuery.processDefinitionKey(definitionKeys).list();
//验证10条查询结果
assertEquals(10, processInstances.size());
//根据开始时间降序查询
processInstanceQuery.orderByStartTime().desc();
//获取第一页的0-5条结果
processInstances = processInstanceQuery.listPage(1, 5);
//获取第二页5-10条结果
List<ProcessInstance> tmpProcessInstances = processInstanceQuery.listPage(6, 10);
//取得第一条结果
Date firstStartTime = processInstances.get(0).getStartTime();
//验证比本页所有的时间都大,表示降序排序正确
for(int i = 1;i < processInstances.size();i++){
Date tmpStartTime = processInstances.get(i).getStartTime();
assertTrue(firstStartTime.after(tmpStartTime));
}
//验证比第二页所有的时间都大,表示降序排序正确
for(int i = 0;i<tmpProcessInstances.size();i++){
Date tmpStartTime = tmpProcessInstances.get(i).getStartTime();
assertTrue(firstStartTime.after(tmpStartTime));
}
//需要重置查询,因为order by 有叠加效果
processInstanceQuery = runtimeService.createProcessInstanceQuery();
processInstanceQuery.processDefinitionKey(definitionKeys);
//根据开始时间升序排序
processInstanceQuery.orderByStartTime().asc();
//获取第一页的0-5条结果
processInstances = processInstanceQuery.listPage(1, 5);
//获取第二页5-10条结果
tmpProcessInstances = processInstanceQuery.listPage(6, 10);
//取得第一条结果
firstStartTime = processInstances.get(0).getStartTime();
//验证比本页所有的时间都小,表示升序正确
for(int i = 1;i < processInstances.size();i++){
Date tmpStartTime = processInstances.get(i).getStartTime();
assertTrue(firstStartTime.before(tmpStartTime));
}
//验证比第二页所有的时间都小,表示升序正确
for(int i = 0;i<tmpProcessInstances.size();i++){
Date tmpStartTime = tmpProcessInstances.get(i).getStartTime();
assertTrue(firstStartTime.before(tmpStartTime));
}
//需要重置查询,因为order by 有叠加效果
processInstanceQuery = runtimeService.createProcessInstanceQuery();
processInstanceQuery.processDefinitionKey(definitionKeys);
//根据更新时间升序排序
processInstanceQuery.orderByUpdateTime().asc();
//获取第一页的0-5条结果
processInstances = processInstanceQuery.listPage(1, 5);
//获取第二页5-10条结果
tmpProcessInstances = processInstanceQuery.listPage(6, 10);
//取得第一条结果
firstStartTime = processInstances.get(0).getUpdateTime();
//验证比本页所有的时间都小,表示升序正确
for(int i = 1;i < processInstances.size();i++){
Date tmpStartTime = processInstances.get(i).getUpdateTime();
assertTrue(firstStartTime.before(tmpStartTime));
}
//验证比第二页所有的时间都小,表示升序正确
for(int i = 0;i<tmpProcessInstances.size();i++){
Date tmpStartTime = tmpProcessInstances.get(i).getUpdateTime();
assertTrue(firstStartTime.before(tmpStartTime));
}
//需要重置查询,因为order by 有叠加效果
processInstanceQuery = runtimeService.createProcessInstanceQuery();
processInstanceQuery.processDefinitionKey(definitionKeys);
//根据更新时间降序序排序
processInstanceQuery.orderByUpdateTime().desc();
//获取第一页的0-5条结果
processInstances = processInstanceQuery.listPage(1, 5);
//获取第二页5-10条结果
tmpProcessInstances = processInstanceQuery.listPage(6, 10);
//取得第一条结果
firstStartTime = processInstances.get(0).getUpdateTime();
//验证比本页所有的时间都小,表示降序正确
for(int i = 1;i < processInstances.size();i++){
Date tmpStartTime = processInstances.get(i).getUpdateTime();
assertTrue(firstStartTime.after(tmpStartTime));
}
//验证比第二页所有的时间都小,表示降序正确
for(int i = 0;i<tmpProcessInstances.size();i++){
Date tmpStartTime = tmpProcessInstances.get(i).getUpdateTime();
assertTrue(firstStartTime.after(tmpStartTime));
}
//验证小于开始时间的查询
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询开始时间小于某个时间的流程实例(时间在方法开始时定义)
processInstances = processInstanceQuery.processDefinitionKey("Process_TaskServiceTest").startTimeBefore(date).list();
//验证是否有4个
assertEquals(4, processInstances.size());
//验证大于开始时间的查询
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询开始时间大于某个时间的流程实例(时间在方法开始时定义)
processInstances = processInstanceQuery.processDefinitionKey("Process_TaskServiceTest").startTimeAfter(date).list();
//验证是否有6个
assertEquals(7, processInstances.size());
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询未结束的流程实例
processInstances = processInstanceQuery.processDefinitionKey("Process_TaskServiceTest").notEnd().list();
//验证是否有10个
assertEquals(10, processInstances.size());
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询已结束的流程实例
processInstances = processInstanceQuery.processDefinitionKey("Process_TaskServiceTest").isEnd().list();
//验证是否有0个
assertEquals(0, processInstances.size());
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询对应bizKey的流程实例
processInstances = processInstanceQuery.processDefinitionKey("Process_TaskServiceTest").processInstanceBusinessKey("BK_testStartProcessInstanceByKey").list();
//验证是否有10个
assertEquals(10, processInstances.size());
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询对应bizKey的流程实例
long count = processInstanceQuery.processDefinitionKey("Process_TaskServiceTest").processInstanceStatus(ProcessInstanceType.RUNNING).count();
//验证是否有10个
assertEquals(10, count);
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询对应bizKey的流程实例(方法2)
processInstances = processInstanceQuery.processInstanceBusinessKey("BK_testStartProcessInstanceByKey","Process_TaskServiceTest").list();
//验证是否有10个
assertEquals(10, processInstances.size());
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询某人发起的流程实例
processInstances = processInstanceQuery.processDefinitionKey("Process_TaskServiceTest").initiator("1200119390").list();
//验证是否有10个
assertEquals(10, processInstances.size());
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询流程定义名称like“TaskServiceTes”的流程实例
processInstances = processInstanceQuery.processDefinitionNameLike("TaskServiceTes").list();
//验证是否有10个
assertEquals(10, processInstances.size());
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询流程定义名称等于“TaskServiceTest”的流程实例
processInstances = processInstanceQuery.processDefinitionName("TaskServiceTest").list();
//验证是否有10个
assertEquals(10, processInstances.size());
String processInstanceId = processInstances.get(0).getId();
TaskQuery taskQuery = taskService.createTaskQuery();
taskQuery.processInstanceId(processInstanceId).taskNotEnd();
TaskInstance taskInstance = taskQuery.list().get(0);
Date begin = new Date();
//创建通用命令
ExpandTaskCommand expandTaskCommandGeneral=new ExpandTaskCommand();
//设置命令为处理
expandTaskCommandGeneral.setCommandType("general");
//设置命令的ID,需和节点上配置的按钮编号对应,会执行其中脚本
expandTaskCommandGeneral.setUserCommandId("Normal_1");
//设置命令的处理任务号
expandTaskCommandGeneral.setTaskId(taskInstance.getId());
//领取任务
taskService.expandTaskComplete(expandTaskCommandGeneral, null);
Date end = new Date();
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询流程定义名称等于“TaskServiceTest”的流程实例
processInstances = processInstanceQuery.processInstanceId(processInstanceId).list();
//验证是否有10个
assertTrue(processInstances.get(0).hasEnded());
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询流程定义名称等于“TaskServiceTest”的流程实例
processInstances = processInstanceQuery.processDefinitionName("TaskServiceTest").endTimeAfter(end).list();
assertEquals(1, processInstances.size());
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询流程定义名称等于“TaskServiceTest”的流程实例
processInstances = processInstanceQuery.processDefinitionName("TaskServiceTest").endTimeAfter(begin).list();
assertEquals(0, processInstances.size());
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询流程定义名称等于“TaskServiceTest”的流程实例
processInstances = processInstanceQuery.processDefinitionName("TaskServiceTest").endTimeBefore(begin).list();
assertEquals(1, processInstances.size());
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询流程定义名称等于“TaskServiceTest”的流程实例
processInstances = processInstanceQuery.processDefinitionName("TaskServiceTest").endTimeBefore(end).list();
assertEquals(0, processInstances.size());
//重置查询
processInstanceQuery = runtimeService.createProcessInstanceQuery();
//查询流程定义名称等于“TaskServiceTest”的流程实例
processInstances = processInstanceQuery.processDefinitionName("TaskServiceTest").endTimeOn(end).list();
assertEquals(0, processInstances.size());
}