/// <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 TriggerCache.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 = Web.Cache.WorkflowTypeCache.Read(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); }
private bool TriggerWorkflows(IEntity entity, WorkflowTriggerType triggerType, PersonAlias personAlias) { Dictionary <string, PropertyInfo> properties = null; using (var rockContext = new RockContext()) { var workflowTypeService = new WorkflowTypeService(rockContext); var workflowService = new WorkflowService(rockContext); foreach (var trigger in TriggerCache.Triggers(entity.TypeName, triggerType).Where(t => t.IsActive == true)) { bool match = true; if (!string.IsNullOrWhiteSpace(trigger.EntityTypeQualifierColumn)) { if (properties == null) { properties = new Dictionary <string, PropertyInfo>(); foreach (PropertyInfo propertyInfo in entity.GetType().GetProperties()) { properties.Add(propertyInfo.Name.ToLower(), propertyInfo); } } match = (properties.ContainsKey(trigger.EntityTypeQualifierColumn.ToLower()) && properties[trigger.EntityTypeQualifierColumn.ToLower()].GetValue(entity, null).ToString() == trigger.EntityTypeQualifierValue); } if (match) { if (triggerType == WorkflowTriggerType.PreSave || triggerType == WorkflowTriggerType.PreDelete) { var workflowType = workflowTypeService.Get(trigger.WorkflowTypeId); if (workflowType != null) { var workflow = Rock.Model.Workflow.Activate(workflowType, trigger.WorkflowName); List <string> workflowErrors; if (!workflow.Process(rockContext, entity, out workflowErrors)) { SaveErrorMessages.AddRange(workflowErrors); return(false); } else { if (workflow.IsPersisted || workflowType.IsPersisted) { workflowService.Add(workflow); rockContext.SaveChanges(); } } } } 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); }