本篇主要是Activiti工作流框架的学习记录,以及对于该框架的基本使用和一些浅显的理解:
查询对象
DeploymentQuery:查询部署表act_re_deployment
ProcessDefinitionQuery:查询流程定义表act_re_procdef
ProcessInstanceQuery:查询流程实例表act_ru_execution
TaskQuery:查询任务表act_ru_task
几个和表对应的对象(接口)
Deployment:对应查询部署表act_re_deployment
ProcessDefinition:对应流程定义表act_re_procdef
ProcessInstance:对应流程实例表act_ru_execution
Task:对应任务表act_ru_task
几个Service对象
RepositoryService:部署流程定义,查询流程定义,查询流程定义文件名称等静态资源信息
RuntimeService:启动流程实例,查询流程实例,删除流程实例
TaskService:查询任务、办理任务
HistoryService:查询历史数据
IdentityService:操作用户表和组表
1)准备工作(在Activiti框架学习记录-01中已经有比较详细的描述):
// 提供了门面做任何操作都会需要使用到该对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 部署流程定义
@Test
public void test1() {
// 门面设计模式
// 流程定义
DeploymentBuilder deploymentBuilder =
processEngine.getRepositoryService().createDeployment();
deploymentBuilder.addClasspathResource("com/yq/activiti/history/history.bpmn");
deploymentBuilder.addClasspathResource("com/yq/activiti/history/history.png");
deploymentBuilder.deploy();
}
// 启动流程实例
@Test
public void test2() {
// 流程图ID
String processDefinitionKey = "lssj";
// 根据key启动实例会保证版本的最新的性质
processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey);
}
//办理任务
@Test
public void test3() {
String taskId = "904"; //任务ID
// 办理任务
processEngine.getTaskService().complete(taskId);
}
2)历史数据查询:
// 查询历史流程实例
@Test
public void test4() {
// act_hi_procinst
// 流程实例历史查询
HistoricProcessInstanceQuery query =
processEngine.getHistoryService().createHistoricProcessInstanceQuery();
List<HistoricProcessInstance> instances =
query.list();
for (HistoricProcessInstance historicProcessInstance : instances) {
System.out.println(historicProcessInstance.getId() +
"\t" + historicProcessInstance.getStartTime() +
"\t" + historicProcessInstance.getEndTime());
}
}
// 历史任务数据
@Test
public void test5() {
// act_hi_taskinst
// 历史任务数据查询
HistoricTaskInstanceQuery query =
processEngine.getHistoryService().createHistoricTaskInstanceQuery();
List<HistoricTaskInstance> instances =
query.list();
//在真正的系统排序是相当重要的一个亮点功能
query.orderByProcessInstanceId();
query.orderByHistoricTaskInstanceStartTime().asc();
for (HistoricTaskInstance historicTaskInstance : instances) {
System.out.println(historicTaskInstance.getId() +
"\t" + historicTaskInstance.getName() +
"\t" + historicTaskInstance.getStartTime() +
"\t" + historicTaskInstance.getEndTime());
}
}
@Test
public void test6() {
//历史活动实例查询对象,查询表:act_hi_actinst
HistoricActivityInstanceQuery query =
processEngine.getHistoryService().createHistoricActivityInstanceQuery();
query.orderByProcessInstanceId();
query.orderByHistoricActivityInstanceStartTime().asc();
List<HistoricActivityInstance> instances = query.list();
for (HistoricActivityInstance historicActivityInstance : instances) {
System.out.println(historicActivityInstance.getProcessInstanceId() +
"\t" + historicActivityInstance.getActivityName());
}
}
// 提供了门面做任何操作都会需要使用到该对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 部署流程定义
@Test
public void test1() {
// 门面设计模式
// 流程定义
DeploymentBuilder deploymentBuilder =
processEngine.getRepositoryService().createDeployment();
deploymentBuilder.addClasspathResource("com/yq/activiti/variable/variable.bpmn");
deploymentBuilder.addClasspathResource("com/yq/activiti/variable/variable.png");
deploymentBuilder.deploy();
}
2)流程数据设置
//设置流程变量的方法一:启动流程实例时候进行设置
@Test
public void test2() {
String processDefinitionKey = "qjlc";
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("key1", "vaule1");
variables.put("key2", 1500);
variables.put("username", "王虎");//从session中获取当前登录用户的名称
ProcessInstance instance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey,variables);
System.out.println(instance.getId());
}
//设置流程变量方法二:办理任务同时设置任务变量(比较合理的一个方法)
@Test
public void test3() {
String taskId = "1804";
Map<String,Object> variables = new HashMap<String, Object>();
variables.put("qjts", 5);
variables.put("qjyy", "不想上班");
variables.put("qjr", new User(1, "admin", 24));
processEngine.getTaskService().complete(taskId,variables);
}
//设置流程变量方法三:RuntiomeService的Set方法进行设置
@Test
public void test4() {
String executionId = "1301";//流程实例ID
String variableName = "k3";//流程变量名称
String variableValue= "v3";//流程变量value
//设置流程变量,设置流程变量的两种方式
processEngine.getRuntimeService().
setVariable(executionId, variableName, variableValue);
Map<String,Object> variables = new HashMap<String, Object>();
variables.put("k4", "v4");
processEngine.getRuntimeService().
setVariables(executionId, variables);
}
//设置流程变量方式四:TaskService的set方法设置
public void test5() {
String taskId = "1002"; //任务ID
//设置一个流程变量
processEngine.getTaskService().setVariable(taskId, "qjsj", 5);
}
2)流程数据获取
//获取流程变量方式一:使用RuntimeService的get方法获取
@Test
public void test6() {
String executionId = "1701";
//获取一个流程变量
User user = (User) processEngine.getRuntimeService().getVariable(executionId, "qjr");
System.out.println(user);
//获取多个流程变量
Map<String, Object> variables = processEngine.getRuntimeService().getVariables(executionId);
Set<String> keySet = variables.keySet();
for (String key : keySet) {
Object value = variables.get(key);
System.out.println(key + "-->" + value);
}
}
//获取流程变量方式二:使用TaskSErvice的get方式获取
@Test
public void test7() {
String taskId = "1905";
//获取一个流程变量
User user = (User) processEngine.getTaskService().getVariable(taskId, "qjr");
System.out.println(user);
//获取当前任务所在流程实例(801)范围内所有的流程变量
Map<String ,Object> variables = processEngine.getTaskService().getVariables(taskId);
Set<String> keySet = variables.keySet();
for (String key : keySet) {
Object value = variables.get(key);
System.out.println(key + "-->" + value);
}
}