Exemple #1
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.Get(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);
        }
        private bool activateActivity(RockContext rockContext, WorkflowAction action, string activityAttributeName)
        {
            Guid guid = GetAttributeValue(action, activityAttributeName).AsGuid();

            if (guid.IsEmpty())
            {
                // No activity.  Just be done.
                return(true);
            }

            var workflow = action.Activity.Workflow;

            var activityType = WorkflowActivityTypeCache.Get(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  itemListValue       = GetAttributeValue(action, "ItemList");
            Guid itemListValueAsGuid = itemListValue.AsGuid();

            if (!itemListValueAsGuid.IsEmpty())
            {
                var workflowAttributeValue = action.GetWorklowAttributeValue(itemListValueAsGuid);
                if (workflowAttributeValue != null)
                {
                    itemListValue = workflowAttributeValue;
                }
            }
            else
            {
                itemListValue = itemListValue.ResolveMergeFields(GetMergeFields(action));
            }

            if (string.IsNullOrWhiteSpace(itemListValue))
            {
                action.AddLogEntry("List is empty, not activating any activities.", true);
                return(true);
            }


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

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

            var workflow = action.Activity.Workflow;

            var activityTypeCache = WorkflowActivityTypeCache.Get(guid);

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

            string activityAttributeKey    = GetAttributeValue(action, "ActivityAttributeKey");
            bool   hasActivityAttributeKey = !string.IsNullOrWhiteSpace(activityAttributeKey);

            foreach (var item in itemListValue.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
            {
                var activity = WorkflowActivity.Activate(activityTypeCache, workflow);
                if (hasActivityAttributeKey)
                {
                    activity.SetAttributeValue(activityAttributeKey, item);
                }
                action.AddLogEntry(string.Format("Activated new '{0}' activity", activityTypeCache.ToString()));
            }

            return(true);
        }
        protected void ddlActivateNewActivity_SelectedIndexChanged(object sender, EventArgs e)
        {
            ParseControls();

            int?activityTypeId = ddlActivateNewActivity.SelectedValueAsId();

            if (activityTypeId.HasValue)
            {
                var activityType = WorkflowActivityTypeCache.Get(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;
        }
Exemple #5
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.Get(guid);

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

            Dictionary <string, string> keyValues = null;
            var attributeValues = GetAttributeValue(action, "AttributeValues");

            if (!string.IsNullOrWhiteSpace(attributeValues))
            {
                keyValues = attributeValues.AsDictionaryOrNull();
            }
            keyValues = keyValues ?? new Dictionary <string, string>();

            var activity = WorkflowActivity.Activate(activityType, workflow);

            activity.LoadAttributes(rockContext);

            foreach (var keyPair in keyValues)
            {
                //
                // Does the key exist as an attribute in the destination activity?
                //
                if (activity.Attributes.ContainsKey(keyPair.Key))
                {
                    var value = keyPair.Value.ResolveMergeFields(GetMergeFields(action));
                    activity.SetAttributeValue(keyPair.Key, value);
                }
                else
                {
                    errorMessages.Add(string.Format("'{0}' is not an attribute key in the activated activity: '{1}'", keyPair.Value, activityType.Name));
                }
            }

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

            return(true);
        }
Exemple #6
0
        /// <summary>
        /// Updates any Cache Objects that are associated with this entity
        /// </summary>
        /// <param name="entityState">State of the entity.</param>
        /// <param name="dbContext">The database context.</param>
        public void UpdateCache(System.Data.Entity.EntityState entityState, Rock.Data.DbContext dbContext)
        {
            var workflowTypeId = WorkflowActivityTypeCache.Get(this.ActivityTypeId, dbContext as RockContext)?.WorkflowTypeId;

            if (workflowTypeId.HasValue)
            {
                WorkflowTypeCache.UpdateCachedEntity(workflowTypeId.Value, System.Data.Entity.EntityState.Modified);
            }

            WorkflowActivityTypeCache.UpdateCachedEntity(this.ActivityTypeId, System.Data.Entity.EntityState.Modified);
            WorkflowActionTypeCache.UpdateCachedEntity(this.Id, entityState);
        }
Exemple #7
0
        protected void bntMerge_Click(object sender, EventArgs e)
        {
            PDFWorkflowObject pdfWorkflowObject = new PDFWorkflowObject();

            pdfWorkflowObject.LavaInput = ceLava.Text;

            pdfWorkflowObject.MergeObjects = new Dictionary <string, object>();
            if (cbCurrentPerson.Checked)
            {
                pdfWorkflowObject.MergeObjects.Add("CurrentPerson", CurrentPerson);
            }
            if (cbGlobal.Checked)
            {
                pdfWorkflowObject.MergeObjects.Add("GlobalAttributes", GlobalAttributesCache.Get());
            }


            Guid workflowTypeGuid = Guid.NewGuid();

            if (Guid.TryParse(GetAttributeValue("WorkflowType"), out workflowTypeGuid))
            {
                var workflowRockContext = new RockContext();
                var workflowTypeService = new WorkflowTypeService(workflowRockContext);
                var workflowType        = workflowTypeService.Get(workflowTypeGuid);
                if (workflowType != null)
                {
                    var workflow = Workflow.Activate(WorkflowTypeCache.Get(workflowType.Id), "PDFLavaWorkflow");

                    List <string> workflowErrors;
                    var           workflowService  = new WorkflowService(workflowRockContext);
                    var           workflowActivity = GetAttributeValue("WorkflowActivity");
                    var           activityType     = workflowType.ActivityTypes.Where(a => a.Name == workflowActivity).FirstOrDefault();
                    if (activityType != null)
                    {
                        WorkflowActivity.Activate(WorkflowActivityTypeCache.Get(activityType.Id), workflow, workflowRockContext);
                        if (workflowService.Process(workflow, pdfWorkflowObject, out workflowErrors))
                        {
                            //success
                        }
                    }
                }
            }

            RockContext       rockContext       = new RockContext();
            BinaryFileService binaryFileService = new BinaryFileService(rockContext);

            pdfWorkflowObject.PDF.FileName = "LavaGeneratedPDF.pdf";
            binaryFileService.Add(pdfWorkflowObject.PDF);
            rockContext.SaveChanges();

            Response.Redirect(pdfWorkflowObject.PDF.Path);
        }
        /// <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.Get(workflowActivityGuid);

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

            var entityType = EntityTypeCache.Get(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);
        }
Exemple #9
0
        public static WorkflowActivity Activate(WorkflowActivityType activityType, Workflow workflow, RockContext rockContext)
        {
            if (activityType != null)
            {
                var activityTypeCache = WorkflowActivityTypeCache.Get(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.GetWorkflowAttributeValue(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.Get(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);
        }
Exemple #11
0
        protected void bntMerge_Click(object sender, EventArgs e)
        {
            var rockContext = new RockContext();
            //Get the mergefields and pdf
            BinaryFileService           binaryFileService = new BinaryFileService(rockContext);
            Dictionary <string, object> mergeFields       = GetMergeFields();
            BinaryFile pdf = binaryFileService.Get(int.Parse(fpSelectedFile.SelectedValue));

            //Create the object we will need to pass to the workflow
            if (pdf != null && mergeFields.Count > 0)
            {
                var pdfEntity = new PDFWorkflowObject();
                pdfEntity.PDF          = pdf;
                pdfEntity.MergeObjects = mergeFields;

                Guid workflowTypeGuid = Guid.NewGuid();
                if (Guid.TryParse(GetAttributeValue("WorkflowType"), out workflowTypeGuid))
                {
                    var workflowRockContext = new RockContext();
                    var workflowTypeService = new WorkflowTypeService(workflowRockContext);
                    var workflowType        = workflowTypeService.Get(workflowTypeGuid);
                    if (workflowType != null)
                    {
                        var workflow = Workflow.Activate(WorkflowTypeCache.Get(workflowType.Id), pdf.FileName);

                        List <string> workflowErrors;
                        var           workflowService  = new WorkflowService(workflowRockContext);
                        var           workflowActivity = GetAttributeValue("WorkflowActivity");
                        var           activityType     = workflowType.ActivityTypes.Where(a => a.Name == workflowActivity).FirstOrDefault();
                        if (activityType != null)
                        {
                            WorkflowActivity.Activate(WorkflowActivityTypeCache.Get(activityType.Id), workflow, workflowRockContext);
                            if (workflowService.Process(workflow, pdfEntity, out workflowErrors))
                            {
                                //success
                            }
                        }
                    }
                }

                var mergedPDF = pdfEntity.PDF;
                //mergedPDF.Guid = Guid.NewGuid();
                binaryFileService.Add(mergedPDF);
                rockContext.SaveChanges();

                Response.Redirect(mergedPDF.Path);
            }
        }
Exemple #12
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.Get(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.Get(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);
        }
Exemple #13
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.Get(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);
                        }
                    }
                }
            }
        }
Exemple #14
0
        protected void btnReopen_Command(object sender, CommandEventArgs e)
        {
            using (RockContext rockContext = new RockContext())
            {
                WorkflowService workflowService = new WorkflowService(rockContext);
                Workflow        workflow        = workflowService.Get(e.CommandArgument.ToString().AsInteger());
                if (workflow != null && !workflow.IsActive)
                {
                    workflow.Status            = "Active";
                    workflow.CompletedDateTime = null;

                    // Find the summary activity and activate it.
                    WorkflowActivityType workflowActivityType = workflow.WorkflowType.ActivityTypes.Where(at => at.Name.Contains("Summary")).FirstOrDefault();
                    WorkflowActivity     workflowActivity     = WorkflowActivity.Activate(WorkflowActivityTypeCache.Get(workflowActivityType.Id, rockContext), workflow, rockContext);
                }
                rockContext.SaveChanges();
            }
            BindGrid();
        }
Exemple #15
0
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            var         errors      = new List <string>();
            RockContext rockContext = new RockContext();
            var         workflows   = GetWorkflows(rockContext);

            //Get the new attribute values from the repeater
            Dictionary <string, string> attributeUpdates = GetNewAttributeValues();

            if (attributeUpdates.Any())
            {
                var mergeFields = LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson);
                foreach (var workflow in workflows)
                {
                    //Reuse the mergefields with new workflow
                    mergeFields["Workflow"] = workflow;

                    //We will store the attribute value changes and apply them all at once
                    Dictionary <string, string> toUpdate = new Dictionary <string, string>();
                    foreach (var update in attributeUpdates)
                    {
                        toUpdate[update.Key] = update.Value.ResolveMergeFields(mergeFields);
                        //if (toUpdate[update.Key])
                        //{

                        //}
                    }

                    foreach (var update in toUpdate)
                    {
                        workflow.SetAttributeValue(update.Key, update.Value);
                    }

                    workflow.SaveAttributeValues();
                }
            }

            //Workflow settings

            //Activate the workflows if requested or if a new activity is activated
            if (ddlState.SelectedValue == "NotComplete" || ddlActivities.SelectedValue.IsNotNullOrWhiteSpace())
            {
                foreach (var workflow in workflows)
                {
                    workflow.CompletedDateTime = null;
                }
            }
            else if (ddlState.SelectedValue == "Complete")
            {
                foreach (var workflow in workflows)
                {
                    workflow.MarkComplete();
                }
            }

            //Update Status message
            if (tbStatus.Text.IsNotNullOrWhiteSpace())
            {
                var mergeFields = LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson);
                foreach (var workflow in workflows)
                {
                    mergeFields["Workflow"] = workflow;
                    workflow.Status         = tbStatus.Text.ResolveMergeFields(mergeFields);
                }
            }

            //Activate New Activity

            int?activityTypeId = ddlActivities.SelectedValueAsId();

            if (activityTypeId.HasValue)
            {
                var activityType = WorkflowActivityTypeCache.Get(activityTypeId.Value);
                if (activityType != null)
                {
                    foreach (var workflow in workflows)
                    {
                        var activity = WorkflowActivity.Activate(activityType, workflow, rockContext);
                        activity.Guid = Guid.NewGuid();

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

            //Process workflows
            List <string> errorMessages = new List <string>();

            if (workflows.Where(w => w.IsActive).Any())
            {
                foreach (var workflow in workflows)
                {
                    WorkflowService workflowService = new WorkflowService(new RockContext());
                    workflowService.Process(workflow, out errorMessages);
                }
            }

            pnlConfirmation.Visible = false;
            pnlDisplay.Visible      = false;
            pnlDone.Visible         = true;
        }
Exemple #16
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((System.Action)(() =>
            {
                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.Get(type.AsGuid());
                        }
                        else if (type.AsIntegerOrNull() != null)
                        {
                            workflowType = WorkflowTypeCache.Get(type.AsInteger());
                        }

                        /* Try to activate the workflow */
                        if (workflowType != null)
                        {
                            workflow = Rock.Model.Workflow.Activate(( WorkflowTypeCache )workflowType, ( string )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.Get(type.AsGuid());
                                        }
                                        else if (type.AsIntegerOrNull() != null)
                                        {
                                            activityType = WorkflowActivityTypeCache.Get(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);
                }
            }));
        }
 /// <summary>
 /// Gets the cache object associated with this Entity
 /// </summary>
 /// <returns></returns>
 public IEntityCache GetCacheObject()
 {
     return(WorkflowActivityTypeCache.Get(this.Id));
 }