Activiti 拓展用户行为类实现人员节点多元化

用户行为类做了些什么?

Activiti 用户行为类支持使用用户id,组织id和表达式指定任务节点的处理人员,使用表达式可以指定调用Spring容器中某个对象的某一个方法指定人员,有跳过设置还可以跳过任务节点

用户行为类拓展点

  • 添加更加强大的groovy脚本支持
  • 添加处理人员信息拓展,使其支持动态脚本指定,方便接入自己的用户系统
  • 添加候选人处理逻辑在候选人丢失的情况下指定给管理员

流程执行顺序

流程启动 -> 执行开始节点行为类 -> 执行第一个任务行为类 -> 完成节点

用户任务的三种监听器

  • TASK_ASSIGNED 任务被指派 发生在TaskEntityManagerImpl中,此时任务已经入库
  • TASK_CREATED 任务被创建 发生在UserTaskActivityBehavior中,此时任务人员节点已经设置完毕
  • TASK_COMPLETED 任务被完成 TaskService.complete() 方法触发,此时会进入下一个节点,执行下一个节点的行为类

用户行为类源码

public class UserTaskActivityBehavior extends TaskActivityBehavior {
    private static final long serialVersionUID = 1L;
    private static final Logger LOGGER = LoggerFactory.getLogger(UserTaskActivityBehavior.class);
    protected UserTask userTask; //这个userTask 对应的是流程文档的用户节点,可通过此对象获取拓展元素

    public UserTaskActivityBehavior(UserTask userTask) {
        this.userTask = userTask;
    }

    public void execute(DelegateExecution execution) {
        CommandContext commandContext = Context.getCommandContext();
        TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
        TaskEntity task = (TaskEntity)taskEntityManager.create();
        task.setExecution((ExecutionEntity)execution);
        task.setTaskDefinitionKey(this.userTask.getId());
        String activeTaskName = null;
        String activeTaskDescription = null;
        String activeTaskDueDate = null;
        String activeTaskPriority = null;
        String activeTaskCategory = null;
        String activeTaskFormKey = null;
        String activeTaskSkipExpression = null;
        String activeTaskAssignee = null;
        String activeTaskOwner = null;
        List<String> activeTaskCandidateUsers = null;
        List<String> activeTaskCandidateGroups = null;
        ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
        //获取表达式管理器
        ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager();
        ObjectNode taskElementProperties;
        if (Context.getProcessEngineConfiguration().isEnableProcessDefinitionInfoCache()) {
            taskElementProperties = Context.getBpmnOverrideElementProperties(this.userTask.getId(), execution.getProcessDefinitionId());
            activeTaskName = this.getActiveValue(this.userTask.getName(), "userTaskName", taskElementProperties);
            activeTaskDescription = this.getActiveValue(this.userTask.getDocumentation(), "userTaskDescription", taskElementProperties);
            activeTaskDueDate = this.getActiveValue(this.userTask.getDueDate(), "userTaskDueDate", taskElementProperties);
            activeTaskPriority = this.getActiveValue(this.userTask.getPriority(), "userTaskPriority", taskElementProperties);
            activeTaskCategory = this.getActiveValue(this.userTask.getCategory(), "userTaskCategory", taskElementProperties);
            activeTaskFormKey = this.getActiveValue(this.userTask.getFormKey(), "userTaskFormKey", taskElementProperties);
            activeTaskSkipExpression = this.getActiveValue(this.userTask.getSkipExpression(), "taskSkipExpression", taskElementProperties);
            activeTaskAssignee = this.getActiveValue(this.userTask.getAssignee(), "userTaskAssignee", taskElementProperties);
            activeTaskOwner = this.getActiveValue(this.userTask.getOwner(), "userTaskOwner", taskElementProperties);
            activeTaskCandidateUsers = this.getActiveValueList(this.userTask.getCandidateUsers(), "userTaskCandidateUsers", taskElementProperties);
            activeTaskCandidateGroups = this.getActiveValueList(this.userTask.getCandidateGroups(), "userTaskCandidateGroups", taskElementProperties);
        } else {
            activeTaskName = this.userTask.getName();
            activeTaskDescription = this.userTask.getDocumentation();
            activeTaskDueDate = this.userTask.getDueDate();
            activeTaskPriority = this.userTask.getPriority();
            activeTaskCategory = this.userTask.getCategory();
            activeTaskFormKey = this.userTask.getFormKey();
            activeTaskSkipExpression = this.userTask.getSkipExpression();
            activeTaskAssignee = this.userTask.getAssignee();
            activeTaskOwner = this.userTask.getOwner();
            activeTaskCandidateUsers = this.userTask.getCandidateUsers();
            activeTaskCandidateGroups = this.userTask.getCandidateGroups();
        }

        String description;
        if (StringUtils.isNotEmpty(activeTaskName)) {
            taskElementProperties = null;

            try {
                description = (String)expressionManager.createExpression(activeTaskName).getValue(execution);
            } catch (ActivitiException var23) {
                description = activeTaskName;
                LOGGER.warn("property not found in task name expression " + var23.getMessage());
            }

            task.setName(description);
        }

        if (StringUtils.isNotEmpty(activeTaskDescription)) {
            taskElementProperties = null;

            try {
                description = (String)expressionManager.createExpression(activeTaskDescription).getValue(execution);
            } catch (ActivitiException var22) {
                description = activeTaskDescription;
                LOGGER.warn("property not found in task description expression " + var22.getMessage());
            }

            task.setDescription(description);
        }

        Object formKey;
        if (StringUtils.isNotEmpty(activeTaskDueDate)) {
            formKey = expressionManager.createExpression(activeTaskDueDate).getValue(execution);
            if (formKey != null) {
                if (formKey instanceof Date) {
                    task.setDueDate((Date)formKey);
                } else {
                    if (!(formKey instanceof String)) {
                        throw new ActivitiIllegalArgumentException("Due date expression does not resolve to a Date or Date string: " + activeTaskDueDate);
                    }

                    String businessCalendarName = null;
                    if (StringUtils.isNotEmpty(this.userTask.getBusinessCalendarName())) {
                        businessCalendarName = expressionManager.createExpression(this.userTask.getBusinessCalendarName()).getValue(execution).toString();
                    } else {
                        businessCalendarName = "dueDate";
                    }

                    BusinessCalendar businessCalendar = Context.getProcessEngineConfiguration().getBusinessCalendarManager().getBusinessCalendar(businessCalendarName);
                    task.setDueDate(businessCalendar.resolveDuedate((String)formKey));
                }
            }
        }

        if (StringUtils.isNotEmpty(activeTaskPriority)) {
            formKey = expressionManager.createExpression(activeTaskPriority).getValue(execution);
            if (formKey != null) {
                if (formKey instanceof String) {
                    try {
                        task.setPriority(Integer.valueOf((String)formKey));
                    } catch (NumberFormatException var21) {
                        throw new ActivitiIllegalArgumentException("Priority does not resolve to a number: " + formKey, var21);
                    }
                } else {
                    if (!(formKey instanceof Number)) {
                        throw new ActivitiIllegalArgumentException("Priority expression does not resolve to a number: " + activeTaskPriority);
                    }

                    task.setPriority(((Number)formKey).intValue());
                }
            }
        }

        if (StringUtils.isNotEmpty(activeTaskCategory)) {
            formKey = expressionManager.createExpression(activeTaskCategory).getValue(execution);
            if (formKey != null) {
                if (!(formKey instanceof String)) {
                    throw new ActivitiIllegalArgumentException("Category expression does not resolve to a string: " + activeTaskCategory);
                }

                task.setCategory((String)formKey);
            }
        }

        if (StringUtils.isNotEmpty(activeTaskFormKey)) {
            formKey = expressionManager.createExpression(activeTaskFormKey).getValue(execution);
            if (formKey != null) {
                if (!(formKey instanceof String)) {
                    throw new ActivitiIllegalArgumentException("FormKey expression does not resolve to a string: " + activeTaskFormKey);
                }

                task.setFormKey((String)formKey);
            }
        }

        taskEntityManager.insert(task, (ExecutionEntity)execution);
        //是否跳过此节点标记
        boolean skipUserTask = false;
        if (StringUtils.isNotEmpty(activeTaskSkipExpression)) {
            Expression skipExpression = expressionManager.createExpression(activeTaskSkipExpression);
            skipUserTask = SkipExpressionUtil.isSkipExpressionEnabled(execution, skipExpression) && SkipExpressionUtil.shouldSkipFlowElement(execution, skipExpression);
        }

        if (!skipUserTask) {
        	// 计算并设置任务节点的候选人
            this.handleAssignments(taskEntityManager, activeTaskAssignee, activeTaskOwner, activeTaskCandidateUsers, activeTaskCandidateGroups, task, expressionManager, execution);
        }

		//广播 task create 事件
        processEngineConfiguration.getListenerNotificationHelper().executeTaskListeners(task, "create");
        if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
          Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_CREATED, task));
        }

        if (skipUserTask) {
        	//如果选择跳过这个任务就会删除这个任务节点并执行level()方法进入下一个节点
            taskEntityManager.deleteTask(task, (String)null, false, false);
            //调用父类的leave方法 有两种情况一种是普通节点 和 多实例节点跳转
            this.leave(execution);
        }

    }

    //设置任务的处理人,候选人,候选组等信息
    protected void handleAssignments(TaskEntityManager taskEntityManager, String assignee, String owner, List<String> candidateUsers, List<String> candidateGroups, TaskEntity task, ExpressionManager expressionManager, DelegateExecution execution) {
        Object ownerExpressionValue;
        String customGroupIdentityLinkType;
        if (StringUtils.isNotEmpty(assignee)) {
            ownerExpressionValue = expressionManager.createExpression(assignee).getValue(execution);
            customGroupIdentityLinkType = null;
            if (ownerExpressionValue != null) {
                customGroupIdentityLinkType = ownerExpressionValue.toString();
            }

            taskEntityManager.changeTaskAssignee(task, customGroupIdentityLinkType);
        }

        if (StringUtils.isNotEmpty(owner)) {
            ownerExpressionValue = expressionManager.createExpression(owner).getValue(execution);
            customGroupIdentityLinkType = null;
            if (ownerExpressionValue != null) {
                customGroupIdentityLinkType = ownerExpressionValue.toString();
            }

            taskEntityManager.changeTaskOwner(task, customGroupIdentityLinkType);
        }

        Expression userIdExpr;
        Object value;
        List candidates;
        Iterator var18;
        if (candidateGroups != null && !candidateGroups.isEmpty()) {
            var18 = candidateGroups.iterator();

            while(var18.hasNext()) {
                customGroupIdentityLinkType = (String)var18.next();
                userIdExpr = expressionManager.createExpression(customGroupIdentityLinkType);
                value = userIdExpr.getValue(execution);
                if (value instanceof String) {
                    candidates = this.extractCandidates((String)value);
                    task.addCandidateGroups(candidates);
                } else {
                    if (!(value instanceof Collection)) {
                        throw new ActivitiIllegalArgumentException("Expression did not resolve to a string or collection of strings");
                    }

                    task.addCandidateGroups((Collection)value);
                }
            }
        }

        if (candidateUsers != null && !candidateUsers.isEmpty()) {
            var18 = candidateUsers.iterator();

            while(var18.hasNext()) {
                customGroupIdentityLinkType = (String)var18.next();
                userIdExpr = expressionManager.createExpression(customGroupIdentityLinkType);
                value = userIdExpr.getValue(execution);
                if (value instanceof String) {
                    candidates = this.extractCandidates((String)value);
                    task.addCandidateUsers(candidates);
                } else {
                    if (!(value instanceof Collection)) {
                        throw new ActivitiException("Expression did not resolve to a string or collection of strings");
                    }

                    task.addCandidateUsers((Collection)value);
                }
            }
        }

        Object value;
        Iterator groupIdSet;
        Iterator var16;
        String groupId;
        Iterator var19;
        String groupIdentityLink;
        Expression idExpression;
        List groupIds;
        if (this.userTask.getCustomUserIdentityLinks() != null && !this.userTask.getCustomUserIdentityLinks().isEmpty()) {
            var18 = this.userTask.getCustomUserIdentityLinks().keySet().iterator();

            label135:
            while(var18.hasNext()) {
                customGroupIdentityLinkType = (String)var18.next();
                var19 = ((Set)this.userTask.getCustomUserIdentityLinks().get(customGroupIdentityLinkType)).iterator();

                while(true) {
                    while(true) {
                        if (!var19.hasNext()) {
                            continue label135;
                        }

                        groupIdentityLink = (String)var19.next();
                        idExpression = expressionManager.createExpression(groupIdentityLink);
                        value = idExpression.getValue(execution);
                        if (value instanceof String) {
                            groupIds = this.extractCandidates((String)value);
                            var16 = groupIds.iterator();

                            while(var16.hasNext()) {
                                groupId = (String)var16.next();
                                task.addUserIdentityLink(groupId, customGroupIdentityLinkType);
                            }
                        } else {
                            if (!(value instanceof Collection)) {
                                throw new ActivitiException("Expression did not resolve to a string or collection of strings");
                            }

                            groupIdSet = ((Collection)value).iterator();

                            while(groupIdSet.hasNext()) {
                                task.addUserIdentityLink((String)groupIdSet.next(), customGroupIdentityLinkType);
                            }
                        }
                    }
                }
            }
        }

        if (this.userTask.getCustomGroupIdentityLinks() != null && !this.userTask.getCustomGroupIdentityLinks().isEmpty()) {
            var18 = this.userTask.getCustomGroupIdentityLinks().keySet().iterator();

            label108:
            while(var18.hasNext()) {
                customGroupIdentityLinkType = (String)var18.next();
                var19 = ((Set)this.userTask.getCustomGroupIdentityLinks().get(customGroupIdentityLinkType)).iterator();

                while(true) {
                    while(true) {
                        if (!var19.hasNext()) {
                            continue label108;
                        }

                        groupIdentityLink = (String)var19.next();
                        idExpression = expressionManager.createExpression(groupIdentityLink);
                        value = idExpression.getValue(execution);
                        if (value instanceof String) {
                            groupIds = this.extractCandidates((String)value);
                            var16 = groupIds.iterator();

                            while(var16.hasNext()) {
                                groupId = (String)var16.next();
                                task.addGroupIdentityLink(groupId, customGroupIdentityLinkType);
                            }
                        } else {
                            if (!(value instanceof Collection)) {
                                throw new ActivitiException("Expression did not resolve to a string or collection of strings");
                            }

                            groupIdSet = ((Collection)value).iterator();

                            while(groupIdSet.hasNext()) {
                                task.addGroupIdentityLink((String)groupIdSet.next(), customGroupIdentityLinkType);
                            }
                        }
                    }
                }
            }
        }

    }

    protected List<String> extractCandidates(String str) {
        return Arrays.asList(str.split("[\\s]*,[\\s]*"));
    }
}

整合Groovy脚本

实现ExpressionManager

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值