/// <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 = new WorkflowActivityTypeService( rockContext ).Queryable()
                .Where( a => a.Guid.Equals( guid ) ).FirstOrDefault();

            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;
        }
        protected void ddlActivateNewActivity_SelectedIndexChanged( object sender, EventArgs e )
        {
            ParseControls();

            int? activityTypeId = ddlActivateNewActivity.SelectedValueAsId();
            if (activityTypeId.HasValue)
            {
                var activityType = new WorkflowActivityTypeService(new RockContext()).Get(activityTypeId.Value);
                if (activityType != null)
                {
                    var activity = WorkflowActivity.Activate( activityType, Workflow );
                    activity.ActivityTypeId = activity.ActivityType.Id;
                    activity.Guid = Guid.NewGuid();

                    foreach( var action in activity.Actions)
                    {
                        action.ActionTypeId = action.ActionType.Id;
                        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;
        }
        /// <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 = new WorkflowActivityTypeService( rockContext ).Queryable()
                .Where( a => a.Guid.Equals( workflowActivityGuid ) ).FirstOrDefault();

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

            var entityType = EntityTypeCache.Read( 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;
        }
Beispiel #4
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.ActionType.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 = new WorkflowActivityTypeService( rockContext ).Queryable()
                        .Where( a => a.Guid.Equals( activityGuid.Value ) ).FirstOrDefault();
                    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 );
                        }
                    }
                }

            }
        }