/// <summary>
        ///
        /// </summary>
        /// <param name="execution"></param>
        public override void Execute(IExecutionEntity execution)
        {
            ICommandContext    commandContext    = Context.CommandContext;
            ITaskEntityManager taskEntityManager = commandContext.TaskEntityManager;

            //如果当前任务为补偿任务,则修改任务的父级为流程实例
            if ((execution.CurrentFlowElement as UserTask).ForCompensation)
            {
                execution.Parent = execution.ProcessInstance;
            }

            ITaskEntity task = taskEntityManager.Create();

            task.Execution         = execution;
            task.TaskDefinitionKey = userTask.Id;
            task.IsRuntimeAssignee();

            task.CanTransfer  = userTask.CanTransfer;
            task.OnlyAssignee = task.OnlyAssignee;

            ProcessEngineConfigurationImpl processEngineConfiguration = Context.ProcessEngineConfiguration;
            ExpressionManager expressionManager = processEngineConfiguration.ExpressionManager;

            string         activeTaskName;
            string         activeTaskDescription;
            string         activeTaskDueDate;
            string         activeTaskCategory;
            string         activeTaskSkipExpression;
            string         activeTaskPriority;
            string         activeTaskFormKey;
            string         activeTaskAssignee;
            string         activeTaskOwner;
            IList <string> activeTaskCandidateUsers;
            IList <string> activeTaskCandidateGroups;

            if (Context.ProcessEngineConfiguration.EnableProcessDefinitionInfoCache)
            {
                JToken taskElementProperties = Context.GetBpmnOverrideElementProperties(userTask.Id, execution.ProcessDefinitionId);
                activeTaskName            = GetActiveValue(userTask.Name, DynamicBpmnConstants.USER_TASK_NAME, taskElementProperties);
                activeTaskDescription     = GetActiveValue(userTask.Documentation, DynamicBpmnConstants.USER_TASK_DESCRIPTION, taskElementProperties);
                activeTaskDueDate         = GetActiveValue(userTask.DueDate, DynamicBpmnConstants.USER_TASK_DUEDATE, taskElementProperties);
                activeTaskPriority        = GetActiveValue(userTask.Priority, DynamicBpmnConstants.USER_TASK_PRIORITY, taskElementProperties);
                activeTaskCategory        = GetActiveValue(userTask.Category, DynamicBpmnConstants.USER_TASK_CATEGORY, taskElementProperties);
                activeTaskFormKey         = GetActiveValue(userTask.FormKey, DynamicBpmnConstants.USER_TASK_FORM_KEY, taskElementProperties);
                activeTaskSkipExpression  = GetActiveValue(userTask.SkipExpression, DynamicBpmnConstants.TASK_SKIP_EXPRESSION, taskElementProperties);
                activeTaskAssignee        = GetActiveValue(userTask.Assignee, DynamicBpmnConstants.USER_TASK_ASSIGNEE, taskElementProperties);
                activeTaskOwner           = GetActiveValue(userTask.Owner, DynamicBpmnConstants.USER_TASK_OWNER, taskElementProperties);
                activeTaskCandidateUsers  = GetActiveValueList(userTask.CandidateUsers, DynamicBpmnConstants.USER_TASK_CANDIDATE_USERS, taskElementProperties);
                activeTaskCandidateGroups = GetActiveValueList(userTask.CandidateGroups, DynamicBpmnConstants.USER_TASK_CANDIDATE_GROUPS, taskElementProperties);
            }
            else
            {
                activeTaskName            = userTask.Name;
                activeTaskDescription     = userTask.Documentation;
                activeTaskDueDate         = userTask.DueDate;
                activeTaskPriority        = userTask.Priority;
                activeTaskCategory        = userTask.Category;
                activeTaskFormKey         = userTask.FormKey;
                activeTaskSkipExpression  = userTask.SkipExpression;
                activeTaskAssignee        = userTask.Assignee;
                activeTaskOwner           = userTask.Owner;
                activeTaskCandidateUsers  = userTask.CandidateUsers;
                activeTaskCandidateGroups = userTask.CandidateGroups;
            }

            if (!string.IsNullOrWhiteSpace(activeTaskName))
            {
                string name;
                try
                {
                    name = (string)expressionManager.CreateExpression(activeTaskName).GetValue(execution);
                }
                catch (ActivitiException e)
                {
                    name = activeTaskName;
                    log.LogWarning("property not found in task name expression " + e.Message);
                }
                task.Name = name;
            }

            if (!string.IsNullOrWhiteSpace(activeTaskDescription))
            {
                string description;
                try
                {
                    description = (string)expressionManager.CreateExpression(activeTaskDescription).GetValue(execution);
                }
                catch (ActivitiException e)
                {
                    description = activeTaskDescription;
                    log.LogWarning("property not found in task description expression " + e.Message);
                }
                task.Description = description;
            }

            if (!string.IsNullOrWhiteSpace(activeTaskDueDate))
            {
                object dueDate = expressionManager.CreateExpression(activeTaskDueDate).GetValue(execution);
                if (dueDate != null)
                {
                    if (dueDate is DateTime time)
                    {
                        task.DueDate = time;
                    }
                    else if (dueDate is string @string)
                    {
                        string businessCalendarName;
                        if (!string.IsNullOrWhiteSpace(userTask.BusinessCalendarName))
                        {
                            businessCalendarName = expressionManager.CreateExpression(userTask.BusinessCalendarName).GetValue(execution).ToString();
                        }
                        else
                        {
                            businessCalendarName = DueDateBusinessCalendar.NAME;
                        }

                        IBusinessCalendar businessCalendar = Context.ProcessEngineConfiguration.BusinessCalendarManager.GetBusinessCalendar(businessCalendarName);
                        task.DueDate = businessCalendar.ResolveDuedate(@string);
                    }
                    else
                    {
                        throw new ActivitiIllegalArgumentException("Due date expression does not resolve to a Date or Date string: " + activeTaskDueDate);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(activeTaskPriority))
            {
                object priority = expressionManager.CreateExpression(activeTaskPriority).GetValue(execution);
                if (priority != null)
                {
                    if (priority is string @string)
                    {
                        try
                        {
                            task.Priority = Convert.ToInt32(@string);
                        }
                        catch (FormatException e)
                        {
                            throw new ActivitiIllegalArgumentException("Priority does not resolve to a number: " + priority, e);
                        }
                    }
                    else if (priority is int || priority is long)
                    {
                        task.Priority = (int)priority;
                    }
                    else
                    {
                        throw new ActivitiIllegalArgumentException("Priority expression does not resolve to a number: " + activeTaskPriority);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(activeTaskCategory))
            {
                object category = expressionManager.CreateExpression(activeTaskCategory).GetValue(execution);
                if (category != null)
                {
                    if (category is string)
                    {
                        task.Category = category.ToString();
                    }
                    else
                    {
                        throw new ActivitiIllegalArgumentException("Category expression does not resolve to a string: " + activeTaskCategory);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(activeTaskFormKey))
            {
                object formKey = expressionManager.CreateExpression(activeTaskFormKey).GetValue(execution);
                if (formKey != null)
                {
                    if (formKey is string)
                    {
                        task.FormKey = formKey.ToString();
                    }
                    else
                    {
                        throw new ActivitiIllegalArgumentException("FormKey expression does not resolve to a string: " + activeTaskFormKey);
                    }
                }
            }

            taskEntityManager.Insert(task, execution);

            bool skipUserTask = false;

            if (!string.IsNullOrWhiteSpace(activeTaskSkipExpression))
            {
                IExpression skipExpression = expressionManager.CreateExpression(activeTaskSkipExpression);
                skipUserTask = SkipExpressionUtil.IsSkipExpressionEnabled(execution, skipExpression) && SkipExpressionUtil.ShouldSkipFlowElement(execution, skipExpression);
            }

            // Handling assignments need to be done after the task is inserted, to have an id
            if (!skipUserTask)
            {
                HandleAssignments(taskEntityManager, activeTaskAssignee, activeTaskOwner, activeTaskCandidateUsers, activeTaskCandidateGroups, task, expressionManager, execution);
            }

            processEngineConfiguration.ListenerNotificationHelper.ExecuteTaskListeners(task, BaseTaskListenerFields.EVENTNAME_CREATE);

            // All properties set, now fire events
            if (Context.ProcessEngineConfiguration.EventDispatcher.Enabled)
            {
                IActivitiEventDispatcher eventDispatcher = Context.ProcessEngineConfiguration.EventDispatcher;
                eventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityEvent(ActivitiEventType.TASK_CREATED, task));
                if (string.IsNullOrWhiteSpace(task.Assignee) == false)
                {
                    eventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityEvent(ActivitiEventType.TASK_ASSIGNED, task));
                }
            }

            if (skipUserTask)
            {
                taskEntityManager.DeleteTask(task, null, false, false);
                Leave(execution);
            }
        }
Exemplo n.º 2
0
        public virtual DateTime?CalculateRepeat()
        {
            IBusinessCalendar businessCalendar = context.Impl.Context.ProcessEngineConfiguration.BusinessCalendarManager.GetBusinessCalendar(CycleBusinessCalendar.Name);

            return(businessCalendar.ResolveDuedate(repeat));
        }
Exemplo n.º 3
0
        /// <summary>
        /// The event definition on which the timer is based.
        ///
        /// Takes in an optional execution, if missing the <seealso cref="NoExecutionVariableScope"/> will be used (eg Timer start event)
        /// </summary>
        public static ITimerJobEntity CreateTimerEntityForTimerEventDefinition(TimerEventDefinition timerEventDefinition, bool isInterruptingTimer, IExecutionEntity executionEntity, string jobHandlerType, string jobHandlerConfig)
        {
            ProcessEngineConfigurationImpl processEngineConfiguration = Context.ProcessEngineConfiguration;

            string            businessCalendarRef = null;
            IExpression       expression          = null;
            ExpressionManager expressionManager   = processEngineConfiguration.ExpressionManager;

            // ACT-1415: timer-declaration on start-event may contain expressions NOT
            // evaluating variables but other context, evaluating should happen nevertheless
            IVariableScope scopeForExpression = executionEntity;

            if (scopeForExpression == null)
            {
                //scopeForExpression = NoExecutionVariableScope.SharedInstance;
            }

            if (!string.IsNullOrWhiteSpace(timerEventDefinition.TimeDate))
            {
                businessCalendarRef = DueDateBusinessCalendar.NAME;
                expression          = expressionManager.CreateExpression(timerEventDefinition.TimeDate);
            }
            else if (!string.IsNullOrWhiteSpace(timerEventDefinition.TimeCycle))
            {
                businessCalendarRef = CycleBusinessCalendar.NAME;
                expression          = expressionManager.CreateExpression(timerEventDefinition.TimeCycle);
            }
            else if (!string.IsNullOrWhiteSpace(timerEventDefinition.TimeDuration))
            {
                businessCalendarRef = DurationBusinessCalendar.NAME;
                expression          = expressionManager.CreateExpression(timerEventDefinition.TimeDuration);
            }

            if (!string.IsNullOrWhiteSpace(timerEventDefinition.CalendarName))
            {
                businessCalendarRef = timerEventDefinition.CalendarName;
                IExpression businessCalendarExpression = expressionManager.CreateExpression(businessCalendarRef);
                businessCalendarRef = businessCalendarExpression.GetValue(scopeForExpression).ToString();
            }

            if (expression == null)
            {
                throw new ActivitiException("Timer needs configuration (either timeDate, timeCycle or timeDuration is needed) (" + timerEventDefinition.Id + ")");
            }

            IBusinessCalendar businessCalendar = processEngineConfiguration.BusinessCalendarManager.GetBusinessCalendar(businessCalendarRef);

            string   dueDateString = null;
            DateTime?duedate       = null;

            object dueDateValue = expression.GetValue(scopeForExpression);

            if (dueDateValue is string)
            {
                dueDateString = (string)dueDateValue;
            }
            else if (dueDateValue is DateTime)
            {
                duedate = (DateTime)dueDateValue;
            }
            else if (dueDateValue is Nullable <DateTime> )
            {
                //JodaTime support
                duedate = (DateTime?)dueDateValue;
            }
            else if (dueDateValue != null)
            {
                throw new ActivitiException("Timer '" + executionEntity.ActivityId + "' was not configured with a valid duration/time, either hand in a java.util.Date or a String in format 'yyyy-MM-dd'T'hh:mm:ss'");
            }

            if (duedate == null && !string.IsNullOrWhiteSpace(dueDateString))
            {
                duedate = businessCalendar.ResolveDuedate(dueDateString);
            }

            ITimerJobEntity timer = null;

            if (duedate != null)
            {
                timer                         = Context.CommandContext.TimerJobEntityManager.Create();
                timer.JobType                 = JobFields.JOB_TYPE_TIMER;
                timer.Revision                = 1;
                timer.JobHandlerType          = jobHandlerType;
                timer.JobHandlerConfiguration = jobHandlerConfig;
                timer.Exclusive               = true;
                timer.Retries                 = processEngineConfiguration.AsyncExecutorNumberOfRetries;
                timer.Duedate                 = duedate;
                if (executionEntity != null)
                {
                    timer.Execution           = executionEntity;
                    timer.ProcessDefinitionId = executionEntity.ProcessDefinitionId;
                    timer.ProcessInstanceId   = executionEntity.ProcessInstanceId;

                    // Inherit tenant identifier (if applicable)
                    if (!string.IsNullOrWhiteSpace(executionEntity.TenantId))
                    {
                        timer.TenantId = executionEntity.TenantId;
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(timerEventDefinition.TimeCycle))
            {
                // See ACT-1427: A boundary timer with a cancelActivity='true', doesn't need to repeat itself
                bool repeat = !isInterruptingTimer;

                // ACT-1951: intermediate catching timer events shouldn't repeat according to spec
                if (executionEntity != null)
                {
                    FlowElement currentElement = executionEntity.CurrentFlowElement;
                    if (currentElement is IntermediateCatchEvent)
                    {
                        repeat = false;
                    }
                }

                if (repeat)
                {
                    string prepared = PrepareRepeat(dueDateString);
                    timer.Repeat = prepared;
                }
            }

            if (timer != null && executionEntity != null)
            {
                timer.Execution           = executionEntity;
                timer.ProcessDefinitionId = executionEntity.ProcessDefinitionId;

                // Inherit tenant identifier (if applicable)
                if (!string.IsNullOrWhiteSpace(executionEntity.TenantId))
                {
                    timer.TenantId = executionEntity.TenantId;
                }
            }

            return(timer);
        }
Exemplo n.º 4
0
        protected internal virtual DateTime?CalculateNextTimer(IJobEntity timerEntity, IVariableScope variableScope)
        {
            IBusinessCalendar businessCalendar = ProcessEngineConfiguration.BusinessCalendarManager.GetBusinessCalendar(GetBusinessCalendarName(TimerEventHandler.GeCalendarNameFromConfiguration(timerEntity.JobHandlerConfiguration), variableScope));

            return(businessCalendar.ResolveDuedate(timerEntity.Repeat, timerEntity.MaxIterations));
        }