/// <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); } } } }
/// <summary> /// 侦听接收通知处理 /// </summary> /// <param name="execution"></param> public void Notify(IExecutionEntity execution) { UserTask task = execution.CurrentFlowElement as UserTask; if (task.ExtensionElements.TryGetValue(BpmnXMLConstants.ELEMENT_EXTENSIONS_PROPERTY, out IList <ExtensionElement> exts)) { if (bool.TryParse(exts.GetAttributeValue(BpmnXMLConstants.ACTIITI_RUNTIME_ASSIGNEE), out bool result) == false || result == false) { return; } var variable = execution.GetVariableInstance(BpmnXMLConstants.RUNTIME_ASSIGNEE_USER_VARIABLE_NAME); RuntimeAssigneeUser user = JToken.FromObject(variable.Value).ToObject <RuntimeAssigneeUser>(); if ((user?.Users?.Count()).GetValueOrDefault() == 0) { throw new NotFoundAssigneeException(); } var varName = exts.GetAttributeValue(BpmnXMLConstants.ACTIITI_RUNTIME_ASSIGNEE_VARIABLE); if (string.IsNullOrWhiteSpace(varName)) { varName = user.Field; } execution.SetLoopVariable(varName, user.Users.Distinct().ToArray()); } }
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()); }