/// <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;
        }
Exemple #2
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);
                        }
                    }
                }
            }
        }
Exemple #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);
                                }
                            });
                        }
                    }
                }
            }
        }
        /// <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 );
                                }
                            } );
                        }
                    }
                }
            }     
        }
Exemple #5
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);
                }
            }
        }
Exemple #6
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);
                        }
                    }
                }
            }
        }
Exemple #8
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 )
            {
                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 );
                        }
                    }
                }
            }
        }
        /// <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);
                                    }
                                });
                            }
                        }
                    }
                }
            }
        }
        /// <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();
                    }
                }
            }
        }
        //  overrides of the base RockBlock methods (i.e. OnInit, OnLoad)

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnInit( EventArgs e )
        {
            base.OnInit( e );

            int? workflowTypeId = PageParameter( "WorkflowTypeId" ).AsIntegerOrNull();
            if ( workflowTypeId.HasValue )
            {
                var rockContext = new RockContext();
                var workflowType = new WorkflowTypeService( rockContext ).Get( workflowTypeId.Value );
                if ( workflowType != null )
                {
                    var workflow = Workflow.Activate( workflowType, PageParameter( "WorkflowName" ) );
                    if ( workflow != null )
                    {

                        object entity = null;

                        int? personId = PageParameter( "PersonId" ).AsIntegerOrNull();
                        if ( personId.HasValue )
                        {
                            entity = new PersonService( rockContext ).Get( personId.Value );
                        }
                        else
                        {
                            int? groupId = PageParameter( "GroupId" ).AsIntegerOrNull();
                            if ( groupId.HasValue )
                            {
                                entity = new GroupService( rockContext ).Get( groupId.Value );
                            }
                        }

                        var qryParams = new Dictionary<string, string>();
                        qryParams.Add( "WorkflowTypeId", workflowTypeId.ToString() );

                        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 );

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

                                qryParams.Add( "WorkflowId", workflow.Id.ToString() );
                            }

                            NavigateToLinkedPage( "WorkflowEntryPage", qryParams );

                        }
                        else
                        {
                            nbError.Title = "Workflow Processing Error(s):";
                            nbError.Text = workflowErrors.AsDelimited( "<br/>" );
                        }
                    }
                    else
                    {
                        nbError.Text = "Could not activate workflow.";
                    }
                }
                else
                {
                    nbError.Text = "Invalid Workflow Type Id.";
                }
            }
            else
            {
                nbError.Text = "A workflow could not be activated due to missing Workflow Type Id query string parameter.";
            }
        }
Exemple #13
0
        private bool TriggerWorkflows( IEntity entity, WorkflowTriggerType triggerType, PersonAlias personAlias )
        {
            Dictionary<string, PropertyInfo> properties = null;

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

            foreach ( var trigger in TriggerCache.Triggers( entity.TypeName, triggerType ).Where( t => t.IsActive == true ) )
            {
                bool match = true;

                if ( !string.IsNullOrWhiteSpace( trigger.EntityTypeQualifierColumn ) )
                {
                    if ( properties == null )
                    {
                        properties = new Dictionary<string, PropertyInfo>();
                        foreach ( PropertyInfo propertyInfo in entity.GetType().GetProperties() )
                        {
                            properties.Add( propertyInfo.Name.ToLower(), propertyInfo );
                        }
                    }

                    match = ( properties.ContainsKey( trigger.EntityTypeQualifierColumn.ToLower() ) &&
                        properties[trigger.EntityTypeQualifierColumn.ToLower()].GetValue( entity, null ).ToString()
                            == trigger.EntityTypeQualifierValue );
                }

                if ( match )
                {
                    if ( triggerType == WorkflowTriggerType.PreSave || triggerType == WorkflowTriggerType.PreDelete )
                    {
                        var workflowType = workflowTypeService.Get( trigger.WorkflowTypeId );

                        if ( workflowType != null )
                        {
                            var workflow = Rock.Model.Workflow.Activate( workflowType, trigger.WorkflowName );

                            List<string> workflowErrors;
                            if ( !workflow.Process( rockContext, entity, out workflowErrors ) )
                            {
                                SaveErrorMessages.AddRange( workflowErrors );
                                return false;
                            }
                            else
                            {
                                if ( workflow.IsPersisted || workflowType.IsPersisted )
                                {
                                    workflowService.Add( workflow );
                                    rockContext.SaveChanges();
                                }
                            }
                        }
                    }
                    else
                    {
                        var transaction = new Rock.Transactions.WorkflowTriggerTransaction();
                        transaction.Trigger = trigger;
                        transaction.Entity = entity.Clone();
                        transaction.PersonAlias = personAlias;
                        Rock.Transactions.RockQueue.TransactionQueue.Enqueue( transaction );
                    }
                }
            }
            return true;
        }
        /// <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 );
                }
            }
        }
Exemple #15
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);
        }
        /// <summary>
        /// Handles the Click event of the lbSave 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 lbSave_Click( object sender, EventArgs e )
        {
            if ( _group != null && _occurrence != null )
            {
                var rockContext = new RockContext();
                var attendanceService = new AttendanceService( rockContext );
                var personAliasService = new PersonAliasService( rockContext );

                var existingAttendees = attendanceService.Queryable()
                        .Where( a =>
                            a.GroupId == _group.Id &&
                            a.ScheduleId == _group.ScheduleId &&
                            a.StartDateTime >= _occurrence.StartDateTime &&
                            a.StartDateTime < _occurrence.EndDateTime )
                        .ToList();

                // If did not meet was selected and this was a manually entered occurrence (not based on a schedule)
                // then just delete all the attendance records instead of tracking a 'did not meet' value
                if ( cbDidNotMeet.Checked && !_occurrence.ScheduleId.HasValue )
                {
                    foreach ( var attendance in existingAttendees )
                    {
                        attendanceService.Delete( attendance );
                    }
                }
                else
                {
                    if ( cbDidNotMeet.Checked )
                    {
                        // If the occurrence is based on a schedule, set the did not meet flags
                        foreach ( var attendance in existingAttendees )
                        {
                            attendance.DidAttend = null;
                            attendance.DidNotOccur = true;
                        }
                    }

                    foreach ( var item in lvMembers.Items )
                    {
                        var hfMember = item.FindControl( "hfMember" ) as HiddenField;
                        var cbMember = item.FindControl( "cbMember" ) as CheckBox;

                        if ( hfMember != null && cbMember != null )
                        {
                            int personId = hfMember.ValueAsInt();

                            var attendance = existingAttendees
                                .Where( a => a.PersonAlias.PersonId == personId )
                                .FirstOrDefault();

                            if ( attendance == null )
                            {
                                int? personAliasId = personAliasService.GetPrimaryAliasId( personId );
                                if ( personAliasId.HasValue )
                                {
                                    attendance = new Attendance();
                                    attendance.GroupId = _group.Id;
                                    attendance.ScheduleId = _group.ScheduleId;
                                    attendance.PersonAliasId = personAliasId;
                                    attendance.StartDateTime = _occurrence.StartDateTime;
                                    attendanceService.Add( attendance );
                                }
                            }

                            if ( attendance != null )
                            {
                                if ( cbDidNotMeet.Checked )
                                {
                                    attendance.DidAttend = null;
                                    attendance.DidNotOccur = true;
                                }
                                else
                                {
                                    attendance.DidAttend = cbMember.Checked;
                                    attendance.DidNotOccur = null;
                                }
                            }
                        }
                    }
                }

                rockContext.SaveChanges();

                WorkflowType workflowType = null;
                Guid? workflowTypeGuid = GetAttributeValue( "Workflow" ).AsGuidOrNull();
                if ( workflowTypeGuid.HasValue )
                {
                    var workflowTypeService = new WorkflowTypeService( rockContext );
                    workflowType = workflowTypeService.Get( workflowTypeGuid.Value );
                    if ( workflowType != null )
                    {
                        try
                        {
                            var workflowService = new WorkflowService( rockContext );
                            var workflow = Workflow.Activate( workflowType, _group.Name );

                            workflow.SetAttributeValue( "StartDateTime", _occurrence.StartDateTime.ToString( "o" ) );
                            workflow.SetAttributeValue( "Schedule", _group.Schedule.Guid.ToString() );

                            List<string> workflowErrors;
                            if ( workflow.Process( rockContext, _group, 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 );
                        }
                    }
                }

                NavigateToParentPage( new Dictionary<string, string> { { "GroupId", _group.Id.ToString() } } );
            }
        }
        /// <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 );
                        }
                    }
                }
            }
        }
Exemple #18
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);
                        }

                    });

                }
            }
        }
        /// <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 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 );
            }
        }
        private void PersistWorkflow(Rock.Model.Workflow workflow )
        {
            if (workflow.Id == 0)
            {
                var workflowService = new WorkflowService( _rockContext );
                workflowService.Add( workflow );
            }

            _rockContext.WrapTransaction( () =>
            {
                _rockContext.SaveChanges();
                workflow.SaveAttributeValues( _rockContext );
                foreach (var activity in workflow.Activities)
                {
                    activity.SaveAttributeValues( _rockContext );
                }
            } );
        }
        /// <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 );
                                }
                            } );
                        }
                    }
                }
            }
        }
        private void SaveForProcessingLater(Rock.Model.Workflow newWorkflow, RockContext rockContext)
        {
            newWorkflow.IsPersisted = true;
            var service = new WorkflowService( rockContext );
            if (newWorkflow.Id == 0)
            {
                service.Add( newWorkflow );
            }

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