/// <summary>
        ///
        /// </summary>
        /// <param name="event"></param>
        public override void OnEvent(IActivitiEvent @event)
        {
            if (@event is CustomTaskCompletedEntityEventImpl taskEvent)
            {
                ITaskEntity taskEntity = taskEvent.Entity as ITaskEntity;

                ICommandContext commandContext = Context.CommandContext;

                IExecutionEntity execution = taskEntity.Execution;

                UserTask userTask = execution.CurrentFlowElement as UserTask;

                if (userTask.HasMultiInstanceLoopCharacteristics() &&
                    (taskEntity.IsAppend.GetValueOrDefault(false) ||
                     taskEntity.IsTransfer.GetValueOrDefault(false)))
                {
                    IExecutionEntity parent = execution.FindMultiInstanceParentExecution();

                    var collection = userTask.LoopCharacteristics.GetCollectionVarName();

                    IList <string> users = parent.GetLoopVariable <IList <string> >(collection);

                    string usr = users.FirstOrDefault(x => x.Equals(taskEntity.Assignee, StringComparison.OrdinalIgnoreCase));

                    if (usr != null)
                    {
                        users.Remove(usr);
                        parent.SetLoopVariable(collection, users);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 循环获取流程实例变量
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="execution">流程实例</param>
        /// <param name="variableName">变量名</param>
        /// <returns></returns>
        internal static T GetLoopVariable <T>(this IExecutionEntity execution, string variableName)
        {
            object           value  = execution.GetVariableLocal(variableName);
            IExecutionEntity parent = execution.Parent;

            while (value == null && parent != null)
            {
                value  = parent.GetLoopVariable <T>(variableName);
                parent = parent.Parent;
            }

            return(value == null ? default : JToken.FromObject(value).ToObject <T>());
        }
Ejemplo n.º 3
0
        public ITask[] Execute(ICommandContext commandContext)
        {
            ProcessEngineConfigurationImpl pec = commandContext.ProcessEngineConfiguration;
            IRuntimeService runtimeService     = pec.RuntimeService;
            ITaskService    taskService        = pec.TaskService;
            IIdGenerator    idGenerator        = pec.IdGenerator;

            ITask            task      = taskService.CreateTaskQuery().SetTaskId(taskId).SingleResult();
            IExecutionEntity execution = runtimeService.CreateExecutionQuery().SetExecutionId(task.ExecutionId).SingleResult() as IExecutionEntity;
            IExecutionEntity parent    = execution.Parent;

            //查找当前待追加人员是否已经存在在任务列表中,proc_inst_id_
            IList <ITask> assignTasks = taskService.CreateTaskQuery()
                                        .SetProcessInstanceId(execution.ProcessInstanceId)
                                        .SetTaskAssigneeIds(assignees)
                                        .List();

            var users = assignees.Where(x => assignTasks.Any(y => x == y.Assignee) == false).ToList();

            if (users.Count == 0)
            {
                throw new NotFoundAssigneeException();
            }

            if (parent.IsMultiInstanceRoot && parent.CurrentFlowElement is UserTask mTask)
            {
                string varName = mTask.LoopCharacteristics.InputDataItem;
                var    match   = new Regex("\\$\\{(.*?)\\}").Match(varName);
                varName = match.Groups[1].Value;

                IList <string> list = parent.GetLoopVariable <IList <string> >(varName);

                parent.SetLoopVariable(varName, users.Union(list).Distinct().ToArray());
            }

            IList <ITask> tasks = new List <ITask>(users.Count);

            foreach (var assignee in users)
            {
                //创建父活动的子活动
                IExecutionEntity newExecution = pec.CommandExecutor.Execute(new CreateChildExecutionCmd(parent));
                //设置为激活 状态
                newExecution.IsActive    = true;
                newExecution.ActivityId  = execution.ActivityId;
                newExecution.BusinessKey = execution.BusinessKey;
                //该属性表示创建的newExecution对象为分支,非常重要,不可缺少
                newExecution.IsConcurrent = execution.IsConcurrent;
                newExecution.IsScope      = false;
                ITaskEntity taskEntity = pec.CommandExecutor.Execute(new NewTaskCmd(pec.IdGenerator.GetNextId()));
                taskEntity.ProcessDefinitionId = task.ProcessDefinitionId;
                taskEntity.TaskDefinitionKey   = task.TaskDefinitionKey;
                taskEntity.ProcessInstanceId   = task.ProcessInstanceId;
                taskEntity.ExecutionId         = newExecution.Id;
                taskEntity.Name = task.Name;

                if (string.IsNullOrWhiteSpace(taskEntity.Id))
                {
                    string taskId = idGenerator.GetNextId();
                    taskEntity.Id = taskId;
                }
                taskEntity.Execution = newExecution;
                taskEntity.Assignee  = assignee;
                if (string.IsNullOrWhiteSpace(assignee) == false)
                {
                    taskEntity.AssigneeUser = AsyncHelper.RunSync(() => userService.GetUser(assignee))?.FullName;
                }
                taskEntity.TenantId = task.TenantId;
                taskEntity.FormKey  = task.FormKey;
                taskEntity.IsAppend = true;

                taskService.SaveTask(taskEntity);

                tasks.Add(taskEntity);
            }

            //修改执行实例父级实例变量数和活动实例变量数
            int nrOfInstances          = parent.GetLoopVariable <int>(MultiInstanceActivityBehavior.NUMBER_OF_INSTANCES);
            int nrOfActiveIntance      = parent.GetLoopVariable <int>(MultiInstanceActivityBehavior.NUMBER_OF_ACTIVE_INSTANCES);
            int nrOfCompletedInstances = parent.GetLoopVariable <int>(MultiInstanceActivityBehavior.NUMBER_OF_COMPLETED_INSTANCES);

            parent.SetLoopVariable(MultiInstanceActivityBehavior.NUMBER_OF_INSTANCES, nrOfInstances + users.Count);
            parent.SetLoopVariable(MultiInstanceActivityBehavior.NUMBER_OF_ACTIVE_INSTANCES, nrOfInstances - nrOfCompletedInstances + users.Count);

            return(tasks.ToArray());
        }