예제 #1
0
        protected void ddlActivateNewActivity_SelectedIndexChanged(object sender, EventArgs e)
        {
            ParseControls();

            int?activityTypeId = ddlActivateNewActivity.SelectedValueAsId();

            if (activityTypeId.HasValue)
            {
                var activityType = WorkflowActivityTypeCache.Read(activityTypeId.Value);
                if (activityType != null)
                {
                    var activity = WorkflowActivity.Activate(activityType, Workflow);
                    activity.Guid = Guid.NewGuid();

                    foreach (var action in activity.Actions)
                    {
                        action.Guid = Guid.NewGuid();
                    }

                    Workflow.AddLogEntry(string.Format("Manually Activated new '{0}' activity", activityType.ToString()));

                    ExpandedActivities.Add(activity.Guid);

                    BuildControls(true, activity.Guid);
                }
            }

            ddlActivateNewActivity.SelectedIndex = 0;
        }
예제 #2
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            Guid guid = GetAttributeValue(action, "Activity").AsGuid();

            if (guid.IsEmpty())
            {
                action.AddLogEntry("Invalid Activity Property", true);
                return(false);
            }

            var workflow = action.Activity.Workflow;

            var activityType = WorkflowActivityTypeCache.Read(guid);

            if (activityType == null)
            {
                action.AddLogEntry("Invalid Activity Property", true);
                return(false);
            }

            WorkflowActivity.Activate(activityType, workflow);
            action.AddLogEntry(string.Format("Activated new '{0}' activity", activityType.ToString()));

            return(true);
        }
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            var workflowActivityGuid = action.GetWorklowAttributeValue(GetAttributeValue(action, "Activity").AsGuid()).AsGuid();

            if (workflowActivityGuid.IsEmpty())
            {
                action.AddLogEntry("Invalid Activity Property", true);
                return(false);
            }

            var attributeKey   = GetAttributeValue(action, "WorkflowAttributeKey", true);
            var attributeValue = GetAttributeValue(action, "WorkflowAttributeValue", true);

            if (string.IsNullOrWhiteSpace(attributeKey) || string.IsNullOrWhiteSpace(attributeValue))
            {
                action.AddLogEntry("Invalid Workflow Property", true);
                return(false);
            }

            var activityType = WorkflowActivityTypeCache.Read(workflowActivityGuid);

            if (activityType == null)
            {
                action.AddLogEntry("Invalid Activity Property", true);
                return(false);
            }

            var entityType = EntityTypeCache.Read(typeof(Rock.Model.Workflow));

            var workflowIds = new AttributeValueService(rockContext)
                              .Queryable()
                              .AsNoTracking()
                              .Where(a => a.Attribute.Key == attributeKey && a.Value == attributeValue && a.Attribute.EntityTypeId == entityType.Id)
                              .Select(a => a.EntityId);

            var workflows = new WorkflowService(rockContext)
                            .Queryable()
                            //.AsNoTracking()
                            .Where(w => w.WorkflowType.ActivityTypes.Any(a => a.Guid == activityType.Guid) && workflowIds.Contains(w.Id))
                            .ToList();

            foreach (var workflow in workflows)
            {
                WorkflowActivity.Activate(activityType, workflow);
                action.AddLogEntry(string.Format("Activated new '{0}' activity in {1} {2}", activityType.ToString(), workflow.TypeName, workflow.WorkflowId));
            }


            return(true);
        }
예제 #4
0
        public static WorkflowActivity Activate(WorkflowActivityType activityType, Workflow workflow, RockContext rockContext)
        {
            if (activityType != null)
            {
                var activityTypeCache = WorkflowActivityTypeCache.Read(activityType.Id);
                var activity          = Activate(activityTypeCache, workflow, rockContext);
                if (activity != null)
                {
                    activity.ActivityType = activityType;
                }
                return(activity);
            }

            return(null);
        }
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            var workflowActivityGuid = action.GetWorklowAttributeValue(GetAttributeValue(action, "Activity").AsGuid()).AsGuid();

            if (workflowActivityGuid.IsEmpty())
            {
                action.AddLogEntry("Invalid Activity Property", true);
                return(false);
            }

            var reference = GetAttributeValue(action, "WorkflowReference", true);

            Rock.Model.Workflow workflow = null;
            if (reference.AsGuidOrNull() != null)
            {
                var referenceGuid = reference.AsGuid();
                workflow = new WorkflowService(rockContext).Queryable()
                           .Where(w => w.Guid == referenceGuid)
                           .FirstOrDefault();
            }
            else if (reference.AsIntegerOrNull() != null)
            {
                var referenceInt = reference.AsInteger();
                workflow = new WorkflowService(rockContext).Queryable()
                           .Where(w => w.Id == referenceInt)
                           .FirstOrDefault();
            }
            else
            {
                action.AddLogEntry("Invalid Workflow Property", true);
                return(false);
            }

            var activityType = WorkflowActivityTypeCache.Read(workflowActivityGuid);

            if (activityType == null)
            {
                action.AddLogEntry("Invalid Activity Property", true);
                return(false);
            }

            WorkflowActivity.Activate(activityType, workflow);
            action.AddLogEntry(string.Format("Activated new '{0}' activity", activityType.ToString()));

            return(true);
        }
예제 #6
0
        /// <summary>
        /// Renders the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="result">The result.</param>
        public override void Render(Context context, TextWriter result)
        {
            // first ensure that entity commands are allowed in the context
            if (!this.IsAuthorized(context))
            {
                result.Write(string.Format(RockLavaBlockBase.NotAuthorizedMessage, this.Name));
                base.Render(context, result);
                return;
            }

            var    attributes       = new Dictionary <string, string>();
            string parmWorkflowType = null;
            string parmWorkflowName = null;
            string parmWorkflowId   = null;
            string parmActivityType = null;

            /* Parse the markup text to pull out configuration parameters. */
            var parms = ParseMarkup(_markup, context);

            foreach (var p in parms)
            {
                if (p.Key.ToLower() == "workflowtype")
                {
                    parmWorkflowType = p.Value;
                }
                else if (p.Key.ToLower() == "workflowname")
                {
                    parmWorkflowName = p.Value;
                }
                else if (p.Key.ToLower() == "workflowid")
                {
                    parmWorkflowId = p.Value;
                }
                else if (p.Key.ToLower() == "activitytype")
                {
                    parmActivityType = p.Value;
                }
                else
                {
                    attributes.AddOrReplace(p.Key, p.Value);
                }
            }

            /* Process inside a new stack level so our own created variables do not
             * persist throughout the rest of the workflow. */
            context.Stack(() =>
            {
                using (var rockContext = new RockContext())
                {
                    WorkflowService workflowService = new WorkflowService(rockContext);
                    Rock.Model.Workflow workflow    = null;
                    WorkflowActivity activity       = null;

                    /* They provided a WorkflowType, so we need to kick off a new workflow. */
                    if (parmWorkflowType != null)
                    {
                        string type = parmWorkflowType;
                        string name = parmWorkflowName ?? string.Empty;
                        WorkflowTypeCache workflowType = null;

                        /* Get the type of workflow */
                        if (type.AsGuidOrNull() != null)
                        {
                            workflowType = WorkflowTypeCache.Read(type.AsGuid());
                        }
                        else if (type.AsIntegerOrNull() != null)
                        {
                            workflowType = WorkflowTypeCache.Read(type.AsInteger());
                        }

                        /* Try to activate the workflow */
                        if (workflowType != null)
                        {
                            workflow = Rock.Model.Workflow.Activate(workflowType, parmWorkflowName);

                            /* Set any workflow attributes that were specified. */
                            foreach (var attr in attributes)
                            {
                                if (workflow.Attributes.ContainsKey(attr.Key))
                                {
                                    workflow.SetAttributeValue(attr.Key, attr.Value.ToString());
                                }
                            }

                            if (workflow != null)
                            {
                                List <string> errorMessages;

                                workflowService.Process(workflow, out errorMessages);

                                if (errorMessages.Any())
                                {
                                    context["Error"] = string.Join("; ", errorMessages.ToArray());
                                }

                                context["Workflow"] = workflow;
                            }
                            else
                            {
                                context["Error"] = "Could not activate workflow.";
                            }
                        }
                        else
                        {
                            context["Error"] = "Workflow type not found.";
                        }
                    }

                    /* They instead provided a WorkflowId, so we are working with an existing Workflow. */
                    else if (parmWorkflowId != null)
                    {
                        string id = parmWorkflowId.ToString();

                        /* Get the workflow */
                        if (id.AsGuidOrNull() != null)
                        {
                            workflow = workflowService.Get(id.AsGuid());
                        }
                        else if (id.AsIntegerOrNull() != null)
                        {
                            workflow = workflowService.Get(id.AsInteger());
                        }

                        if (workflow != null)
                        {
                            if (workflow.CompletedDateTime == null)
                            {
                                /* Currently we cannot activate an activity in a workflow that is currently
                                 * being processed. The workflow is held in-memory so the activity we would
                                 * activate would not show up for the processor and probably never run.
                                 */
                                if (!workflow.IsProcessing)
                                {
                                    bool hasError = false;

                                    /* If they provided an ActivityType parameter then we need to activate
                                     * a new activity in the workflow.
                                     */
                                    if (parmActivityType != null)
                                    {
                                        string type = parmActivityType.ToString();
                                        WorkflowActivityTypeCache activityType = null;

                                        /* Get the type of activity */
                                        if (type.AsGuidOrNull() != null)
                                        {
                                            activityType = WorkflowActivityTypeCache.Read(type.AsGuid());
                                        }
                                        else if (type.AsIntegerOrNull() != null)
                                        {
                                            activityType = WorkflowActivityTypeCache.Read(type.AsInteger());
                                        }

                                        if (activityType != null)
                                        {
                                            activity = WorkflowActivity.Activate(activityType, workflow);

                                            /* Set any workflow attributes that were specified. */
                                            foreach (var attr in attributes)
                                            {
                                                if (activity.Attributes.ContainsKey(attr.Key))
                                                {
                                                    activity.SetAttributeValue(attr.Key, attr.Value.ToString());
                                                }
                                            }
                                        }
                                        else
                                        {
                                            context["Error"] = "Activity type was not found.";
                                            hasError         = true;
                                        }
                                    }

                                    /* Process the existing Workflow. */
                                    if (!hasError)
                                    {
                                        List <string> errorMessages;
                                        workflowService.Process(workflow, out errorMessages);

                                        if (errorMessages.Any())
                                        {
                                            context["Error"] = string.Join("; ", errorMessages.ToArray());
                                        }

                                        context["Workflow"] = workflow;
                                        context["Activity"] = activity;
                                    }
                                }
                                else
                                {
                                    context["Error"] = "Cannot activate activity on workflow that is currently being processed.";
                                }
                            }
                            else
                            {
                                context["Error"] = "Workflow has already been completed.";
                            }
                        }
                        else
                        {
                            context["Error"] = "Workflow not found.";
                        }
                    }
                    else
                    {
                        context["Error"] = "Must specify one of WorkflowType or WorkflowId.";
                    }

                    RenderAll(NodeList, context, result);
                }
            });
        }
예제 #7
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            double hoursElapsed = HoursElapsed(action);
            string emailStatus  = EmailStatus(action);

            if (hoursElapsed <= 0)
            {
                SendEmail(rockContext, action);
            }
            else
            {
                var timedOut = false;

                WorkflowActivityTypeCache unopenedActivityType = null;
                int? unopenedTimeout = null;
                Guid?guid            = GetAttributeValue(action, "UnopenedTimeoutActivity").AsGuidOrNull();
                if (guid.HasValue)
                {
                    unopenedActivityType = WorkflowActivityTypeCache.Read(guid.Value);
                    unopenedTimeout      = GetAttributeValue(action, "UnopenedTimeoutLength").AsIntegerOrNull();

                    if (emailStatus != OPENED_STATUS &&
                        emailStatus != CLICKED_STATUS &&
                        unopenedActivityType != null &&
                        unopenedTimeout.HasValue &&
                        unopenedTimeout.Value < hoursElapsed)
                    {
                        action.AddLogEntry("Unopened Timeout Occurred", true);
                        WorkflowActivity.Activate(unopenedActivityType, action.Activity.Workflow, rockContext);
                        timedOut = true;
                    }
                }

                WorkflowActivityTypeCache noActionActivityType = null;
                int?noActionTimeout = null;
                guid = GetAttributeValue(action, "NoActionTimeoutActivity").AsGuidOrNull();
                if (guid.HasValue)
                {
                    noActionActivityType = WorkflowActivityTypeCache.Read(guid.Value);
                    noActionTimeout      = GetAttributeValue(action, "NoActionTimeoutLength").AsIntegerOrNull();

                    if (emailStatus != CLICKED_STATUS &&
                        noActionActivityType != null &&
                        noActionTimeout.HasValue &&
                        noActionTimeout.Value < hoursElapsed)
                    {
                        action.AddLogEntry("No Action Timeout Occurred", true);
                        WorkflowActivity.Activate(noActionActivityType, action.Activity.Workflow, rockContext);
                        timedOut = true;
                    }
                }

                if (timedOut)
                {
                    UpdateEmailStatus(action.Guid, TIMEOUT_STATUS, string.Empty, rockContext, false);
                    return(true);
                }
            }

            return(false);
        }
예제 #8
0
        /// <summary>
        /// Updates the email status.
        /// </summary>
        /// <param name="actionGuid">The action unique identifier.</param>
        /// <param name="status">The status.</param>
        /// <param name="emailEventType">Type of the email event.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="ProcessWorkflow">if set to <c>true</c> [process workflow].</param>
        public static void UpdateEmailStatus(Guid actionGuid, string status, string emailEventType, RockContext rockContext, bool ProcessWorkflow)
        {
            var action = new WorkflowActionService(rockContext).Get(actionGuid);

            if (action != null && action.Activity != null)
            {
                string attrKey = action.ActionTypeCache.Guid.ToString() + "_EmailStatus";

                action.Activity.LoadAttributes(rockContext);
                string currentStatus = action.Activity.GetAttributeValue(attrKey);

                // Sometimes Clicked events are reported before opens. If this is the case, do not update the status from clicked to opened.
                bool updateStatus = true;
                if (status == OPENED_STATUS && currentStatus == CLICKED_STATUS)
                {
                    updateStatus = false;
                }

                if (!string.IsNullOrWhiteSpace(emailEventType) && (emailEventType != status || !updateStatus))
                {
                    action.AddLogEntry(string.Format("Email Event Type: {0}", emailEventType), true);
                }

                if (updateStatus)
                {
                    action.Activity.SetAttributeValue(attrKey, status);
                    action.Activity.SaveAttributeValues(rockContext);
                    action.AddLogEntry(string.Format("Email Status Updated to '{0}'", status), true);
                }

                Guid?activityGuid = null;
                switch (status)
                {
                case OPENED_STATUS:
                {
                    activityGuid = GetActionAttributeValue(action, "OnOpenActivity").AsGuid();
                    break;
                }

                case CLICKED_STATUS:
                {
                    activityGuid = GetActionAttributeValue(action, "OnClickedActivity").AsGuid();
                    break;
                }

                case FAILED_STATUS:
                {
                    activityGuid = GetActionAttributeValue(action, "OnFailedActivity").AsGuid();
                    break;
                }
                }

                if (activityGuid.HasValue)
                {
                    var workflow     = action.Activity.Workflow;
                    var activityType = WorkflowActivityTypeCache.Read(activityGuid.Value);
                    if (workflow != null && activityType != null)
                    {
                        WorkflowActivity.Activate(activityType, workflow);
                        action.AddLogEntry(string.Format("Activated new '{0}' activity", activityType.ToString()));

                        if (ProcessWorkflow)
                        {
                            List <string> workflowErrors;
                            new WorkflowService(rockContext).Process(workflow, out workflowErrors);
                        }
                    }
                }
            }
        }