Esempio n. 1
0
        /// <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);
        }
Esempio n. 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;

            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;
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
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;

            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);
        }
Esempio n. 5
0
        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;
        }