最近刚接触activiti 用的6.x
网上资料太少 断点追流程看属性写了个加签的操作
目前测试的没啥问题 如有问题 请留言指教
上代码
完事之后三个表查查有没有新增数据 有就成了
多实例的才能复制,调用之前最好先判断下
protected String executionId; //execution 即流程执行线路,或者执行环境
protected List<String> userList; //要赋值的用户集合,也可以逗号隔开的字符串
protected FlowNode targetNode; //要复制的节点(多实例)
public AddTask(String executionId, List<String> userList, FlowNode targetNode) {
this.executionId = executionId;
this.userList = userList;
this.targetNode = targetNode;
}
@Override
public Void execute(CommandContext commandContext) {
RuntimeService runtimeService = commandContext
.getProcessEngineConfiguration().getRuntimeService();
TaskService taskService = commandContext
.getProcessEngineConfiguration().getTaskService();
List<Execution> list = runtimeService.createExecutionQuery()
.executionId(executionId).list();//executionid不会重复,查出其实就一个结果
for (Execution execution : list) {
ExecutionEntity ee = (ExecutionEntity) execution;
//下面这个是重点,如果是子流程需要获取两级parent,其他get一次就好
ExecutionEntity parent = ee.getParent().getParent();
//创建子线路
ExecutionEntity createExecution = commandContext.getExecutionEntityManager().createChildExecution(parent == null ? ee.getParent() : parent);
//下面这几个跟着设就行
createExecution.setActive(true);
createExecution.setConcurrent(false);
createExecution.setScope(false);
createExecution.setCurrentFlowElement(ee.getCurrentFlowElement());
createExecution.setProcessDefinitionId(ee.getProcessDefinitionId());
createExecution.setProcessDefinitionKey(ee.getProcessDefinitionKey());
//拿到id生成器
IdGenerator idGenerator = commandContext
.getProcessEngineConfiguration().getIdGenerator();
String nextId = idGenerator.getNextId();
//查询当前的task实例,有的属性flownode没有得用taskentity赋值
Task singleResult = taskService.createTaskQuery()
.executionId(executionId).singleResult();
TaskEntity t = (TaskEntity) singleResult;
//创建新的task
TaskEntity taskEntity = new TaskEntityImpl();
taskEntity.setCreateTime(new Date());
taskEntity.setProcessDefinitionId(t.getProcessDefinitionId());
taskEntity.setTaskDefinitionKey(targetNode.getId());
taskEntity.setProcessInstanceId(t.getProcessInstanceId());
//跟流程线路绑定
taskEntity.setExecutionId(createExecution.getId());
taskEntity.setExecution(createExecution);
taskEntity.setName(targetNode.getName());
//给id赋值
taskEntity.setId(nextId);
//等于0才会新建
taskEntity.setRevision(0);
//给task赋执行者,需要自己去领任务
taskEntity.addCandidateUsers(userList);
//存储task
taskService.saveTask(taskEntity);
//下面这些不用动,复制过去就行
int loopCounter = getLoopVariable(createExecution, "nrOfInstances");
int nrOfCompletedInstances = getLoopVariable(createExecution,
"nrOfActiveInstances");
setLoopVariable(createExecution, "nrOfInstances", loopCounter + 1, true);
setLoopVariable(createExecution, "nrOfActiveInstances",
nrOfCompletedInstances + 1, true);
}
return null;
}
protected void setLoopVariable(ExecutionEntity execution,
String variableName, Object value, boolean paretnt) {
DelegateExecution parent = execution.getParent();
parent.setVariableLocal(variableName, value);
}
protected Integer getLoopVariable(ExecutionEntity execution,
String variableName) {
Object value = execution.getVariableLocal(variableName);
DelegateExecution parent = execution.getParent();
while (value == null && parent != null) {
value = parent.getVariableLocal(variableName);
parent = parent.getParent();
}
return (Integer) (value != null ? value : 0);
}
//最后调用执行 这个哪用放哪 上面弄一个单独的类 复用
processEngine.getManagementService().executeCommand(new AddTask(task.getExecutionId(), userList, targetNode));
------------------------------------------------------------------------------------------------------------
!!重点是多实例,非多实例节点不要去加签