Service/Data access class for Rock.Model.Workflow entity objects
Пример #1
0
        /// <summary>
        /// Launch the workflow
        /// </summary>
        protected void LaunchTheWorkflow(string workflowName)
        {
            Guid workflowTypeGuid = Guid.NewGuid();

            if (Guid.TryParse(workflowName, out workflowTypeGuid))
            {
                var workflowTypeService = new WorkflowTypeService();
                var workflowType        = workflowTypeService.Get(workflowTypeGuid);
                if (workflowType != null)
                {
                    var workflow = Rock.Model.Workflow.Activate(workflowType, workflowName);

                    List <string> workflowErrors;
                    if (workflow.Process(out workflowErrors))
                    {
                        if (workflowType.IsPersisted)
                        {
                            var workflowService = new Rock.Model.WorkflowService();
                            workflowService.Add(workflow, CurrentPersonId);
                            workflowService.Save(workflow, CurrentPersonId);
                        }
                    }
                }
            }
        }
Пример #2
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 workflow = action.Activity.Workflow;
            workflow.IsPersisted = true;

            if ( GetAttributeValue( action, "PersistImmediately" ).AsBoolean( false ) )
            {
                var service = new WorkflowService( rockContext );
                if ( workflow.Id == 0 )
                {
                    service.Add( workflow );
                }

                rockContext.WrapTransaction( () =>
                {
                    rockContext.SaveChanges();
                    workflow.SaveAttributeValues( rockContext );
                    foreach ( var activity in workflow.Activities )
                    {
                        activity.SaveAttributeValues( rockContext );
                    }
                } );
            }

            action.AddLogEntry( "Updated workflow to be persisted!" );

            return true;
        }
Пример #3
0
        /// <summary>
        /// Launch the workflow
        /// </summary>
        protected void LaunchTheWorkflow(string workflowName)
        {
            Guid workflowTypeGuid = Guid.NewGuid();

            if (Guid.TryParse(workflowName, out workflowTypeGuid))
            {
                var rockContext         = new RockContext();
                var workflowTypeService = new WorkflowTypeService(rockContext);
                var workflowType        = workflowTypeService.Get(workflowTypeGuid);
                if (workflowType != null)
                {
                    var workflow = Rock.Model.Workflow.Activate(workflowType, workflowName);

                    List <string> workflowErrors;
                    if (workflow.Process(rockContext, out workflowErrors))
                    {
                        if (workflow.IsPersisted || workflowType.IsPersisted)
                        {
                            var workflowService = new Rock.Model.WorkflowService(rockContext);
                            workflowService.Add(workflow);

                            rockContext.WrapTransaction(() =>
                            {
                                rockContext.SaveChanges();
                                workflow.SaveAttributeValues(rockContext);
                                foreach (var activity in workflow.Activities)
                                {
                                    activity.SaveAttributeValues(rockContext);
                                }
                            });
                        }
                    }
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Job that will close workflows.
        /// 
        /// Called by the <see cref="IScheduler" /> when a
        /// <see cref="ITrigger" /> fires that is associated with
        /// the <see cref="IJob" />.
        /// </summary>
        public virtual void Execute( IJobExecutionContext context )
        {
            JobDataMap dataMap = context.JobDetail.JobDataMap;

            // run a SQL query to do something
            var workflowTypeGuids = dataMap.GetString( "WorkflowTypes" ).Split(',').Select(Guid.Parse).ToList();
            int? expirationAge = dataMap.GetString( "ExpirationAge" ).AsIntegerOrNull();
            string closeStatus = dataMap.GetString( "CloseStatus" );

            RockContext rockContext = new RockContext();
            var workflowService = new WorkflowService( rockContext );

            var qry = workflowService.Queryable()
                        .Where( w => workflowTypeGuids.Contains( w.WorkflowType.Guid ) );

            if ( expirationAge.HasValue )
            {
                var expirationDate = RockDateTime.Now.AddMinutes( expirationAge.Value );

                qry = qry.Where(w => w.CreatedDateTime <= expirationDate );
            }

            var workflows = qry.ToList();

            foreach(var workflow in workflows )
            {
                workflow.MarkComplete();
                workflow.Status = closeStatus;

                rockContext.SaveChanges();
            }
        }
Пример #5
0
        /// <summary>
        /// Launch the workflow
        /// </summary>
        protected void LaunchTheWorkflow(string workflowName)
        {
            Guid workflowTypeGuid = Guid.NewGuid();
            if ( Guid.TryParse( workflowName, out workflowTypeGuid ) )
            {
                var rockContext = new RockContext();
                var workflowTypeService = new WorkflowTypeService(rockContext);
                var workflowType = workflowTypeService.Get( workflowTypeGuid );
                if ( workflowType != null )
                {
                    var workflow = Rock.Model.Workflow.Activate( workflowType, workflowName );

                    List<string> workflowErrors;
                    if ( workflow.Process( rockContext, out workflowErrors ) )
                    {
                        if ( workflow.IsPersisted || workflowType.IsPersisted )
                        {
                            var workflowService = new Rock.Model.WorkflowService(rockContext);
                            workflowService.Add( workflow );

                            rockContext.WrapTransaction( () =>
                            {
                                rockContext.SaveChanges();
                                workflow.SaveAttributeValues( rockContext );
                                foreach ( var activity in workflow.Activities )
                                {
                                    activity.SaveAttributeValues( rockContext );
                                }
                            } );
                        }
                    }
                }
            }     
        }
Пример #6
0
        /// <summary>
        /// Handles the FileUploaded event of the fsFile 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 fsFile_FileUploaded(object sender, EventArgs e)
        {
            using (new Rock.Data.UnitOfWorkScope())
            {
                var        binaryFileService = new BinaryFileService();
                BinaryFile binaryFile        = null;
                if (fsFile.BinaryFileId.HasValue)
                {
                    binaryFile = binaryFileService.Get(fsFile.BinaryFileId.Value);
                }

                if (binaryFile != null)
                {
                    if (!string.IsNullOrWhiteSpace(tbName.Text))
                    {
                        binaryFile.FileName = tbName.Text;
                    }

                    // set binaryFile.Id to original id since the UploadedFile is a temporary binaryFile with a different id
                    binaryFile.Id               = hfBinaryFileId.ValueAsInt();
                    binaryFile.Description      = tbDescription.Text;
                    binaryFile.BinaryFileTypeId = ddlBinaryFileType.SelectedValueAsInt();
                    if (binaryFile.BinaryFileTypeId.HasValue)
                    {
                        binaryFile.BinaryFileType = new BinaryFileTypeService().Get(binaryFile.BinaryFileTypeId.Value);
                    }

                    binaryFile.LoadAttributes();
                    Rock.Attribute.Helper.GetEditValues(phAttributes, binaryFile);

                    // Process uploaded file using an optional workflow
                    Guid workflowTypeGuid = Guid.NewGuid();
                    if (Guid.TryParse(GetAttributeValue("Workflow"), out workflowTypeGuid))
                    {
                        var workflowTypeService = new WorkflowTypeService();
                        var workflowType        = workflowTypeService.Get(workflowTypeGuid);
                        if (workflowType != null)
                        {
                            var workflow = Workflow.Activate(workflowType, binaryFile.FileName);

                            List <string> workflowErrors;
                            if (workflow.Process(binaryFile, out workflowErrors))
                            {
                                binaryFile = binaryFileService.Get(binaryFile.Id);

                                if (workflowType.IsPersisted)
                                {
                                    var workflowService = new Rock.Model.WorkflowService();
                                    workflowService.Add(workflow, CurrentPersonId);
                                    workflowService.Save(workflow, CurrentPersonId);
                                }
                            }
                        }
                    }

                    ShowBinaryFileDetail(binaryFile);
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Launches the workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="connectionWorkflow">The connection workflow.</param>
        /// <param name="name">The name.</param>
        private void LaunchWorkflow(RockContext rockContext, Attendance attendance, PersonAlias pagePersonAlias, WorkflowType pageWorkflowType)
        {
            if (pageWorkflowType != null)
            {
                var workflow = Rock.Model.Workflow.Activate(pageWorkflowType, pageWorkflowType.WorkTerm, rockContext);
                if (workflow != null)
                {
                    workflow.SetAttributeValue("Person", attendance.PersonAlias.Guid);
                    workflow.SetAttributeValue("PagePerson", pagePersonAlias.Guid);

                    var workflowService = new Rock.Model.WorkflowService(rockContext);

                    List <string> workflowErrors;
                    if (workflowService.Process(workflow, attendance, out workflowErrors))
                    {
                        if (workflow.Id != 0)
                        {
                            if (workflow.HasActiveEntryForm(CurrentPerson))
                            {
                                var qryParam = new Dictionary <string, string>();
                                qryParam.Add("WorkflowTypeId", pageWorkflowType.Id.ToString());
                                qryParam.Add("WorkflowId", workflow.Id.ToString());
                                NavigateToLinkedPage("WorkflowEntryPage", qryParam);
                            }
                            else
                            {
                                mdWorkflowLaunched.Show(string.Format("A '{0}' workflow has been started.",
                                                                      pageWorkflowType.Name), ModalAlertType.Information);
                            }
                        }
                        else
                        {
                            mdWorkflowLaunched.Show(string.Format("A '{0}' workflow was processed (but not persisted).",
                                                                  pageWorkflowType.Name), ModalAlertType.Information);
                        }
                    }
                    else
                    {
                        mdWorkflowLaunched.Show("Workflow Processing Error(s):<ul><li>" + workflowErrors.AsDelimited("</li><li>") + "</li></ul>", ModalAlertType.Information);
                    }
                    return;
                }
            }

            Dictionary <string, string> qParams = new Dictionary <string, string>();

            qParams.Add("AttendanceId", attendance.Id.ToString());
            qParams.Add("PagePersonId", pagePersonAlias.Person.Id.ToString());
            NavigateToLinkedPage("WorkflowEntryPage", qParams);
        }
Пример #8
0
        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a <see cref="ITrigger" />
        /// fires that is associated with the <see cref="IJob" />.
        /// </summary>
        /// <param name="context">The execution context.</param>
        /// <remarks>
        /// The implementation may wish to set a  result object on the
        /// JobExecutionContext before this method exits.  The result itself
        /// is meaningless to Quartz, but may be informative to
        /// <see cref="IJobListener" />s or
        /// <see cref="ITriggerListener" />s that are watching the job's
        /// execution.
        /// </remarks>
        public virtual void Execute( IJobExecutionContext context )
        {
            var service = new WorkflowService();

            foreach ( var workflow in service.GetActive() )
            {
                if ( !workflow.LastProcessedDateTime.HasValue ||
                    DateTime.Now.Subtract( workflow.LastProcessedDateTime.Value ).TotalSeconds >= workflow.WorkflowType.ProcessingIntervalSeconds )
                {
                    var errorMessages = new List<string>();
                    service.Process( workflow, null, out errorMessages );
                }
            }
        }
Пример #9
0
        /// <summary>
        /// Launch the workflow
        /// </summary>
        protected void LaunchTheWorkflow(string workflowName, IJobExecutionContext context)
        {
            Guid workflowTypeGuid = Guid.NewGuid();

            if (Guid.TryParse(workflowName, out workflowTypeGuid))
            {
                var workflowType = WorkflowTypeCache.Get(workflowTypeGuid);
                if (workflowType != null && (workflowType.IsActive ?? true))
                {
                    var workflow = Rock.Model.Workflow.Activate(workflowType, workflowName);

                    List <string> workflowErrors;
                    var           processed = new Rock.Model.WorkflowService(new RockContext()).Process(workflow, out workflowErrors);
                    context.Result = (processed ? "Processed " : "Did not process ") + workflow.ToString();
                }
            }
        }
Пример #10
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>();

            Guid? workflowGuid = GetAttributeValue( action, "Workflow", true ).AsGuidOrNull();
            if ( workflowGuid.HasValue )
            {
                using ( var newRockContext = new RockContext() )
                {
                    var workflowService = new WorkflowService( newRockContext );
                    var workflow = workflowService.Get( workflowGuid.Value );
                    if ( workflow != null )
                    {
                        string status = GetAttributeValue( action, "Status" ).ResolveMergeFields( GetMergeFields( action ) );
                        workflow.Status = status;
                        workflow.AddLogEntry( string.Format( "Status set to '{0}' by another workflow: {1}", status, action.Activity.Workflow ) );
                        newRockContext.SaveChanges();

                        action.AddLogEntry( string.Format( "Set Status to '{0}' on another workflow: {1}", status, workflow ) );

                        bool processNow = GetAttributeValue( action, "ProcessNow" ).AsBoolean();
                        if ( processNow )
                        {
                            var processErrors = new List<string>();
                            if ( !workflowService.Process( workflow, out processErrors ) )
                            {
                                action.AddLogEntry( "Error(s) occurred processing target workflow: " + processErrors.AsDelimited( ", " ) );
                            }
                        }

                        return true;
                    }
                }

                action.AddLogEntry( "Could not find selected workflow." );
            }
            else
            {
                action.AddLogEntry( "Workflow attribute was not set." );
                return true;    // Continue processing in this case
            }

            return false;
        }
        /// <summary>
        /// Handles the Delete event of the gWorkflows control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param>
        protected void gWorkflows_Delete( object sender, RowEventArgs e )
        {
            var rockContext = new RockContext();
            WorkflowService workflowService = new WorkflowService( rockContext );
            Workflow workflow = workflowService.Get( (int)e.RowKeyValue );
            if ( workflow != null )
            {
                string errorMessage;
                if ( !workflowService.CanDelete( workflow, out errorMessage ) )
                {
                    mdGridWarning.Show( errorMessage, ModalAlertType.Information );
                    return;
                }

                workflowService.Delete( workflow );
                rockContext.SaveChanges();
            }

            BindGrid();
        }
Пример #12
0
        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a <see cref="ITrigger" />
        /// fires that is associated with the <see cref="IJob" />.
        /// </summary>
        /// <param name="context">The execution context.</param>
        /// <remarks>
        /// The implementation may wish to set a  result object on the
        /// JobExecutionContext before this method exits.  The result itself
        /// is meaningless to Quartz, but may be informative to
        /// <see cref="IJobListener" />s or
        /// <see cref="ITriggerListener" />s that are watching the job's
        /// execution.
        /// </remarks>
        public virtual void Execute( IJobExecutionContext context )
        {
            foreach ( var workflowId in new WorkflowService( new RockContext() ).GetActive().Select(a => a.Id).ToList() )
            {
                // create a new rockContext and service for every workflow to prevent a build-up of Context.ChangeTracker.Entries()
                var rockContext = new RockContext();
                var workflowService = new WorkflowService( rockContext );
                var workflow = workflowService.Queryable( "WorkflowType" ).FirstOrDefault( a => a.Id == workflowId );
                if ( workflow != null )
                {
                    if ( !workflow.LastProcessedDateTime.HasValue ||
                        RockDateTime.Now.Subtract( workflow.LastProcessedDateTime.Value ).TotalSeconds >= ( workflow.WorkflowType.ProcessingIntervalSeconds ?? 0 ) )
                    {
                        var errorMessages = new List<string>();

                        workflowService.Process( workflow, out errorMessages );
                    }
                }
            }
        }
Пример #13
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);
        }
        /// <summary>
        /// Execute method to write transaction to the database.
        /// </summary>
        public void Execute()
        {
            if (Trigger != null)
            {
                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(Dto, out workflowErrors))
                    {
                        if (workflowType.IsPersisted)
                        {
                            var workflowService = new Rock.Model.WorkflowService();
                            workflowService.Add(workflow, PersonId);
                            workflowService.Save(workflow, PersonId);
                        }
                    }
                }
            }
        }
Пример #15
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 workflow = action.Activity.Workflow;
            if ( workflow.Id >= 0 )
            {
                // Create a new RockContext so that any previous updates are ignored and
                // workflow can be sucessfully deleted
                var newRockContext = new RockContext();
                var workflowService = new WorkflowService( newRockContext );
                var workflowToDelete = workflowService.Get( workflow.Id );
                if ( workflowToDelete != null )
                {
                    workflowService.Delete( workflowToDelete );
                    newRockContext.SaveChanges();
                }
            }

            workflow.Id = 0;
            workflow.IsPersisted = false;

            return true;
        }
Пример #16
0
        /// <summary>
        /// Execute method to write transaction to the database.
        /// </summary>
        public void Execute()
        {
            if ( Trigger != null )
            {
                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, out workflowErrors ) )
                    {
                        if ( workflowType.IsPersisted )
                        {
                            var workflowService = new Rock.Model.WorkflowService();
                            workflowService.Add( workflow, PersonId );
                            workflowService.Save( workflow, PersonId );
                        }
                    }
                }
            }
        }
Пример #17
0
        /// <summary>
        /// Launch the workflow
        /// </summary>
        protected void LaunchTheWorkflow(string workflowName)
        {
            Guid workflowTypeGuid = Guid.NewGuid();
            if ( Guid.TryParse( workflowName, out workflowTypeGuid ) )
            {
                var workflowTypeService = new WorkflowTypeService();
                var workflowType = workflowTypeService.Get( workflowTypeGuid );
                if ( workflowType != null )
                {
                    var workflow = Rock.Model.Workflow.Activate( workflowType, workflowName );

                    List<string> workflowErrors;
                    if ( workflow.Process( out workflowErrors ) )
                    {
                        if ( workflowType.IsPersisted )
                        {
                            var workflowService = new Rock.Model.WorkflowService();
                            workflowService.Add( workflow, CurrentPersonId );
                            workflowService.Save( workflow, CurrentPersonId );
                        }
                    }
                }
            }     
        }
        /// <summary>
        /// Execute method to write transaction to the database.
        /// </summary>
        public void Execute()
        {
            if (Trigger != null)
            {
                using (var rockContext = new RockContext())
                {
                    var workflowTypeService = new WorkflowTypeService(rockContext);
                    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))
                        {
                            if (workflow.IsPersisted || workflowType.IsPersisted)
                            {
                                var workflowService = new Rock.Model.WorkflowService(rockContext);
                                workflowService.Add(workflow);

                                rockContext.WrapTransaction(() =>
                                {
                                    rockContext.SaveChanges();
                                    workflow.SaveAttributeValues(rockContext);
                                    foreach (var activity in workflow.Activities)
                                    {
                                        activity.SaveAttributeValues(rockContext);
                                    }
                                });
                            }
                        }
                    }
                }
            }
        }
Пример #19
0
        private void LoadWorkflowType()
        {
            if ( _rockContext == null )
            {
                _rockContext = new RockContext();
            }

            if ( _workflowService == null )
            {
                _workflowService = new WorkflowService( _rockContext );
            }

            if ( _workflowTypeService == null )
            {
                _workflowTypeService = new WorkflowTypeService( _rockContext );
            }

            // Get the workflow type id (initial page request)
            if ( !WorkflowTypeId.HasValue )
            {
                // Get workflow type set by attribute value
                Guid workflowTypeguid = GetAttributeValue( "WorkflowType" ).AsGuid();
                if ( !workflowTypeguid.IsEmpty() )
                {
                    _workflowType = _workflowTypeService.Get( workflowTypeguid );
                }

                // If an attribute value was not provided, check for query/route value
                if ( _workflowType != null )
                {
                    WorkflowTypeId = _workflowType.Id;
                    ConfiguredType = true;
                }
                else
                {
                    WorkflowTypeId = PageParameter( "WorkflowTypeId" ).AsIntegerOrNull();
                    ConfiguredType = false;
                }
            }

            // Get the workflow type
            if ( _workflowType == null && WorkflowTypeId.HasValue )
            {
                _workflowType = _workflowTypeService.Get( WorkflowTypeId.Value );
            }
        }
        /// <summary>
        /// Handles the FileUploaded event of the fsFile 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 fsFile_FileUploaded( object sender, EventArgs e )
        {
            var rockContext = new RockContext();
            var binaryFileService = new BinaryFileService( rockContext );
            BinaryFile binaryFile = null;
            if ( fsFile.BinaryFileId.HasValue )
            {
                binaryFile = binaryFileService.Get( fsFile.BinaryFileId.Value );
            }

            if ( binaryFile != null )
            {
                if ( !string.IsNullOrWhiteSpace( tbName.Text ) )
                {
                    binaryFile.FileName = tbName.Text;
                }

                // set binaryFile.Id to original id since the UploadedFile is a temporary binaryFile with a different id
                binaryFile.Id = hfBinaryFileId.ValueAsInt();
                binaryFile.Description = tbDescription.Text;
                binaryFile.BinaryFileTypeId = ddlBinaryFileType.SelectedValueAsInt();
                if ( binaryFile.BinaryFileTypeId.HasValue )
                {
                    binaryFile.BinaryFileType = new BinaryFileTypeService( rockContext ).Get( binaryFile.BinaryFileTypeId.Value );
                }

                var tempList = OrphanedBinaryFileIdList;
                tempList.Add( fsFile.BinaryFileId.Value );
                OrphanedBinaryFileIdList = tempList;

                // load attributes, then get the attribute values from the UI
                binaryFile.LoadAttributes();
                Rock.Attribute.Helper.GetEditValues( phAttributes, binaryFile );

                // Process uploaded file using an optional workflow (which will probably populate attribute values)
                Guid workflowTypeGuid = Guid.NewGuid();
                if ( Guid.TryParse( GetAttributeValue( "Workflow" ), out workflowTypeGuid ) )
                {
                    try
                    {
                        // temporarily set the binaryFile.Id to the uploaded binaryFile.Id so that workflow can do stuff with it
                        binaryFile.Id = fsFile.BinaryFileId ?? 0;

                        // create a rockContext for the workflow so that it can save it's changes, without
                        var workflowRockContext = new RockContext();
                        var workflowTypeService = new WorkflowTypeService( workflowRockContext );
                        var workflowType = workflowTypeService.Get( workflowTypeGuid );
                        if ( workflowType != null )
                        {
                            var workflow = Workflow.Activate( workflowType, binaryFile.FileName );

                            List<string> workflowErrors;
                            if ( workflow.Process( workflowRockContext, binaryFile, out workflowErrors ) )
                            {
                                binaryFile = binaryFileService.Get( binaryFile.Id );

                                if ( workflow.IsPersisted || workflowType.IsPersisted )
                                {
                                    var workflowService = new Rock.Model.WorkflowService( workflowRockContext );
                                    workflowService.Add( workflow );
                                    workflowRockContext.SaveChanges();
                                }
                            }
                        }
                    }
                    finally
                    {
                        // set binaryFile.Id to original id again since the UploadedFile is a temporary binaryFile with a different id
                        binaryFile.Id = hfBinaryFileId.ValueAsInt();
                    }
                }

                ShowBinaryFileDetail( binaryFile );
            }
        }
Пример #21
0
        /// <summary>
        /// Launches the workflow.
        /// </summary>
        /// <param name="workflowTypeGuid">The workflow type unique identifier.</param>
        /// <param name="family">The family.</param>
        private void LaunchWorkflow( Guid workflowTypeGuid, Group family )
        {
            int adultRoleId = GroupTypeCache.Read( SystemGuid.GroupType.GROUPTYPE_FAMILY.AsGuid() ).Roles.Where(r => r.Guid == SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT.AsGuid()).FirstOrDefault().Id;

            var headOfHouse = family.Members.Where( m => m.GroupRoleId == adultRoleId ).OrderBy( m => m.Person.Gender ).FirstOrDefault();

            // don't launch a workflow if no adult is present in the family
            if ( headOfHouse != null && headOfHouse.Person != null && headOfHouse.Person.PrimaryAlias != null ) {
                var spouse = family.Members.Where( m => m.GroupRoleId == adultRoleId && m.PersonId != headOfHouse.Person.Id ).FirstOrDefault();

                using ( var rockContext = new RockContext() )
                {
                    var workflowTypeService = new WorkflowTypeService( rockContext );
                    var workflowType = workflowTypeService.Get( workflowTypeGuid );
                    if ( workflowType != null )
                    {
                        var workflowService = new WorkflowService( rockContext );
                        var workflow = Rock.Model.Workflow.Activate( workflowType, headOfHouse.Person.FullName, rockContext );
                        workflowService.Add( workflow );
                        rockContext.SaveChanges();

                        workflow.SetAttributeValue( "Family", family.Guid );
                        workflow.SetAttributeValue( "HeadOfHouse", headOfHouse.Person.PrimaryAlias.Guid );

                        if ( family.Campus != null )
                        {
                            workflow.SetAttributeValue( "Campus", family.Campus.Guid );
                        }

                        if ( spouse != null && spouse.Person != null && spouse.Person.PrimaryAlias != null )
                        {
                            workflow.SetAttributeValue( "Spouse", spouse.Person.PrimaryAlias.Guid );
                        }

                        workflow.SaveAttributeValues();
                    }
                }
            }
        }
Пример #22
0
        /// <summary>
        /// Activates and processes a workflow activity.  If the workflow has not yet been activated, it will
        /// also be activated
        /// </summary>
        /// <param name="activityName">Name of the activity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        protected bool ProcessActivity( string activityName, out List<string> errorMessages )
        {
            errorMessages = new List<string>();

            Guid? guid = GetAttributeValue( "WorkflowType" ).AsGuidOrNull();
            if ( guid.HasValue )
            {
                using ( var rockContext = new RockContext() )
                {
                    var workflowTypeService = new WorkflowTypeService( rockContext );
                    var workflowService = new WorkflowService( rockContext );

                    var workflowType = workflowTypeService.Queryable( "ActivityTypes" )
                        .Where( w => w.Guid.Equals( guid.Value ) )
                        .FirstOrDefault();

                    if ( workflowType != null )
                    {
                        if ( CurrentWorkflow == null )
                        {
                            CurrentWorkflow = Rock.Model.Workflow.Activate( workflowType, CurrentCheckInState.Kiosk.Device.Name, rockContext );

                            if ( IsOverride )
                            {
                                CurrentWorkflow.SetAttributeValue( "Override", "True" );
                            }
                        }

                        var activityType = workflowType.ActivityTypes.Where( a => a.Name == activityName ).FirstOrDefault();
                        if ( activityType != null )
                        {
                            WorkflowActivity.Activate( activityType, CurrentWorkflow, rockContext );
                            if ( workflowService.Process( CurrentWorkflow, CurrentCheckInState, out errorMessages ) )
                            {
                                // Keep workflow active for continued processing
                                CurrentWorkflow.CompletedDateTime = null;

                                return true;
                            }
                        }
                        else
                        {
                            errorMessages.Add( string.Format( "Workflow type does not have a '{0}' activity type", activityName ) );
                        }
                    }
                    else
                    {
                        errorMessages.Add( "Invalid Workflow Type" );
                    }
                }
            }

            return false;
        }
Пример #23
0
        /// <summary>
        /// Launch a specific workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="workflowTrigger">The connection workflow.</param>
        /// <param name="target">The name.</param>
        private bool LaunchWorkflow(RockContext rockContext, Step target, StepWorkflowTrigger workflowTrigger)
        {
            if (target == null ||
                workflowTrigger == null)
            {
                mdWorkflowResult.Show("Workflow Processing Failed:<ul><li>The workflow parameters are invalid.</li></ul>", ModalAlertType.Information);
                return(false);
            }

            var workflowType = workflowTrigger.WorkflowType;

            if (workflowType == null || !(workflowType.IsActive ?? true))
            {
                mdWorkflowResult.Show("Workflow Processing Failed:<ul><li>This workflow is unavailable.</li></ul>", ModalAlertType.Information);
                return(false);
            }

            if (!workflowType.IsAuthorized(Authorization.VIEW, CurrentPerson))
            {
                mdWorkflowResult.Show("Workflow Processing Failed:<ul><li>You are not authorized to access this workflow.</li></ul>", ModalAlertType.Information);
                return(false);
            }

            var workflowTypeCache = WorkflowTypeCache.Get(workflowType);

            var workflow = Rock.Model.Workflow.Activate(workflowTypeCache, workflowTrigger.WorkflowType.WorkTerm, rockContext);

            if (workflow == null)
            {
                mdWorkflowResult.Show("Workflow Processing Failed:<ul><li>The workflow could not be activated.</li></ul>", ModalAlertType.Information);
                return(false);
            }

            var workflowService = new Rock.Model.WorkflowService(rockContext);

            List <string> workflowErrors;

            var processed = workflowService.Process(workflow, target, out workflowErrors);

            if (processed)
            {
                if (workflow.HasActiveEntryForm(CurrentPerson))
                {
                    // If the workflow has a user entry form that can be displayed for the current user, show it now.
                    // Note that for a non-persisted workflow, a new instance of the workflow will be created after the user form is saved.
                    var qryParam = new Dictionary <string, string>();

                    qryParam.Add("WorkflowTypeId", workflowType.Id.ToString());

                    if (workflow.Id != 0)
                    {
                        qryParam.Add("WorkflowId", workflow.Id.ToString());
                    }

                    var entryPage = this.GetAttributeValue(AttributeKey.WorkflowEntryPage);

                    if (string.IsNullOrWhiteSpace(entryPage))
                    {
                        mdWorkflowResult.Show("A Workflow Entry Page has not been configured for this block.", ModalAlertType.Alert);
                        return(false);
                    }

                    NavigateToLinkedPage(AttributeKey.WorkflowEntryPage, qryParam);
                    return(false);
                }
                else if (workflow.Id != 0)
                {
                    // The workflow has been started and persisted, but it has no requirement for user interaction.
                    mdWorkflowResult.Show(string.Format("A '{0}' workflow has been started.", workflowType.Name), ModalAlertType.Information);
                    return(true);
                }
                else
                {
                    // The workflow has run to completion, and it has no requirement for user interaction.
                    mdWorkflowResult.Show(string.Format("A '{0}' workflow was processed.", workflowType.Name), ModalAlertType.Information);

                    return(true);
                }
            }
            else
            {
                mdWorkflowResult.Show("Workflow Processing Failed:<ul><li>" + workflowErrors.AsDelimited("</li><li>") + "</li></ul>", ModalAlertType.Information);
            }

            return(false);
        }
        /// <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;
        }
Пример #25
0
        /// <summary>
        /// Handles the Delete event of the gWorkflows control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param>
        protected void gWorkflows_Delete( object sender, RowEventArgs e )
        {
            RockTransactionScope.WrapTransaction( () =>
            {
                WorkflowService workflowService = new WorkflowService();
                Workflow workflow = workflowService.Get( (int)e.RowKeyValue );
                if ( workflow != null )
                {
                    string errorMessage;
                    if ( !workflowService.CanDelete( workflow, out errorMessage ) )
                    {
                        mdGridWarning.Show( errorMessage, ModalAlertType.Information );
                        return;
                    }

                    workflowService.Delete( workflow, CurrentPersonId );
                    workflowService.Save( workflow, CurrentPersonId );
                }
            } );

            BindGrid();
        }
Пример #26
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;
        }
Пример #27
0
        /// <summary>
        /// Handles the FileUploaded event of the fsFile 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 fsFile_FileUploaded(object sender, EventArgs e)
        {
            var        rockContext       = new RockContext();
            var        binaryFileService = new BinaryFileService(rockContext);
            BinaryFile binaryFile        = null;

            if (fsFile.BinaryFileId.HasValue)
            {
                binaryFile = binaryFileService.Get(fsFile.BinaryFileId.Value);
            }

            if (binaryFile != null)
            {
                if (!string.IsNullOrWhiteSpace(tbName.Text))
                {
                    binaryFile.FileName = tbName.Text;
                }

                // set binaryFile.Id to original id since the UploadedFile is a temporary binaryFile with a different id
                binaryFile.Id               = hfBinaryFileId.ValueAsInt();
                binaryFile.Description      = tbDescription.Text;
                binaryFile.BinaryFileTypeId = ddlBinaryFileType.SelectedValueAsInt();
                if (binaryFile.BinaryFileTypeId.HasValue)
                {
                    binaryFile.BinaryFileType = new BinaryFileTypeService(rockContext).Get(binaryFile.BinaryFileTypeId.Value);
                }

                var tempList = OrphanedBinaryFileIdList;
                tempList.Add(fsFile.BinaryFileId.Value);
                OrphanedBinaryFileIdList = tempList;

                // load attributes, then get the attribute values from the UI
                binaryFile.LoadAttributes();
                Rock.Attribute.Helper.GetEditValues(phAttributes, binaryFile);

                // Process uploaded file using an optional workflow (which will probably populate attribute values)
                Guid workflowTypeGuid = Guid.NewGuid();
                if (Guid.TryParse(GetAttributeValue("Workflow"), out workflowTypeGuid))
                {
                    try
                    {
                        // temporarily set the binaryFile.Id to the uploaded binaryFile.Id so that workflow can do stuff with it
                        binaryFile.Id = fsFile.BinaryFileId ?? 0;

                        // create a rockContext for the workflow so that it can save it's changes, without
                        var workflowRockContext = new RockContext();
                        var workflowTypeService = new WorkflowTypeService(workflowRockContext);
                        var workflowType        = workflowTypeService.Get(workflowTypeGuid);
                        if (workflowType != null)
                        {
                            var workflow = Workflow.Activate(workflowType, binaryFile.FileName);

                            List <string> workflowErrors;
                            if (workflow.Process(binaryFile, out workflowErrors))
                            {
                                binaryFile = binaryFileService.Get(binaryFile.Id);

                                if (workflowType.IsPersisted)
                                {
                                    var workflowService = new Rock.Model.WorkflowService(workflowRockContext);
                                    workflowService.Add(workflow);
                                    workflowRockContext.SaveChanges();
                                }
                            }
                        }
                    }
                    finally
                    {
                        // set binaryFile.Id to original id again since the UploadedFile is a temporary binaryFile with a different id
                        binaryFile.Id = hfBinaryFileId.ValueAsInt();
                    }
                }

                ShowBinaryFileDetail(binaryFile);
            }
        }
        /// <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 )
        {
            var rockContext = new RockContext();
            var service = new WorkflowService( rockContext );

            ParseControls( rockContext, true );

            Workflow dbWorkflow = null;

            if ( Workflow != null )
            {
                dbWorkflow = service.Get( Workflow.Id );

                if ( dbWorkflow != null )
                {
                    if ( !dbWorkflow.CompletedDateTime.HasValue && Workflow.CompletedDateTime.HasValue )
                    {
                        dbWorkflow.AddLogEntry( "Workflow Manually Completed." );
                        dbWorkflow.CompletedDateTime = Workflow.CompletedDateTime;
                    }
                    else if ( dbWorkflow.CompletedDateTime.HasValue && !Workflow.CompletedDateTime.HasValue )
                    {
                        dbWorkflow.AddLogEntry( "Workflow Manually Re-Activated." );
                        dbWorkflow.CompletedDateTime = null;
                    }

                    if ( dbWorkflow.Name.Trim() != Workflow.Name.Trim() )
                    {
                        dbWorkflow.AddLogEntry( string.Format( "Workflow name manually changed from '{0}' to '{0}'.", dbWorkflow.Name, tbName.Text ) );
                        dbWorkflow.Name = Workflow.Name;
                    }

                    if ( dbWorkflow.Status.Trim() != Workflow.Status.Trim() )
                    {
                        dbWorkflow.AddLogEntry( string.Format( "Workflow status manually changed from '{0}' to '{0}'.", dbWorkflow.Status, tbStatus.Text ) );
                        dbWorkflow.Status = Workflow.Status;
                    }

                    if ( !dbWorkflow.InitiatorPersonAliasId.Equals(Workflow.InitiatorPersonAliasId))
                    {
                        dbWorkflow.AddLogEntry( string.Format( "Workflow status manually changed from '{0}' to '{0}'.",
                            dbWorkflow.InitiatorPersonAlias != null ? dbWorkflow.InitiatorPersonAlias.Person.FullName : "",
                            Workflow.InitiatorPersonAlias != null ? Workflow.InitiatorPersonAlias.Person.FullName : "" ) );
                        dbWorkflow.InitiatorPersonAlias = Workflow.InitiatorPersonAlias;
                        dbWorkflow.InitiatorPersonAliasId = Workflow.InitiatorPersonAliasId;
                    }

                    if ( !Page.IsValid || !dbWorkflow.IsValid )
                    {
                        return;
                    }

                    foreach ( var activity in Workflow.Activities )
                    {
                        if ( !activity.IsValid )
                        {
                            return;
                        }
                        foreach ( var action in activity.Actions )
                        {
                            if ( !action.IsValid )
                            {
                                return;
                            }
                        }
                    }

                    rockContext.WrapTransaction( () =>
                    {
                        rockContext.SaveChanges();

                        dbWorkflow.LoadAttributes( rockContext );
                        foreach ( var attributeValue in Workflow.AttributeValues )
                        {
                            dbWorkflow.SetAttributeValue( attributeValue.Key, Workflow.GetAttributeValue( attributeValue.Key ) );
                        }
                        dbWorkflow.SaveAttributeValues( rockContext );

                        WorkflowActivityService workflowActivityService = new WorkflowActivityService( rockContext );
                        WorkflowActionService workflowActionService = new WorkflowActionService( rockContext );

                        var activitiesInUi = new List<Guid>();
                        var actionsInUI = new List<Guid>();
                        foreach ( var activity in Workflow.Activities )
                        {
                            activitiesInUi.Add( activity.Guid );
                            foreach ( var action in activity.Actions )
                            {
                                actionsInUI.Add( action.Guid );
                            }
                        }

                        // delete WorkflowActions that were removed in the UI
                        foreach ( var action in workflowActionService.Queryable()
                            .Where( a =>
                                a.Activity.WorkflowId.Equals( dbWorkflow.Id ) &&
                                !actionsInUI.Contains( a.Guid ) ) )
                        {
                            workflowActionService.Delete( action );
                        }

                        // delete WorkflowActivities that aren't assigned in the UI anymore
                        foreach ( var activity in workflowActivityService.Queryable()
                            .Where( a =>
                                a.WorkflowId.Equals( dbWorkflow.Id ) &&
                                !activitiesInUi.Contains( a.Guid ) ) )
                        {
                            workflowActivityService.Delete( activity );
                        }

                        rockContext.SaveChanges();

                        // add or update WorkflowActivities(and Actions) that are assigned in the UI
                        foreach ( var editorWorkflowActivity in Workflow.Activities )
                        {
                            // Add or Update the activity type
                            WorkflowActivity workflowActivity = dbWorkflow.Activities.FirstOrDefault( a => a.Guid.Equals( editorWorkflowActivity.Guid ) );
                            if ( workflowActivity == null )
                            {
                                workflowActivity = new WorkflowActivity();
                                workflowActivity.ActivityTypeId = editorWorkflowActivity.ActivityTypeId;
                                dbWorkflow.Activities.Add( workflowActivity );
                            }

                            workflowActivity.AssignedPersonAliasId = editorWorkflowActivity.AssignedPersonAliasId;
                            workflowActivity.AssignedGroupId = editorWorkflowActivity.AssignedGroupId;
                            workflowActivity.ActivatedDateTime = editorWorkflowActivity.ActivatedDateTime;
                            workflowActivity.LastProcessedDateTime = editorWorkflowActivity.LastProcessedDateTime;

                            if ( !workflowActivity.CompletedDateTime.HasValue && editorWorkflowActivity.CompletedDateTime.HasValue )
                            {
                                workflowActivity.AddLogEntry( "Activity Manually Completed." );
                                workflowActivity.CompletedDateTime = RockDateTime.Now;
                            }
                            if ( workflowActivity.CompletedDateTime.HasValue && !editorWorkflowActivity.CompletedDateTime.HasValue )
                            {
                                workflowActivity.AddLogEntry( "Activity Manually Re-Activated." );
                                workflowActivity.CompletedDateTime = null;
                            }

                            // Save Activity Type
                            rockContext.SaveChanges();

                            // Save ActivityType Attributes
                            workflowActivity.LoadAttributes( rockContext );
                            foreach ( var attributeValue in editorWorkflowActivity.AttributeValues )
                            {
                                workflowActivity.SetAttributeValue( attributeValue.Key, editorWorkflowActivity.GetAttributeValue( attributeValue.Key ) );
                            }
                            workflowActivity.SaveAttributeValues( rockContext );

                            foreach ( var editorWorkflowAction in editorWorkflowActivity.Actions )
                            {
                                WorkflowAction workflowAction = workflowActivity.Actions.FirstOrDefault( a => a.Guid.Equals( editorWorkflowAction.Guid ) );
                                if ( workflowAction == null )
                                {
                                    // New action
                                    workflowAction = new WorkflowAction();
                                    workflowAction.ActionTypeId = editorWorkflowAction.ActionTypeId;
                                    workflowActivity.Actions.Add( workflowAction );
                                }

                                workflowAction.LastProcessedDateTime = editorWorkflowAction.LastProcessedDateTime;
                                workflowAction.FormAction = editorWorkflowAction.FormAction;

                                if ( !workflowAction.CompletedDateTime.HasValue && editorWorkflowAction.CompletedDateTime.HasValue )
                                {
                                    workflowAction.AddLogEntry( "Action Manually Completed." );
                                    workflowAction.CompletedDateTime = RockDateTime.Now;
                                }
                                if ( workflowAction.CompletedDateTime.HasValue && !editorWorkflowAction.CompletedDateTime.HasValue )
                                {
                                    workflowAction.AddLogEntry( "Action Manually Re-Activated." );
                                    workflowAction.CompletedDateTime = null;
                                }
                            }

                            // Save action updates
                            rockContext.SaveChanges();

                        }

                    } );

                }

                Workflow = service
                    .Queryable("WorkflowType,Activities.ActivityType,Activities.Actions.ActionType")
                    .FirstOrDefault( w => w.Id == Workflow.Id );

                var errorMessages = new List<string>();
                service.Process( Workflow, out errorMessages );

            }

            ShowReadonlyDetails();
        }
Пример #29
0
        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a <see cref="ITrigger" />
        /// fires that is associated with the <see cref="IJob" />.
        /// </summary>
        /// <param name="context">The execution context.</param>
        /// <remarks>
        /// The implementation may wish to set a  result object on the
        /// JobExecutionContext before this method exits.  The result itself
        /// is meaningless to Quartz, but may be informative to
        /// <see cref="IJobListener" />s or
        /// <see cref="ITriggerListener" />s that are watching the job's
        /// execution.
        /// </remarks>
        public virtual void Execute( IJobExecutionContext context )
        {
            int workflowsProcessed = 0;
            int workflowErrors = 0;
            int workflowExceptions = 0;
            var ProcessingErrors = new List<string>();
            var exceptionMsgs = new List<string>();

            foreach ( var workflowId in new WorkflowService( new RockContext() )
                .GetActive()
                .Select( w => w.Id )
                .ToList() )
            {
                try
                {
                    // create a new rockContext and service for every workflow to prevent a build-up of Context.ChangeTracker.Entries()
                    var rockContext = new RockContext();
                    var workflowService = new WorkflowService( rockContext );
                    var workflow = workflowService.Queryable( "WorkflowType" ).FirstOrDefault( a => a.Id == workflowId );
                    if ( workflow != null )
                    {
                        try
                        {
                            if ( !workflow.LastProcessedDateTime.HasValue ||
                                RockDateTime.Now.Subtract( workflow.LastProcessedDateTime.Value ).TotalSeconds >= ( workflow.WorkflowType.ProcessingIntervalSeconds ?? 0 ) )
                            {
                                var errorMessages = new List<string>();

                                var processed = workflowService.Process( workflow, out errorMessages );
                                if ( processed )
                                {
                                    workflowsProcessed++;
                                }
                                else
                                {
                                    workflowErrors++;
                                    ProcessingErrors.Add( string.Format( "{0} [{1}] - {2} [{3}]: {4}", workflow.WorkflowType.Name, workflow.WorkflowTypeId, workflow.Name, workflow.Id, errorMessages.AsDelimited( ", " ) ) );
                                }
                            }
                        }
                        catch ( Exception ex )
                        {
                            string workflowDetails = string.Format( "{0} [{1}] - {2} [{3}]", workflow.WorkflowType.Name, workflow.WorkflowTypeId, workflow.Name, workflow.Id );
                            exceptionMsgs.Add( workflowDetails + ": " + ex.Message );
                            throw new Exception( "Exception occurred processing workflow: " + workflowDetails, ex );
                        }
                    }
                }

                catch ( Exception ex )
                {
                    ExceptionLogService.LogException( ex, null );
                    workflowExceptions++;
                }
            }

            var resultMsg = new StringBuilder();
            resultMsg.AppendFormat( "{0} workflows processed", workflowsProcessed );
            if ( workflowErrors > 0 )
            {
                resultMsg.AppendFormat( ", {0} workflows reported an error", workflowErrors );
            }
            if ( workflowExceptions > 0 )
            {
                resultMsg.AppendFormat( ", {0} workflows caused an exception", workflowExceptions );
            }
            if ( ProcessingErrors.Any() )
            {
                resultMsg.Append( Environment.NewLine + ProcessingErrors.AsDelimited( Environment.NewLine ) );
            }

            if ( exceptionMsgs.Any() )
            {
                throw new Exception( "One or more exceptions occurred processing workflows..." + Environment.NewLine + exceptionMsgs.AsDelimited( Environment.NewLine ) );
            }

            context.Result = resultMsg.ToString();
        }
Пример #30
0
        /// <summary>
        /// Launches the workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="connectionWorkflow">The connection workflow.</param>
        /// <param name="name">The name.</param>
        private void LaunchWorkflow( RockContext rockContext, ConnectionRequest connectionRequest, ConnectionWorkflow connectionWorkflow )
        {
            if ( connectionRequest != null && connectionWorkflow != null && connectionWorkflow.WorkflowType != null )
            {
                var workflow = Rock.Model.Workflow.Activate( connectionWorkflow.WorkflowType, connectionWorkflow.WorkflowType.WorkTerm, rockContext );
                if ( workflow != null )
                {
                    var workflowService = new Rock.Model.WorkflowService( rockContext );

                    List<string> workflowErrors;
                    if ( workflowService.Process( workflow, connectionRequest, out workflowErrors ) )
                    {
                        if ( workflow.Id != 0 )
                        {
                            ConnectionRequestWorkflow connectionRequestWorkflow = new ConnectionRequestWorkflow();
                            connectionRequestWorkflow.ConnectionRequestId = connectionRequest.Id;
                            connectionRequestWorkflow.WorkflowId = workflow.Id;
                            connectionRequestWorkflow.ConnectionWorkflowId = connectionWorkflow.Id;
                            connectionRequestWorkflow.TriggerType = connectionWorkflow.TriggerType;
                            connectionRequestWorkflow.TriggerQualifier = connectionWorkflow.QualifierValue;
                            new ConnectionRequestWorkflowService( rockContext ).Add( connectionRequestWorkflow );

                            rockContext.SaveChanges();

                            if ( workflow.HasActiveEntryForm( CurrentPerson ) )
                            {
                                var qryParam = new Dictionary<string, string>();
                                qryParam.Add( "WorkflowTypeId", connectionWorkflow.WorkflowType.Id.ToString() );
                                qryParam.Add( "WorkflowId", workflow.Id.ToString() );
                                NavigateToLinkedPage( "WorkflowEntryPage", qryParam );
                            }
                            else
                            {
                                mdWorkflowLaunched.Show( string.Format( "A '{0}' workflow has been started.",
                                    connectionWorkflow.WorkflowType.Name ), ModalAlertType.Information );
                            }

                            ShowDetail( PageParameter( "ConnectionRequestId" ).AsInteger(), PageParameter( "ConnectionOpportunityId" ).AsIntegerOrNull() );
                        }
                        else
                        {
                            mdWorkflowLaunched.Show( string.Format( "A '{0}' workflow was processed (but not persisted).",
                                connectionWorkflow.WorkflowType.Name ), ModalAlertType.Information );
                        }
                    }
                    else
                    {
                        mdWorkflowLaunched.Show( "Workflow Processing Error(s):<ul><li>" + workflowErrors.AsDelimited( "</li><li>" ) + "</li></ul>", ModalAlertType.Information );
                    }
                }
            }
        }
Пример #31
0
 /// <summary>
 /// Handles the Edit event of the gWorkflows control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
 protected void gWorkflows_Edit( object sender, RowEventArgs e )
 {
     var workflow = new WorkflowService( new RockContext() ).Get( e.RowKeyId );
     if ( workflow != null )
     {
         if ( workflow.HasActiveEntryForm( CurrentPerson ) )
         {
             var qryParam = new Dictionary<string, string>();
             qryParam.Add( "WorkflowTypeId", workflow.WorkflowTypeId.ToString() );
             qryParam.Add( "WorkflowId", workflow.Id.ToString() );
             NavigateToLinkedPage( "EntryPage", qryParam );
         }
         else
         {
             NavigateToLinkedPage( "DetailPage", "workflowId", e.RowKeyId );
         }
     }
 }
Пример #32
0
        /// <summary>
        /// Execute method to write transaction to the database.
        /// </summary>
        public void Execute()
        {
            if ( Trigger != null )
            {
                var rockContext = new RockContext();
                var workflowTypeService = new WorkflowTypeService( rockContext );
                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 ) )
                    {
                        if ( workflow.IsPersisted || workflowType.IsPersisted )
                        {
                            var workflowService = new Rock.Model.WorkflowService( rockContext );
                            workflowService.Add( workflow );

                            rockContext.WrapTransaction( () =>
                            {
                                rockContext.SaveChanges();
                                workflow.SaveAttributeValues( rockContext );
                                foreach ( var activity in workflow.Activities )
                                {
                                    activity.SaveAttributeValues( rockContext );
                                }
                            } );
                        }
                    }
                }
            }
        }
Пример #33
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)
        {
            Dictionary <string, PropertyInfo> properties = null;

            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 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, 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.Entity   = entity.Clone();
                        transaction.PersonId = personId;
                        Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);
                    }
                }
            }
            return(true);
        }
Пример #34
0
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            WorkflowService workflowService = new WorkflowService();
            SortProperty sortProperty = gWorkflows.SortProperty;

            var qry = workflowService.Queryable();

            WorkflowType workflowType = this.ContextEntity<WorkflowType>();

            if ( workflowType == null )
            {
                pnlWorkflowList.Visible = false;
                return;
            }

            // if this isn't currently a persisted workflow type, and there are no records, hide the panel
            if ( !workflowType.IsPersisted )
            {
                if ( qry.Count() == 0 )
                {
                    pnlWorkflowList.Visible = false;
                    return;
                }
            }

            if (!string.IsNullOrWhiteSpace(workflowType.WorkTerm))
            {
                gWorkflows.RowItemText = workflowType.WorkTerm;
                lGridTitle.Text = workflowType.WorkTerm.Pluralize();
            }

            AttributeService attributeService = new AttributeService();

            // add attributes with IsGridColumn to grid
            string qualifierValue = workflowType.Id.ToString();
            var qryWorkflowTypeAttributes = attributeService.GetByEntityTypeId( new Workflow().TypeId ).AsQueryable()
                .Where( a => a.EntityTypeQualifierColumn.Equals( "WorkflowTypeId", StringComparison.OrdinalIgnoreCase )
                && a.EntityTypeQualifierValue.Equals( qualifierValue ) );

            qryWorkflowTypeAttributes = qryWorkflowTypeAttributes.Where( a => a.IsGridColumn );

            List<Attribute> gridItems = qryWorkflowTypeAttributes.ToList();

            foreach ( var item in gWorkflows.Columns.OfType<AttributeField>().ToList() )
            {
                gWorkflows.Columns.Remove( item );
            }

            foreach ( var item in gridItems.OrderBy( a => a.Order ).ThenBy( a => a.Name ) )
            {
                string dataFieldExpression = item.Key;
                bool columnExists = gWorkflows.Columns.OfType<AttributeField>().FirstOrDefault( a => a.DataField.Equals( dataFieldExpression ) ) != null;
                if ( !columnExists )
                {
                    AttributeField boundField = new AttributeField();
                    boundField.DataField = dataFieldExpression;
                    boundField.HeaderText = item.Name;
                    boundField.SortExpression = string.Empty;
                    int insertPos = gWorkflows.Columns.IndexOf( gWorkflows.Columns.OfType<DeleteField>().First() );
                    gWorkflows.Columns.Insert( insertPos, boundField );
                }
            }


            pnlWorkflowList.Visible = true;

            qry = qry.Where( a => a.WorkflowTypeId.Equals( workflowType.Id ) );

            if ( sortProperty != null )
            {
                gWorkflows.DataSource = qry.Sort( sortProperty ).ToList();
            }
            else
            {
                gWorkflows.DataSource = qry.OrderBy( s => s.Name ).ToList();
            }

            gWorkflows.DataBind();
        }
        private List<WorkflowAction> GetWorkflows()
        {
            var actions = new List<WorkflowAction>();

            if ( CurrentPerson != null )
            {
                using ( var rockContext = new RockContext() )
                {
                    var categoryIds = GetCategories( rockContext );

                    var qry = new WorkflowService( rockContext ).Queryable()
                        .Where( w =>
                            w.ActivatedDateTime.HasValue &&
                            !w.CompletedDateTime.HasValue &&
                            w.InitiatorPersonAlias.PersonId == CurrentPerson.Id );

                    if ( categoryIds.Any() )
                    {
                        qry = qry
                            .Where( w =>
                                w.WorkflowType.CategoryId.HasValue &&
                                categoryIds.Contains( w.WorkflowType.CategoryId.Value ) );
                    }

                    foreach ( var workflow in qry.OrderBy( w => w.ActivatedDateTime ) )
                    {
                        var activity = new WorkflowActivity();
                        activity.Workflow = workflow;

                        var action = new WorkflowAction();
                        action.Activity = activity;

                        actions.Add( action );
                    }
                }
            }

            return actions;
        }
Пример #36
0
        protected void lbProfileNext_Click( object sender, EventArgs e )
        {
            // setup merge fields
            var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields( this.RockPage, this.CurrentPerson );
            mergeFields.Add( "PersonId", hfPersonId.Value );
            mergeFields.Add( "FirstName", tbFirstName.Text );
            mergeFields.Add( "LastName", tbLastName.Text );
            mergeFields.Add( "StreetAddress", acAddress.Street1 );
            mergeFields.Add( "City", acAddress.City );
            mergeFields.Add( "State", acAddress.State );
            mergeFields.Add( "PostalCode", acAddress.PostalCode );
            mergeFields.Add( "Country", acAddress.Country );
            mergeFields.Add( "Email", tbEmail.Text );
            mergeFields.Add( "HomePhone", pnbHomePhone.Text );
            mergeFields.Add( "MobilePhone", pnbHomePhone.Text );
            mergeFields.Add( "BirthDate", dpBirthdate.Text );
            mergeFields.Add( "OtherUpdates", tbOtherUpdates.Text );

            // if an email was provided email results
            RockContext rockContext = new RockContext();
            if ( !string.IsNullOrWhiteSpace( GetAttributeValue( "UpdateEmail" ) ) )
            {
                var receiptEmail = new SystemEmailService( rockContext ).Get( new Guid( GetAttributeValue( "UpdateEmail" ) ) );

                if ( receiptEmail != null )
                {
                    var appRoot = Rock.Web.Cache.GlobalAttributesCache.Read( rockContext ).GetValue( "PublicApplicationRoot" );

                    var recipients = new List<RecipientData>();
                    recipients.Add( new RecipientData( null, mergeFields ) );

                    Email.Send( receiptEmail.Guid, recipients, appRoot );
                }
            }

            // launch workflow if configured
            if ( !string.IsNullOrWhiteSpace( GetAttributeValue( "WorkflowType" ) ) )
            {
                var workflowTypeService = new WorkflowTypeService( rockContext );
                var workflowService = new WorkflowService( rockContext );
                var workflowType = workflowTypeService.Get( new Guid( GetAttributeValue( "WorkflowType" ) ) );

                if ( workflowType != null )
                {
                    var workflow = Rock.Model.Workflow.Activate( workflowType, "Kiosk Update Info" );

                    // set attributes
                    workflow.SetAttributeValue( "PersonId", hfPersonId.Value );
                    workflow.SetAttributeValue( "FirstName", tbFirstName.Text );
                    workflow.SetAttributeValue( "LastName", tbLastName.Text );
                    workflow.SetAttributeValue( "StreetAddress", acAddress.Street1 );
                    workflow.SetAttributeValue( "City", acAddress.City );
                    workflow.SetAttributeValue( "State", acAddress.State );
                    workflow.SetAttributeValue( "PostalCode", acAddress.PostalCode );
                    workflow.SetAttributeValue( "Country", acAddress.Country );
                    workflow.SetAttributeValue( "Email", tbEmail.Text );
                    workflow.SetAttributeValue( "HomePhone", pnbHomePhone.Text );
                    workflow.SetAttributeValue( "MobilePhone", pnbHomePhone.Text );
                    workflow.SetAttributeValue( "BirthDate", dpBirthdate.Text );
                    workflow.SetAttributeValue( "OtherUpdates", tbOtherUpdates.Text );

                    // lauch workflow
                    List<string> workflowErrors;
                    workflowService.Process( workflow, out workflowErrors );
                }
            }

            HidePanels();
            pnlComplete.Visible = true;

            lCompleteMessage.Text = GetAttributeValue( "CompleteMessageLava" ).ResolveMergeFields( mergeFields );

            bool enableDebug = GetAttributeValue( "EnableDebug" ).AsBoolean();
            if ( enableDebug && IsUserAuthorized( Authorization.EDIT ) )
            {
                lDebug.Visible = true;
                lDebug.Text = mergeFields.lavaDebugInfo();
            }
        }
Пример #37
0
        /// <summary>
        /// Launches the workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="connectionWorkflow">The connection workflow.</param>
        /// <param name="name">The name.</param>
        private void LaunchWorkflow( RockContext rockContext, Attendance attendance, PersonAlias pagePersonAlias, WorkflowType pageWorkflowType )
        {
            if ( pageWorkflowType != null )
            {
                var workflow = Rock.Model.Workflow.Activate( pageWorkflowType, pageWorkflowType.WorkTerm, rockContext );
                if ( workflow != null )
                {
                    workflow.SetAttributeValue( "Person", attendance.PersonAlias.Guid );
                    workflow.SetAttributeValue( "PagePerson", pagePersonAlias.Guid );

                    var workflowService = new Rock.Model.WorkflowService( rockContext );

                    List<string> workflowErrors;
                    if ( workflowService.Process( workflow, attendance, out workflowErrors ) )
                    {
                        if ( workflow.Id != 0 )
                        {

                            if ( workflow.HasActiveEntryForm( CurrentPerson ) )
                            {
                                var qryParam = new Dictionary<string, string>();
                                qryParam.Add( "WorkflowTypeId", pageWorkflowType.Id.ToString() );
                                qryParam.Add( "WorkflowId", workflow.Id.ToString() );
                                NavigateToLinkedPage( "WorkflowEntryPage", qryParam );
                            }
                            else
                            {
                                mdWorkflowLaunched.Show( string.Format( "A '{0}' workflow has been started.",
                                    pageWorkflowType.Name ), ModalAlertType.Information );
                            }
                        }
                        else
                        {
                            mdWorkflowLaunched.Show( string.Format( "A '{0}' workflow was processed (but not persisted).",
                                pageWorkflowType.Name ), ModalAlertType.Information );
                        }
                    }
                    else
                    {
                        mdWorkflowLaunched.Show( "Workflow Processing Error(s):<ul><li>" + workflowErrors.AsDelimited( "</li><li>" ) + "</li></ul>", ModalAlertType.Information );
                    }
                    return;
                }
            }

            Dictionary<string, string> qParams = new Dictionary<string, string>();
            qParams.Add( "AttendanceId", attendance.Id.ToString() );
            qParams.Add( "PagePersonId", pagePersonAlias.Person.Id.ToString() );
            NavigateToLinkedPage( "WorkflowEntryPage", qParams );
        }
Пример #38
0
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            if ( _workflowType != null )
            {
                pnlWorkflowList.Visible = true;

                if ( _workflowType != null )
                {
                    var rockContext = new RockContext();
                    var workflowService = new WorkflowService( rockContext );

                    var qry = workflowService
                        .Queryable( "Activities.ActivityType,InitiatorPersonAlias.Person" ).AsNoTracking()
                        .Where( w => w.WorkflowTypeId.Equals( _workflowType.Id ) );

                    // Activated Date Range Filter
                    if ( drpActivated.LowerValue.HasValue )
                    {
                        qry = qry.Where( w => w.ActivatedDateTime >= drpActivated.LowerValue.Value );
                    }
                    if ( drpActivated.UpperValue.HasValue )
                    {
                        DateTime upperDate = drpActivated.UpperValue.Value.Date.AddDays( 1 );
                        qry = qry.Where( w => w.ActivatedDateTime.Value < upperDate );
                    }

                    // State Filter
                    List<string> states = cblState.SelectedValues;
                    if ( states.Count == 1 )    // Don't filter if none or both options are selected
                    {
                        if ( states[0] == "Active" )
                        {
                            qry = qry.Where( w => !w.CompletedDateTime.HasValue );
                        }
                        else
                        {
                            qry = qry.Where( w => w.CompletedDateTime.HasValue );
                        }
                    }

                    // Completed Date Range Filter
                    if ( drpCompleted.LowerValue.HasValue )
                    {
                        qry = qry.Where( w => w.CompletedDateTime.HasValue && w.CompletedDateTime.Value >= drpCompleted.LowerValue.Value );
                    }
                    if ( drpCompleted.UpperValue.HasValue )
                    {
                        DateTime upperDate = drpCompleted.UpperValue.Value.Date.AddDays( 1 );
                        qry = qry.Where( w => w.CompletedDateTime.HasValue && w.CompletedDateTime.Value < upperDate );
                    }

                    string name = tbName.Text;
                    if ( !string.IsNullOrWhiteSpace( name ) )
                    {
                        qry = qry.Where( w => w.Name.StartsWith( name ) );
                    }

                    int? personId = ppInitiator.SelectedValue;
                    if ( personId.HasValue )
                    {
                        qry = qry.Where( w => w.InitiatorPersonAlias.PersonId == personId.Value );
                    }

                    string status = tbStatus.Text;
                    if ( !string.IsNullOrWhiteSpace( status ) )
                    {
                        qry = qry.Where( w => w.Status.StartsWith( status ) );
                    }

                    // Filter query by any configured attribute filters
                    if ( AvailableAttributes != null && AvailableAttributes.Any() )
                    {
                        var attributeValueService = new AttributeValueService( rockContext );
                        var parameterExpression = attributeValueService.ParameterExpression;

                        foreach ( var attribute in AvailableAttributes )
                        {
                            var filterControl = phAttributeFilters.FindControl( "filter_" + attribute.Id.ToString() );
                            if ( filterControl != null )
                            {
                                var filterValues = attribute.FieldType.Field.GetFilterValues( filterControl, attribute.QualifierValues, Rock.Reporting.FilterMode.SimpleFilter );
                                var expression = attribute.FieldType.Field.AttributeFilterExpression( attribute.QualifierValues, filterValues, parameterExpression );
                                if ( expression != null )
                                {
                                    var attributeValues = attributeValueService
                                        .Queryable()
                                        .Where( v => v.Attribute.Id == attribute.Id );

                                    attributeValues = attributeValues.Where( parameterExpression, expression, null );

                                    qry = qry.Where( w => attributeValues.Select( v => v.EntityId ).Contains( w.Id ) );
                                }
                            }
                        }
                    }

                    IQueryable<Workflow> workflows = null;

                    var sortProperty = gWorkflows.SortProperty;
                    if ( sortProperty != null )
                    {
                        if ( sortProperty.Property == "Initiator" )
                        {
                            if ( sortProperty.Direction == SortDirection.Ascending )
                            {
                                workflows = qry
                                    .OrderBy( w => w.InitiatorPersonAlias.Person.LastName )
                                    .ThenBy( w => w.InitiatorPersonAlias.Person.NickName );
                            }
                            else
                            {
                                workflows = qry
                                    .OrderByDescending( w => w.InitiatorPersonAlias.Person.LastName )
                                    .ThenByDescending( w => w.InitiatorPersonAlias.Person.NickName );
                            }
                        }
                        else
                        {
                            workflows = qry.Sort( sortProperty );
                        }
                    }
                    else
                    {
                        workflows = qry.OrderByDescending( s => s.CreatedDateTime );
                    }

                    // Since we're not binding to actual workflow list, but are using AttributeField columns,
                    // we need to save the workflows into the grid's object list
                    var workflowObjectQry = workflows;
                    if ( gWorkflows.AllowPaging )
                    {
                        workflowObjectQry = workflowObjectQry.Skip( gWorkflows.PageIndex * gWorkflows.PageSize ).Take( gWorkflows.PageSize );
                    }

                    gWorkflows.ObjectList = workflowObjectQry.ToList().ToDictionary( k => k.Id.ToString(), v => v as object );

                    gWorkflows.EntityTypeId = EntityTypeCache.Read<Workflow>().Id;
                    var qryGrid = workflows.Select( w => new
                    {
                        w.Id,
                        w.Name,
                        Initiator = w.InitiatorPersonAlias.Person,
                        Activities = w.Activities.Where( a => a.ActivatedDateTime.HasValue && !a.CompletedDateTime.HasValue ).OrderBy( a => a.ActivityType.Order ).Select( a => a.ActivityType.Name ),
                        w.CreatedDateTime,
                        Status = w.Status,
                        IsCompleted = w.CompletedDateTime.HasValue
                    } );

                    gWorkflows.SetLinqDataSource( qryGrid );
                    gWorkflows.DataBind();
                }
                else
                {
                    pnlWorkflowList.Visible = false;
                }
            }
        }
Пример #39
0
        protected void LaunchWorkflow(Guid workflowGuid, Dictionary<string, string> attributes)
        {
            RockContext _rockContext = new RockContext();
            WorkflowService _workflowService = new WorkflowService(_rockContext);
            WorkflowTypeService _workflowTypeService = new WorkflowTypeService(_rockContext);
            WorkflowType _workflowType = _workflowTypeService.Get(workflowGuid);

            Workflow _workflow = Rock.Model.Workflow.Activate(_workflowType, "New Test" + _workflowType.WorkTerm);

            foreach (KeyValuePair<string, string> attribute in attributes)
            {
                _workflow.SetAttributeValue(attribute.Key, attribute.Value);
            }

            List<string> errorMessages;

            if ( _workflowService.Process( _workflow, out errorMessages ) )
            {
                // If the workflow type is persisted, save the workflow
                if (_workflow.IsPersisted || _workflowType.IsPersisted)
                {
                    if (_workflow.Id == 0)
                    {
                        _workflowService.Add(_workflow);
                    }

                    _rockContext.WrapTransaction(() =>
                    {
                        _rockContext.SaveChanges();
                        _workflow.SaveAttributeValues(_rockContext);
                        foreach (var activity in _workflow.Activities)
                        {
                            activity.SaveAttributeValues(_rockContext);
                        }

                    });

                }
            }
        }
        private void LaunchWorkflow( WorkflowType workflowType, FinancialTransaction transaction )
        {
            if ( workflowType != null )
            {
                using ( var rockContext = new RockContext() )
                {
                    string workflowName = transaction.TransactionCode;
                    if ( transaction.AuthorizedPersonAliasId != null )
                    {
                        var person = new PersonAliasService( rockContext ).GetPerson( transaction.AuthorizedPersonAliasId.Value );
                        if ( person != null )
                        {
                            workflowName = person.FullName;
                        }
                    }

                    var workflowService = new WorkflowService( rockContext );
                    var workflow = Rock.Model.Workflow.Activate( workflowType, workflowName, rockContext );
                    if ( workflow != null )
                    {
                        List<string> workflowErrors;
                        workflowService.Process( workflow, transaction, out workflowErrors );
                    }
                }
            }
        }
        /// <summary>
        /// Adds the person to group.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="person">The person.</param>
        /// <param name="workflowType">Type of the workflow.</param>
        /// <param name="groupMembers">The group members.</param>
        private void AddPersonToGroup( RockContext rockContext, Person person, WorkflowType workflowType, List<GroupMember> groupMembers )
        {
            if (person != null )
            {
                if ( !_group.Members
                    .Any( m =>
                        m.PersonId == person.Id &&
                        m.GroupRoleId == _defaultGroupRole.Id))
                {
                    var groupMemberService = new GroupMemberService(rockContext);
                    var groupMember = new GroupMember();
                    groupMember.PersonId = person.Id;
                    groupMember.GroupRoleId = _defaultGroupRole.Id;
                    groupMember.GroupMemberStatus = (GroupMemberStatus)GetAttributeValue("GroupMemberStatus").AsInteger();
                    groupMember.GroupId = _group.Id;
                    groupMemberService.Add( groupMember );
                    rockContext.SaveChanges();

                    if ( workflowType != null )
                    {
                        try
                        {
                            var workflowService = new WorkflowService( rockContext );
                            var workflow = Workflow.Activate( workflowType, person.FullName );

                            List<string> workflowErrors;
                            if ( workflow.Process( rockContext, groupMember, out workflowErrors ) )
                            {
                                if ( workflow.IsPersisted || workflow.IsPersisted )
                                {
                                    if ( workflow.Id == 0 )
                                    {
                                        workflowService.Add( workflow );
                                    }

                                    rockContext.WrapTransaction( () =>
                                    {
                                        rockContext.SaveChanges();
                                        workflow.SaveAttributeValues( _rockContext );
                                        foreach ( var activity in workflow.Activities )
                                        {
                                            activity.SaveAttributeValues( rockContext );
                                        }
                                    } );
                                }
                            }

                        }
                        catch (Exception ex)
                        {
                            ExceptionLogService.LogException( ex, this.Context );
                        }
                    }
                }
            }
        }