示例#1
0
        protected internal override object Execute(ICommandContext commandContext, ITaskEntity task)
        {
            bool assignedToNoOne = false;

            if (IdentityLinkType.ASSIGNEE.Equals(identityType))
            {
                string assigneeUser = null;
                if (string.IsNullOrWhiteSpace(identityId) == false)
                {
                    //TODO: 考虑性能问题,暂时不要获取人员信息
                    //assigneeUser = AsyncHelper.RunSync(() => userService.GetUser(identityId))?.FullName;
                }
                commandContext.TaskEntityManager.ChangeTaskAssignee(task, identityId, assigneeUser);
                assignedToNoOne = identityId is null;
            }
            else if (IdentityLinkType.OWNER.Equals(identityType))
            {
                commandContext.TaskEntityManager.ChangeTaskOwner(task, identityId);
            }
            else if (IDENTITY_USER == identityIdType)
            {
                task.AddUserIdentityLink(identityId, identityType);
            }
            else if (IDENTITY_GROUP == identityIdType)
            {
                task.AddGroupIdentityLink(identityId, identityType);
            }

            bool forceNullUserId = false;

            if (assignedToNoOne)
            {
                // ACT-1317: Special handling when assignee is set to NULL, a
                // CommentEntity notifying of assignee-delete should be created
                forceNullUserId = true;
            }

            if (IDENTITY_USER == identityIdType)
            {
                commandContext.HistoryManager.CreateUserIdentityLinkComment(taskId, identityId, identityType, true, forceNullUserId);
            }
            else
            {
                commandContext.HistoryManager.CreateGroupIdentityLinkComment(taskId, identityId, identityType, true);
            }

            return(null);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="taskEntityManager"></param>
        /// <param name="assignee"></param>
        /// <param name="owner"></param>
        /// <param name="candidateUsers"></param>
        /// <param name="candidateGroups"></param>
        /// <param name="task"></param>
        /// <param name="expressionManager"></param>
        /// <param name="execution"></param>
        protected internal virtual void HandleAssignments(ITaskEntityManager taskEntityManager, string assignee, string owner, IList <string> candidateUsers, IList <string> candidateGroups, ITaskEntity task, ExpressionManager expressionManager, IExecutionEntity execution)
        {
            if (!string.IsNullOrWhiteSpace(assignee))
            {
                object assigneeExpressionValue = expressionManager.CreateExpression(assignee).GetValue(execution);
                string assigneeValue           = null;
                if (assigneeExpressionValue != null)
                {
                    assigneeValue = assigneeExpressionValue.ToString();
                }
                string assigneeUser = null;

                if (string.IsNullOrWhiteSpace(assigneeValue) == false)
                {
                    IUserServiceProxy userService = ProcessEngineServiceProvider.Resolve <IUserServiceProxy>();

                    var user = userService.GetUser(assigneeValue)
                               .ConfigureAwait(false)
                               .GetAwaiter()
                               .GetResult();

                    assigneeUser = user?.FullName;

                    task.SetVariableLocal(assigneeValue, user);
                }
                taskEntityManager.ChangeTaskAssigneeNoEvents(task, assigneeValue, assigneeUser);
            }

            if (!string.IsNullOrWhiteSpace(owner))
            {
                object ownerExpressionValue = expressionManager.CreateExpression(owner).GetValue(execution);
                string ownerValue           = null;
                if (ownerExpressionValue != null)
                {
                    ownerValue = ownerExpressionValue.ToString();
                }

                taskEntityManager.ChangeTaskOwner(task, ownerValue);
            }

            if (candidateGroups != null && candidateGroups.Count > 0)
            {
                foreach (string candidateGroup in candidateGroups)
                {
                    IExpression groupIdExpr = expressionManager.CreateExpression(candidateGroup);
                    object      value       = groupIdExpr.GetValue(execution);
                    if (value is string @string)
                    {
                        IList <string> candidates = ExtractCandidates(@string);
                        task.AddCandidateGroups(candidates);
                    }
                    else if (value is ICollection)
                    {
                        task.AddCandidateGroups((ICollection <string>)value);
                    }
                    else
                    {
                        throw new ActivitiIllegalArgumentException("Expression did not resolve to a string or collection of strings");
                    }
                }
            }

            if (candidateUsers != null && candidateUsers.Count > 0)
            {
                foreach (string candidateUser in candidateUsers)
                {
                    IExpression userIdExpr = expressionManager.CreateExpression(candidateUser);
                    object      value      = userIdExpr.GetValue(execution);
                    if (value is string @string)
                    {
                        IList <string> candidates = ExtractCandidates(@string);
                        task.AddCandidateUsers(candidates);
                    }
                    else if (value is ICollection)
                    {
                        task.AddCandidateUsers((ICollection <string>)value);
                    }
                    else
                    {
                        throw new ActivitiException("Expression did not resolve to a string or collection of strings");
                    }
                }
            }

            if (userTask.CustomUserIdentityLinks != null && userTask.CustomUserIdentityLinks.Count > 0)
            {
                foreach (string customUserIdentityLinkType in userTask.CustomUserIdentityLinks.Keys)
                {
                    foreach (string userIdentityLink in userTask.CustomUserIdentityLinks[customUserIdentityLinkType])
                    {
                        IExpression idExpression = expressionManager.CreateExpression(userIdentityLink);
                        object      value        = idExpression.GetValue(execution);
                        if (value is string @string)
                        {
                            IList <string> userIds = ExtractCandidates(@string);
                            foreach (string userId in userIds)
                            {
                                task.AddUserIdentityLink(userId, customUserIdentityLinkType);
                            }
                        }
                        else if (value is ICollection collection)
                        {
                            IEnumerator userIdSet = collection.GetEnumerator();
                            while (userIdSet.MoveNext())
                            {
                                task.AddUserIdentityLink((string)userIdSet.Current, customUserIdentityLinkType);
                            }
                        }
                        else
                        {
                            throw new ActivitiException("Expression did not resolve to a string or collection of strings");
                        }
                    }
                }
            }

            if (userTask.CustomGroupIdentityLinks != null && userTask.CustomGroupIdentityLinks.Count > 0)
            {
                foreach (string customGroupIdentityLinkType in userTask.CustomGroupIdentityLinks.Keys)
                {
                    foreach (string groupIdentityLink in userTask.CustomGroupIdentityLinks[customGroupIdentityLinkType])
                    {
                        IExpression idExpression = expressionManager.CreateExpression(groupIdentityLink);
                        object      value        = idExpression.GetValue(execution);
                        if (value is string @string)
                        {
                            IList <string> groupIds = ExtractCandidates(@string);
                            foreach (string groupId in groupIds)
                            {
                                task.AddGroupIdentityLink(groupId, customGroupIdentityLinkType);
                            }
                        }
                        else if (value is ICollection collection)
                        {
                            IEnumerator groupIdSet = collection.GetEnumerator();
                            while (groupIdSet.MoveNext())
                            {
                                task.AddGroupIdentityLink((string)groupIdSet.Current, customGroupIdentityLinkType);
                            }
                        }
                        else
                        {
                            throw new ActivitiException("Expression did not resolve to a string or collection of strings");
                        }
                    }
                }
            }
        }