Beispiel #1
0
            /// <summary>
            /// Initializes a new instance of the <see cref="ContextItem" /> class.
            /// </summary>
            /// <param name="entity">The entity.</param>
            /// <param name="dbEntityEntry">The database entity entry.</param>
            /// <param name="enableAuditing">if set to <c>true</c> [enable auditing].</param>
            public ContextItem(IEntity entity, DbEntityEntry dbEntityEntry, bool enableAuditing)
            {
                Entity        = entity;
                DbEntityEntry = dbEntityEntry;
                if (enableAuditing)
                {
                    Audit = new Audit();

                    switch (dbEntityEntry.State)
                    {
                    case EntityState.Added:
                    {
                        Audit.AuditType = AuditType.Add;
                        break;
                    }

                    case EntityState.Deleted:
                    {
                        Audit.AuditType = AuditType.Delete;
                        break;
                    }

                    case EntityState.Modified:
                    {
                        Audit.AuditType = AuditType.Modify;
                        break;
                    }
                    }
                }

                PreSaveState = dbEntityEntry.State;

                if (dbEntityEntry.State == EntityState.Modified)

                {
                    var triggers = WorkflowTriggersCache.Triggers(entity.TypeName)
                                   .Where(t => t.WorkflowTriggerType == WorkflowTriggerType.ImmediatePostSave || t.WorkflowTriggerType == WorkflowTriggerType.PostSave);

                    if (triggers.Any())
                    {
                        OriginalValues = new Dictionary <string, object>();
                        foreach (var p in DbEntityEntry.OriginalValues.PropertyNames)
                        {
                            OriginalValues.Add(p, DbEntityEntry.OriginalValues[p]);
                        }
                    }
                }
            }
Beispiel #2
0
        /// <summary>
        /// Triggers all the workflows of the given triggerType for the given entity item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="triggerType">Type of the trigger.</param>
        /// <param name="personAlias">The person alias.</param>
        /// <returns></returns>
        private bool TriggerWorkflows(ContextItem item, WorkflowTriggerType triggerType, PersonAlias personAlias)
        {
            IEntity entity = item.Entity;
            Dictionary <string, PropertyInfo> properties = null;

            // Look at each trigger for this entity and for the given trigger type
            // and see if it's a match.
            foreach (var trigger in WorkflowTriggersCache.Triggers(entity.TypeName, triggerType).Where(t => t.IsActive == true))
            {
                bool match = true;

                // If a qualifier column was given, then we need to check the previous or current qualifier value
                // otherwise it's just an automatic match.
                if (!string.IsNullOrWhiteSpace(trigger.EntityTypeQualifierColumn))
                {
                    // Get and cache the properties https://lotsacode.wordpress.com/2010/04/13/reflection-type-getproperties-and-performance/
                    // (Note: its possible that none of the triggers need them, so future TODO could be to
                    // bypass all this in that case.
                    if (properties == null)
                    {
                        properties = new Dictionary <string, PropertyInfo>();
                        foreach (PropertyInfo propertyInfo in entity.GetType().GetProperties())
                        {
                            properties.Add(propertyInfo.Name.ToLower(), propertyInfo);
                        }
                    }

                    match = IsQualifierMatch(item, properties, trigger);
                }

                // If we found a matching trigger, then fire it; otherwise do nothing.
                if (match)
                {
                    // If it's one of the pre or immediate triggers, fire it immediately; otherwise queue it.
                    if (triggerType == WorkflowTriggerType.PreSave || triggerType == WorkflowTriggerType.PreDelete || triggerType == WorkflowTriggerType.ImmediatePostSave)
                    {
                        var workflowType = WorkflowTypeCache.Get(trigger.WorkflowTypeId);
                        if (workflowType != null && (workflowType.IsActive ?? true))
                        {
                            var workflow = Rock.Model.Workflow.Activate(workflowType, trigger.WorkflowName);

                            List <string> workflowErrors;

                            using (var rockContext = new RockContext())
                            {
                                var workflowService = new WorkflowService(rockContext);
                                if (!workflowService.Process(workflow, entity, out workflowErrors))
                                {
                                    SaveErrorMessages.AddRange(workflowErrors);
                                    return(false);
                                }
                            }
                        }
                    }
                    else
                    {
                        var transaction = new Rock.Transactions.WorkflowTriggerTransaction();
                        transaction.Trigger     = trigger;
                        transaction.Entity      = entity.Clone();
                        transaction.PersonAlias = personAlias;
                        Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);
                    }
                }
            }


            return(true);
        }
Beispiel #3
0
 public static void Refresh()
 {
     WorkflowTriggersCache.Refresh();
 }
Beispiel #4
0
 public static List <WorkflowTrigger> Triggers(string entityTypeName, WorkflowTriggerType triggerType)
 {
     return(WorkflowTriggersCache.Triggers(entityTypeName, triggerType));
 }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            WorkflowTrigger        workflowTrigger;
            var                    rockContext            = new RockContext();
            WorkflowTriggerService WorkflowTriggerService = new WorkflowTriggerService(rockContext);
            AttributeService       attributeService       = new AttributeService(rockContext);
            bool                   usePreviousValue       = false;

            int WorkflowTriggerId = int.Parse(hfWorkflowTriggerId.Value);

            if (WorkflowTriggerId == 0)
            {
                workflowTrigger = new WorkflowTrigger();
                WorkflowTriggerService.Add(workflowTrigger);
            }
            else
            {
                workflowTrigger = WorkflowTriggerService.Get(WorkflowTriggerId);
            }

            workflowTrigger.WorkflowTypeId      = ddlWorkflowType.SelectedValueAsInt().Value;
            workflowTrigger.WorkflowTriggerType = (WorkflowTriggerType)System.Enum.Parse(typeof(WorkflowTriggerType), rblTriggerType.SelectedValue);

            workflowTrigger.EntityTypeId = ddlEntityType.SelectedValueAsInt().Value;
            workflowTrigger.EntityTypeQualifierColumn = ddlQualifierColumn.SelectedValue;

            //
            // If the trigger type is PreSave, PostSave or ImmediatePostSave then we have
            // a previous value option.
            //
            if (workflowTrigger.WorkflowTriggerType == WorkflowTriggerType.PreSave ||
                workflowTrigger.WorkflowTriggerType == WorkflowTriggerType.PostSave ||
                workflowTrigger.WorkflowTriggerType == WorkflowTriggerType.ImmediatePostSave)
            {
                usePreviousValue = true;
            }

            // If the trigger type is PreSave and the tbQualifierValue does not exist,
            // use the previous and alt qualifier value
            if (usePreviousValue)
            {
                if (!string.IsNullOrEmpty(tbQualifierValue.Text))
                {
                    // in this case, use the same value as the previous and current qualifier value
                    workflowTrigger.EntityTypeQualifierValue         = tbQualifierValue.Text;
                    workflowTrigger.EntityTypeQualifierValuePrevious = tbQualifierValue.Text;
                }
                else
                {
                    workflowTrigger.EntityTypeQualifierValue         = tbQualifierValueAlt.Text;
                    workflowTrigger.EntityTypeQualifierValuePrevious = tbPreviousQualifierValue.Text;
                }
            }
            else
            {
                // use the regular qualifier and clear the previous value qualifier since it does not apply.
                workflowTrigger.EntityTypeQualifierValue         = tbQualifierValue.Text;
                workflowTrigger.EntityTypeQualifierValuePrevious = string.Empty;
            }

            workflowTrigger.IsActive = cbIsActive.Checked;
            if (string.IsNullOrWhiteSpace(tbWorkflowName.Text))
            {
                workflowTrigger.WorkflowName = null;
            }
            else
            {
                workflowTrigger.WorkflowName = tbWorkflowName.Text;
            }

            if (!workflowTrigger.IsValid)
            {
                // Controls will render the error messages
                return;
            }

            rockContext.SaveChanges();

            WorkflowTriggersCache.Remove();

            NavigateToParentPage();
        }