任务委派只是任务人将当前的任务交给接收人进行审批,完成任务后又重新回到任务人身上。委派人查询任务与完成任务与正常的有区别。
/**
* 指定代办人
*/
@Test
public void delegateTask() {
String taskId = "2511";
String loginName = "0003";
activitiService.delegateTask(taskId, loginName);
}
/**
* 正在运行的任务表中被委派人办理任务后任务会回到委派人 ,历史任务表中也一样,只是多了一个人进行审批
*/
@Test
public void resolveTask() {
String taskId = "2511";
Map map = new HashMap<>();
activitiService.resolveTask(taskId ,map);
}
@Test
public void getTaskByOwner() {
String processDefinitionKey = "delegateTask";
String owner = "0003";
activitiService.getTaskByOwner(processDefinitionKey, owner);
}
/**
* 根据任务拥有者查询任务
*
* @param processDefinitionKey
* @param owner
* @return
*/
boolean getTaskByOwner(String processDefinitionKey, String owner);
/**
* 委派人完成任务
*
* @param taskId
* @param map
* @return
*/
boolean resolveTask(String taskId, Map map);
@Override
public boolean delegateTask(String taskId, String loginName) {
taskService.delegateTask(taskId, loginName);
return true;
}
@Override
public boolean getTaskByOwner(String processDefinitionKey, String owner) {
TaskQuery taskQuery = taskService.createTaskQuery();
// 任务列表
List list = taskQuery.processDefinitionKey(processDefinitionKey).taskOwner(owner).list();// 指定办理人
for (Task task : list) {
LOGGER.info("任务处理人【{}】", task.getAssignee());
LOGGER.info("流程名称【{}】", task.getName());
LOGGER.info("任务id【{}】", task.getId());
LOGGER.info("流程定义id【{}】", task.getProcessDefinitionId());
LOGGER.info("执行对象id【{}】", task.getExecutionId());
LOGGER.info("任务拥有者【{}】", task.getOwner());
}
return true;
}
@Override
public boolean resolveTask(String taskId, Map map) {
taskService.resolveTask(taskId, map);
return true;
}
任务转办是把任务交给别人进行处理
/**
* 任务转办,将任务交给其他人处理
*/
@Test
public void turnTask() {
String taskId = "7511";
String assignee = "0003";
activitiService.trunTask(taskId,assignee);
}
@Override
public boolean trunTask(String taskId, String assignee) {
taskService.setAssignee(taskId, assignee);
return true;
}
**会签:**当任务有多人审批,但只要部分人审批就可以通过,类似于投票,十个人中五个人投票通过即可通过,那么久需要使用会签
@Test
public void startActiviti() {
String processDefinitionKey = "counterSign";
String orgCode = "070067801";
String[] assigneeList = {"0001","0002","0003"};
// 设置变量
HashMap map = new HashMap<>();
map.put("createLoginName", "0003");
map.put("orgCode", orgCode);
//必须是List
map.put("assigneeList", Arrays.asList(assigneeList));
//map.put("signCount", 0);
activitiService.startActivitiAndFinsh(processDefinitionKey, map);
}
@Test
public void getValaier() {
String taskId = "35044";
Map variable = activitiService.getVariableByTaskId(taskId , false);
System.out.println("nrOfInstances 实例总数:"+variable.get("nrOfInstances"));
System.out.println("nrOfActiveInstances 当前还没有完成的实例个数:"+variable.get("nrOfActiveInstances"));
System.out.println("nrOfCompletedInstances 当前完成的实例个数数::"+variable.get("nrOfCompletedInstances"));
}
/**
* 挂起流程实例
*
* @param processInstanceId
* @return
*/
boolean suspendProcessInstanceById(String processInstanceId);
/**
* 删除流程实例
*
* @param processInstanceId
* @param reason
* @return
*/
boolean deleteProcessInstance(String processInstanceId, String reason);
/**
* 激活流程实例(激活挂起的流程实例)
*
* @param processInstanceId
* @return
*/
boolean activateProcessInstanceById(String processInstanceId);
/**
* 挂起流程定义
*
* @param processDefineKey
* @param cascade
* @return
*/
boolean suspendProcessDefineKey(String processDefineKey, boolean cascade);
/**
* 激活流程定义
*
* @param processDefineKey
* @param cascade
* @return
*/
boolean activateProcessDefinitionByKey(String processDefineKey, boolean cascade);
@Override
public boolean suspendProcessInstanceById(String processInstanceId) {
runtimeService.suspendProcessInstanceById(processInstanceId);
return true;
}
@Override
public boolean activateProcessInstanceById(String processInstanceId) {
runtimeService.activateProcessInstanceById(processInstanceId);
return true;
}
@Override
public boolean suspendProcessDefineKey(String processDefineKey, boolean cascade) {
repositoryService.suspendProcessDefinitionByKey(processDefineKey,cascade,new Date());
return true;
}
@Override
public boolean activateProcessDefinitionByKey(String processDefineKey,boolean cascade){
repositoryService.activateProcessDefinitionByKey(processDefineKey,cascade,new Date());
return true;
}
github地址(转载)
转自: https://blog.csdn.net/qq_32759985/article/details/80721130
activiti中关于TASK的数据库表
其中有两个字段:OWNER_,ASSIGNEE_
这两个字段的意义是:
ASSIGNEE_(受理人):task任务的受理人,就是执行TASK的人,这个又分两种情况(有值,NULL)
1). 有值的情况:XML流程里面定义的受理人,TASK会直接填入这个人;
2). NULL:XML没有指定受理人或者只指定了候选组;
没有值的时候,可以使用签收功能去指定受理人,就是候选组里面谁签收谁就成了受理人:
Task task=taskService.createTaskQuery().singleResult();
//签收
taskService.claim(task.getId(), "billy");
logger.info(taskService.createTaskQuery().singleResult().getAssignee());
//结果:billy
/*反签收*/
taskService.claim(task.getId(), null);
// 或者
taskService.unclaim(task.getId());
/*反签收*/
Task task=taskService.createTaskQuery().singleResult();
//委托
taskService.delegateTask(task.getId(), "cc");
logger.info(taskService.createTaskQuery().singleResult().getOwner());
logger.info(taskService.createTaskQuery().singleResult().getAssignee());
//结果:owner是Billy,assignee是cc
package com.studio.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;
import org.activiti.engine.impl.pvm.process.TransitionImpl;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
public class ActivitiUtil {
private static ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
private static IdentityService identityService = processEngine.getIdentityService();
private static RuntimeService runtimeService = processEngine.getRuntimeService();
private static TaskService taskService = processEngine.getTaskService();
private static RepositoryService repositoryService = processEngine.getRepositoryService();
private static HistoryService historyService = processEngine.getHistoryService();
public static void TaskRollBack(String taskId){
try {
Map variables;
// 取得当前任务
HistoricTaskInstance currTask = historyService
.createHistoricTaskInstanceQuery().taskId(taskId)
.singleResult();
// 取得流程实例
ProcessInstance instance = runtimeService
.createProcessInstanceQuery()
.processInstanceId(currTask.getProcessInstanceId())
.singleResult();
if (instance == null) {
//流程结束
}
variables = instance.getProcessVariables();
// 取得流程定义
ProcessDefinitionEntity definition = (ProcessDefinitionEntity) (processEngine.getRepositoryService().getProcessDefinition(currTask
.getProcessDefinitionId()));
if (definition == null) {
//log.error("流程定义未找到");
return ;
}
// 取得上一步活动
ActivityImpl currActivity = ((ProcessDefinitionImpl) definition)
.findActivity(currTask.getTaskDefinitionKey());
List nextTransitionList = currActivity
.getIncomingTransitions();
// 清除当前活动的出口
List oriPvmTransitionList = new ArrayList();
List pvmTransitionList = currActivity
.getOutgoingTransitions();
for (PvmTransition pvmTransition : pvmTransitionList) {
oriPvmTransitionList.add(pvmTransition);
}
pvmTransitionList.clear();
// 建立新出口
List newTransitions = new ArrayList();
for (PvmTransition nextTransition : nextTransitionList) {
PvmActivity nextActivity = nextTransition.getSource();
ActivityImpl nextActivityImpl = ((ProcessDefinitionImpl) definition)
.findActivity(nextActivity.getId());
TransitionImpl newTransition = currActivity
.createOutgoingTransition();
newTransition.setDestination(nextActivityImpl);
newTransitions.add(newTransition);
}
// 完成任务
List tasks = taskService.createTaskQuery()
.processInstanceId(instance.getId())
.taskDefinitionKey(currTask.getTaskDefinitionKey()).list();
for (Task task : tasks) {
taskService.complete(task.getId(), variables);
historyService.deleteHistoricTaskInstance(task.getId());
}
// 恢复方向
for (TransitionImpl transitionImpl : newTransitions) {
currActivity.getOutgoingTransitions().remove(transitionImpl);
}
for (PvmTransition pvmTransition : oriPvmTransitionList) {
pvmTransitionList.add(pvmTransition);
}
return ;
} catch (Exception e) {
return ;
}
}
}