protected void CreateSingleTrigger( string modelName, WorkflowTriggerType triggerType, string qualifierColumn, string qualifierValue, string workflowTypeGuid, string description ) { var insertQuery = string.Format( @" INSERT INTO [dbo].[WorkflowTrigger] ([IsSystem] ,[EntityTypeId] ,[EntityTypeQualifierColumn] ,[EntityTypeQualifierValue] ,[WorkflowTypeId] ,[WorkflowTriggerType] ,[WorkflowName] ,[Guid] ,[IsActive]) VALUES (0 ,(SELECT Id FROM EntityType WHERE NAME = 'Rock.Model.{0}') ,'{1}' ,{2} ,(SELECT Id FROM WorkflowType WHERE Guid = '{3}') ,{4} ,'{5} API Sync' ,NEWID() ,1)", modelName, qualifierColumn, qualifierValue, workflowTypeGuid, (int)triggerType, description ); Sql( insertQuery ); }
/// <summary> /// Returns a queryable collection of the <see cref="Rock.Model.WorkflowTrigger"/> by <see cref="Rock.Model.EntityType"/> name /// and <see cref="Rock.Model.WorkflowTriggerType"/> /// </summary> /// <param name="entityTypeName">A <see cref="System.String"/> representing the name of the <see cref="Rock.Model.EntityType"/> to filter by.</param> /// <param name="triggerType">The <see cref="Rock.Model.WorkflowTriggerType"/> to filter by.</param> /// <returns>A queryable collection of <see cref="Rock.Model.WorkflowTrigger"/> entities that meet the specified criteria.</returns> public IQueryable<WorkflowTrigger> Get( string entityTypeName, WorkflowTriggerType triggerType ) { return Repository.AsQueryable() .Where( t => t.EntityType.Name == entityTypeName && t.WorkflowTriggerType == triggerType ); }
/// <summary> /// Returns a queryable collection of the <see cref="Rock.Model.WorkflowTrigger"/> by <see cref="Rock.Model.EntityType"/> name /// and <see cref="Rock.Model.WorkflowTriggerType"/> /// </summary> /// <param name="entityTypeName">A <see cref="System.String"/> representing the name of the <see cref="Rock.Model.EntityType"/> to filter by.</param> /// <param name="triggerType">The <see cref="Rock.Model.WorkflowTriggerType"/> to filter by.</param> /// <returns>A queryable collection of <see cref="Rock.Model.WorkflowTrigger"/> entities that meet the specified criteria.</returns> public IQueryable <WorkflowTrigger> Get(string entityTypeName, WorkflowTriggerType triggerType) { return(Repository.AsQueryable() .Where(t => t.EntityType.Name == entityTypeName && t.WorkflowTriggerType == triggerType)); }
protected void CreateSingleTrigger(string modelName, WorkflowTriggerType triggerType, string qualifierColumn, string qualifierValue, string workflowTypeGuid, string description) { var insertQuery = string.Format(@" INSERT INTO [dbo].[WorkflowTrigger] ([IsSystem] ,[EntityTypeId] ,[EntityTypeQualifierColumn] ,[EntityTypeQualifierValue] ,[WorkflowTypeId] ,[WorkflowTriggerType] ,[WorkflowName] ,[Guid] ,[IsActive]) VALUES (0 ,(SELECT Id FROM EntityType WHERE NAME = 'Rock.Model.{0}') ,'{1}' ,{2} ,(SELECT Id FROM WorkflowType WHERE Guid = '{3}') ,{4} ,'{5} API Sync' ,NEWID() ,1)", modelName, qualifierColumn, qualifierValue, workflowTypeGuid, (int)triggerType, description); Sql(insertQuery); }
/// <summary> /// Triggerses the specified entity type name. /// </summary> /// <param name="entityTypeName">Name of the entity type.</param> /// <param name="triggerType">Type of the trigger.</param> /// <returns></returns> public List <WorkflowTriggerDto> Triggers(string entityTypeName, WorkflowTriggerType triggerType) { var triggers = new List <WorkflowTriggerDto>(); if (EntityTriggers != null && EntityTriggers.ContainsKey(entityTypeName)) { foreach (var trigger in EntityTriggers[entityTypeName]) { if (trigger.WorkflowTriggerType == triggerType) { triggers.Add(trigger); } } } return(triggers); }
/// <summary> /// Gets a collection of Workflow Triggers for the specified criteria. /// </summary> /// <param name="entityTypeName">Name of the entity type.</param> /// <param name="triggerType">Type of the trigger.</param> /// <returns></returns> public static List <WorkflowTrigger> Triggers(string entityTypeName, WorkflowTriggerType triggerType) { var triggers = Get(); if (triggers == null) { return(new List <WorkflowTrigger>()); } if (triggers.EntityTriggers != null && triggers.EntityTriggers.ContainsKey(entityTypeName)) { return(triggers.EntityTriggers[entityTypeName] .Where(t => t.WorkflowTriggerType == triggerType) .ToList()); } return(new List <WorkflowTrigger>()); }
/// <summary> /// Triggerses the specified entity type name. /// </summary> /// <param name="entityTypeName">Name of the entity type.</param> /// <param name="triggerType">Type of the trigger.</param> /// <returns></returns> public static List<WorkflowTrigger> Triggers( string entityTypeName, WorkflowTriggerType triggerType ) { var triggers = new List<WorkflowTrigger>(); lock ( obj ) { if ( EntityTriggers != null && EntityTriggers.ContainsKey( entityTypeName ) ) { foreach ( var trigger in EntityTriggers[entityTypeName] ) { if ( trigger.WorkflowTriggerType == triggerType ) { triggers.Add( trigger ); } } } } return triggers; }
/// <summary> /// Triggers the workflows. /// </summary> /// <param name="entity">The entity.</param> /// <param name="triggerType">Type of the trigger.</param> /// <param name="personId">The person id.</param> /// <returns></returns> private bool TriggerWorkflows(IEntity entity, WorkflowTriggerType triggerType, int?personId) { foreach (var trigger in TriggerCache.Instance.Triggers(entity.TypeName, triggerType)) { if (triggerType == WorkflowTriggerType.PreSave || triggerType == WorkflowTriggerType.PreDelete) { var workflowTypeService = new WorkflowTypeService(); var workflowType = workflowTypeService.Get(trigger.WorkflowTypeId); if (workflowType != null) { var workflow = Rock.Model.Workflow.Activate(workflowType, trigger.WorkflowName); List <string> workflowErrors; if (!workflow.Process(entity.Dto, out workflowErrors)) { ErrorMessages.AddRange(workflowErrors); return(false); } else { if (workflowType.IsPersisted) { var workflowService = new Rock.Model.WorkflowService(); workflowService.Add(workflow, personId); workflowService.Save(workflow, personId); } } } } else { var transaction = new Rock.Transactions.WorkflowTriggerTransaction(); transaction.Trigger = trigger; transaction.Dto = entity.Dto; transaction.PersonId = personId; Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction); } } return(true); }
/// <summary> /// Returns a <see cref="System.String" /> that represents this instance. /// </summary> /// <returns> /// A <see cref="System.String" /> that represents this instance. /// </returns> public override string ToString() { // TODO ask David if we can EntityType.ToStringSafe() instead of EntityType.Name below return(this.WorkflowName ?? EntityType.Name + " " + WorkflowTriggerType.ConvertToString()); }
/// <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); }
public static List <WorkflowTrigger> Triggers(string entityTypeName, WorkflowTriggerType triggerType) { return(WorkflowTriggersCache.Triggers(entityTypeName, triggerType)); }
/// <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; using ( var rockContext = new RockContext() ) { var workflowTypeService = new WorkflowTypeService( rockContext ); var workflowService = new WorkflowService( rockContext ); // 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 = workflowTypeService.Get( trigger.WorkflowTypeId ); if ( workflowType != null ) { var workflow = Rock.Model.Workflow.Activate( workflowType, trigger.WorkflowName ); List<string> workflowErrors; 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; }
/// <summary> /// Returns a <see cref="System.String" /> that represents this instance. /// </summary> /// <returns> /// A <see cref="System.String" /> that represents this instance. /// </returns> public override string ToString() { return(this.WorkflowName ?? EntityType.Name + " " + WorkflowTriggerType.ConvertToString()); }
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); }
private bool TriggerWorkflows( IEntity entity, WorkflowTriggerType triggerType, PersonAlias personAlias ) { Dictionary<string, PropertyInfo> properties = null; 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; }
/// <summary> /// Creates the workflow trigger. /// </summary> /// <param name="entityTypeName">Name of the entity type.</param> /// <param name="triggerType">Type of the trigger.</param> /// <param name="qualifierColumn">The qualifier column.</param> /// <param name="qualifierValue">The qualifier value.</param> /// <param name="workflowTypeGuid">The workflow type unique identifier.</param> /// <param name="description">The description.</param> /// <param name="guid">The unique identifier.</param> public void CreateWorkflowTrigger( string entityTypeName, WorkflowTriggerType triggerType, string qualifierColumn, string qualifierValue, string workflowTypeGuid, string description, string guid = null ) { guid = guid != null ? string.Format( "'{0}'", guid ) : "NEWID()"; Migration.Sql( string.Format( @" INSERT INTO [dbo].[WorkflowTrigger] ([IsSystem] ,[EntityTypeId] ,[EntityTypeQualifierColumn] ,[EntityTypeQualifierValue] ,[WorkflowTypeId] ,[WorkflowTriggerType] ,[WorkflowName] ,[Guid] ,[IsActive]) VALUES (0 ,(SELECT Id FROM EntityType WHERE NAME = 'Rock.Model.{0}') ,'{1}' ,'{2}' ,(SELECT Id FROM WorkflowType WHERE Guid = '{3}') ,{4} ,'{5}' ,{6} ,1)", entityTypeName, qualifierColumn, qualifierValue, workflowTypeGuid, (int)triggerType, description, guid ) ); }