/** 校验目标节点是否可以回退. */ public boolean validateTargetActivity(String targetActivityId) { JdbcTemplate jdbcTemplate = ApplicationContextHelper.getBean(JdbcTemplate.class); String historyTaskId = jdbcTemplate.queryForObject( "select id_ from ACT_HI_TASKINST where act_id_=? order by END_TIME_ desc", String.class, targetActivityId); // 先找到历史任务 HistoricTaskInstanceEntity historicTaskInstanceEntity = Context.getCommandContext() .getHistoricTaskInstanceEntityManager() .findHistoricTaskInstanceById(historyTaskId); // 再反向查找历史任务对应的历史节点 HistoricActivityInstanceEntity historicActivityInstanceEntity = getHistoricActivityInstanceEntity(historyTaskId); logger.info("historic activity instance is : {}", historicActivityInstanceEntity.getId()); Graph graph = new ActivitiHistoryGraphBuilder(historicTaskInstanceEntity.getProcessInstanceId()).build(); Node node = graph.findById(historicActivityInstanceEntity.getId()); if (!this.checkCouldRollback(node)) { logger.info("cannot rollback {}", taskId); return false; } return true; }
/** 判断跳过节点. */ public boolean isSkipActivity(String historyActivityId) { JdbcTemplate jdbcTemplate = ApplicationContextHelper.getBean(JdbcTemplate.class); String historyTaskId = jdbcTemplate.queryForObject( "select task_id_ from ACT_HI_ACTINST where id_=?", String.class, historyActivityId); HistoricTaskInstanceEntity historicTaskInstanceEntity = Context.getCommandContext() .getHistoricTaskInstanceEntityManager() .findHistoricTaskInstanceById(historyTaskId); String deleteReason = historicTaskInstanceEntity.getDeleteReason(); return "跳过".equals(deleteReason); }
/** * 撤销流程. * * @return 0-撤销成功 1-流程结束 2-下一结点已经通过,不能撤销 */ public Integer execute(CommandContext commandContext) { // 获得历史任务 HistoricTaskInstanceEntity historicTaskInstanceEntity = Context.getCommandContext() .getHistoricTaskInstanceEntityManager() .findHistoricTaskInstanceById(historyTaskId); // 获得历史节点 HistoricActivityInstanceEntity historicActivityInstanceEntity = getHistoricActivityInstanceEntity(historyTaskId); Graph graph = new ActivitiHistoryGraphBuilder(historicTaskInstanceEntity.getProcessInstanceId()).build(); Node node = graph.findById(historicActivityInstanceEntity.getId()); if (!checkCouldWithdraw(node)) { logger.info("cannot withdraw {}", historyTaskId); return 2; } // 删除所有活动中的task this.deleteActiveTasks(historicTaskInstanceEntity.getProcessInstanceId()); // 获得期望撤销的节点后面的所有节点历史 List<String> historyNodeIds = new ArrayList<String>(); this.collectNodes(node, historyNodeIds); this.deleteHistoryActivities(historyNodeIds); // 恢复期望撤销的任务和历史 this.processHistoryTask(historicTaskInstanceEntity, historicActivityInstanceEntity); logger.info("activiti is withdraw {}", historicTaskInstanceEntity.getName()); return 0; }
public HumanTaskDTO createHumanTask( DelegateTask delegateTask, HistoricTaskInstanceEntity historicTaskInstanceEntity) throws Exception { HumanTaskConnector humanTaskConnector = ApplicationContextHelper.getBean(HumanTaskConnector.class); HumanTaskDTO humanTaskDto = new HumanTaskBuilder().setDelegateTask(delegateTask).build(); if ("发起流程".equals(historicTaskInstanceEntity.getDeleteReason())) { humanTaskDto.setCatalog(HumanTaskConstants.CATALOG_START); } HistoricProcessInstance historicProcessInstance = Context.getCommandContext() .getHistoricProcessInstanceEntityManager() .findHistoricProcessInstance(delegateTask.getProcessInstanceId()); humanTaskDto.setProcessStarter(historicProcessInstance.getStartUserId()); humanTaskDto = humanTaskConnector.saveHumanTask(humanTaskDto); return humanTaskDto; }
/** * 退回流程. * * @return true success, false failure */ public Boolean execute(CommandContext commandContext) { // 初始化当前的起点信息 this.initSource(); // 找到回退的目标节点 String targetActivityId = this.findTargetActivityId(); if (targetActivityId == null) { logger.info("cannot find targetActivity for : {}", this.taskId); return Boolean.FALSE; } /* * // 尝试查找最近的上游userTask String historyTaskId = this.findNearestUserTask(); * logger.info("nearest history user task is : {}", historyTaskId); * * if (historyTaskId == null) { logger.info("cannot rollback {}", taskId); * * return "activity"; } */ // 校验这个节点是否可以回退 boolean isValid = this.validateTargetActivity(targetActivityId); if (!isValid) { logger.info("cannot rollback for : {} to {}", this.taskId, targetActivityId); return Boolean.FALSE; } JdbcTemplate jdbcTemplate = ApplicationContextHelper.getBean(JdbcTemplate.class); String historyTaskId = jdbcTemplate.queryForObject( "select id_ from ACT_HI_TASKINST where act_id_=? order by END_TIME_ desc", String.class, targetActivityId); HistoricTaskInstanceEntity historicTaskInstanceEntity = Context.getCommandContext() .getHistoricTaskInstanceEntityManager() .findHistoricTaskInstanceById(historyTaskId); HistoricActivityInstanceEntity historicActivityInstanceEntity = getHistoricActivityInstanceEntity(historyTaskId); // 开始回退 if (this.isSameBranch(historicTaskInstanceEntity)) { // 如果退回的目标节点的executionId与当前task的executionId一样,说明是同一个分支 // 只删除当前分支的task this.deleteActiveTask(); } else { // 否则认为是从分支跳回主干 // 删除所有活动中的task this.deleteActiveTasks(historicTaskInstanceEntity.getProcessInstanceId()); // 获得期望退回的节点后面的所有节点历史 List<String> historyNodeIds = new ArrayList<String>(); Graph graph = new ActivitiHistoryGraphBuilder(historicTaskInstanceEntity.getProcessInstanceId()) .build(); Node node = graph.findById(historicActivityInstanceEntity.getId()); this.collectNodes(node, historyNodeIds); this.deleteHistoryActivities(historyNodeIds); } // 恢复期望退回的任务和历史 this.processHistoryTask(historicTaskInstanceEntity, historicActivityInstanceEntity); logger.info("activiti is rollback {}", historicTaskInstanceEntity.getName()); return Boolean.TRUE; }
/** 根据任务历史,创建待办任务. */ public void processHistoryTask( HistoricTaskInstanceEntity historicTaskInstanceEntity, HistoricActivityInstanceEntity historicActivityInstanceEntity) { /* * historicTaskInstanceEntity.setEndTime(null); historicTaskInstanceEntity.setDurationInMillis(null); * historicActivityInstanceEntity.setEndTime(null); historicActivityInstanceEntity.setDurationInMillis(null); */ // 创建新任务 TaskEntity task = TaskEntity.create(new Date()); task.setProcessDefinitionId(historicTaskInstanceEntity.getProcessDefinitionId()); // task.setId(historicTaskInstanceEntity.getId()); // task.setAssigneeWithoutCascade(historicTaskInstanceEntity.getAssignee()); task.setAssigneeWithoutCascade(this.userId); task.setParentTaskIdWithoutCascade(historicTaskInstanceEntity.getParentTaskId()); task.setNameWithoutCascade(historicTaskInstanceEntity.getName()); task.setTaskDefinitionKey(historicTaskInstanceEntity.getTaskDefinitionKey()); task.setExecutionId(historicTaskInstanceEntity.getExecutionId()); task.setPriority(historicTaskInstanceEntity.getPriority()); task.setProcessInstanceId(historicTaskInstanceEntity.getProcessInstanceId()); task.setExecutionId(historicTaskInstanceEntity.getExecutionId()); task.setDescriptionWithoutCascade(historicTaskInstanceEntity.getDescription()); task.setTenantId(historicTaskInstanceEntity.getTenantId()); Context.getCommandContext().getTaskEntityManager().insert(task); // 把流程指向任务对应的节点 ExecutionEntity executionEntity = Context.getCommandContext() .getExecutionEntityManager() .findExecutionById(historicTaskInstanceEntity.getExecutionId()); executionEntity.setActivity(getActivity(historicActivityInstanceEntity)); // 创建HistoricActivityInstance Context.getCommandContext().getHistoryManager().recordActivityStart(executionEntity); // 创建HistoricTaskInstance Context.getCommandContext().getHistoryManager().recordTaskCreated(task, executionEntity); Context.getCommandContext().getHistoryManager().recordTaskId(task); // 更新ACT_HI_ACTIVITY里的assignee字段 Context.getCommandContext().getHistoryManager().recordTaskAssignment(task); try { // humanTask this.createHumanTask(task, historicTaskInstanceEntity); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } }
public boolean isSameBranch(HistoricTaskInstanceEntity historicTaskInstanceEntity) { TaskEntity taskEntity = Context.getCommandContext().getTaskEntityManager().findTaskById(taskId); return taskEntity.getExecutionId().equals(historicTaskInstanceEntity.getExecutionId()); }
public void processHistoryTask( HistoricTaskInstanceEntity historicTaskInstanceEntity, HistoricActivityInstanceEntity historicActivityInstanceEntity) { historicTaskInstanceEntity.setEndTime(null); historicTaskInstanceEntity.setDurationInMillis(null); historicActivityInstanceEntity.setEndTime(null); historicActivityInstanceEntity.setDurationInMillis(null); TaskEntity task = TaskEntity.create(new Date()); task.setProcessDefinitionId(historicTaskInstanceEntity.getProcessDefinitionId()); task.setId(historicTaskInstanceEntity.getId()); task.setAssigneeWithoutCascade(historicTaskInstanceEntity.getAssignee()); task.setParentTaskIdWithoutCascade(historicTaskInstanceEntity.getParentTaskId()); task.setNameWithoutCascade(historicTaskInstanceEntity.getName()); task.setTaskDefinitionKey(historicTaskInstanceEntity.getTaskDefinitionKey()); task.setExecutionId(historicTaskInstanceEntity.getExecutionId()); task.setPriority(historicTaskInstanceEntity.getPriority()); task.setProcessInstanceId(historicTaskInstanceEntity.getProcessInstanceId()); task.setDescriptionWithoutCascade(historicTaskInstanceEntity.getDescription()); task.setTenantId(historicTaskInstanceEntity.getTenantId()); Context.getCommandContext().getTaskEntityManager().insert(task); try { HumanTaskConnector humanTaskConnector = ApplicationContextHelper.getBean(HumanTaskConnector.class); // humantask humanTaskConnector.removeHumanTaskByTaskId(historicTaskInstanceEntity.getId()); this.createHumanTask(task); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } ExecutionEntity executionEntity = Context.getCommandContext() .getExecutionEntityManager() .findExecutionById(historicTaskInstanceEntity.getExecutionId()); executionEntity.setActivity(getActivity(historicActivityInstanceEntity)); }