/// <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 ) { ConnectionOpportunity connectionOpportunity = null; using ( RockContext rockContext = new RockContext() ) { int? groupTypeId = ddlGroupType.SelectedValueAsInt(); if ( groupTypeId.HasValue && GroupsState.Any( g => g.GroupTypeId != groupTypeId.Value ) ) { string groupTypeName = ddlGroupType.SelectedItem.Text; nbInvalidGroupTypes.Text = string.Format( "<p>One or more of the selected groups is not a <strong>{0}</strong> type. Please select groups that have a group type of <strong>{0}</strong>.", groupTypeName ); nbInvalidGroupTypes.Visible = true; return; } ConnectionOpportunityService connectionOpportunityService = new ConnectionOpportunityService( rockContext ); EventCalendarItemService eventCalendarItemService = new EventCalendarItemService( rockContext ); ConnectionWorkflowService connectionWorkflowService = new ConnectionWorkflowService( rockContext ); ConnectionRequestWorkflowService connectionRequestWorkflowService = new ConnectionRequestWorkflowService( rockContext ); ConnectionOpportunityConnectorGroupService connectionOpportunityConnectorGroupsService = new ConnectionOpportunityConnectorGroupService( rockContext ); ConnectionOpportunityCampusService connectionOpportunityCampusService = new ConnectionOpportunityCampusService( rockContext ); ConnectionOpportunityGroupService connectionOpportunityGroupService = new ConnectionOpportunityGroupService( rockContext ); int connectionOpportunityId = hfConnectionOpportunityId.ValueAsInt(); if ( connectionOpportunityId != 0 ) { connectionOpportunity = connectionOpportunityService .Queryable( "ConnectionOpportunityGroups, ConnectionWorkflows" ) .Where( ei => ei.Id == connectionOpportunityId ) .FirstOrDefault(); } if ( connectionOpportunity == null ) { connectionOpportunity = new ConnectionOpportunity(); connectionOpportunity.Name = string.Empty; connectionOpportunity.ConnectionTypeId = PageParameter( "ConnectionTypeId" ).AsInteger(); connectionOpportunityService.Add( connectionOpportunity ); } connectionOpportunity.Name = tbName.Text; connectionOpportunity.Summary = htmlSummary.Text; connectionOpportunity.Description = htmlDescription.Text; connectionOpportunity.IsActive = cbIsActive.Checked; connectionOpportunity.PublicName = tbPublicName.Text; connectionOpportunity.IconCssClass = tbIconCssClass.Text; connectionOpportunity.GroupTypeId = ddlGroupType.SelectedValue.AsInteger(); connectionOpportunity.GroupMemberRoleId = ddlGroupRole.SelectedValue.AsInteger(); connectionOpportunity.GroupMemberStatus = ddlGroupMemberStatus.SelectedValueAsEnum<GroupMemberStatus>(); connectionOpportunity.UseAllGroupsOfType = tglUseAllGroupsOfGroupType.Checked; int? orphanedPhotoId = null; if ( imgupPhoto.BinaryFileId != null ) { if ( connectionOpportunity.PhotoId != imgupPhoto.BinaryFileId ) { orphanedPhotoId = connectionOpportunity.PhotoId; } connectionOpportunity.PhotoId = imgupPhoto.BinaryFileId.Value; } // remove any workflows that removed in the UI var uiWorkflows = WorkflowsState.Where( w => w.ConnectionTypeId == null ).Select( l => l.Guid ); foreach ( var connectionWorkflow in connectionOpportunity.ConnectionWorkflows.Where( l => !uiWorkflows.Contains( l.Guid ) ).ToList() ) { foreach( var requestWorkflow in connectionRequestWorkflowService.Queryable() .Where( w => w.ConnectionWorkflowId == connectionWorkflow.Id ) ) { connectionRequestWorkflowService.Delete( requestWorkflow ); } connectionOpportunity.ConnectionWorkflows.Remove( connectionWorkflow ); connectionWorkflowService.Delete( connectionWorkflow ); } // Add or Update workflows from the UI foreach ( var workflowTypeStateObj in WorkflowsState.Where( w => w.ConnectionTypeId == null ) ) { ConnectionWorkflow connectionOpportunityWorkflow = connectionOpportunity.ConnectionWorkflows.Where( a => a.Guid == workflowTypeStateObj.Guid ).FirstOrDefault(); if ( connectionOpportunityWorkflow == null ) { connectionOpportunityWorkflow = new ConnectionWorkflow(); connectionOpportunity.ConnectionWorkflows.Add( connectionOpportunityWorkflow ); } connectionOpportunityWorkflow.Id = workflowTypeStateObj.Id; connectionOpportunityWorkflow.Guid = workflowTypeStateObj.Guid; connectionOpportunityWorkflow.ConnectionTypeId = workflowTypeStateObj.ConnectionTypeId; connectionOpportunityWorkflow.WorkflowTypeId = workflowTypeStateObj.WorkflowTypeId; connectionOpportunityWorkflow.TriggerType = workflowTypeStateObj.TriggerType; connectionOpportunityWorkflow.QualifierValue = workflowTypeStateObj.QualifierValue; connectionOpportunityWorkflow.ConnectionOpportunityId = connectionOpportunity.Id; } // remove any group campuses that removed in the UI var uiConnectorGroups = ConnectorGroupsState.Select( l => l.Guid ); foreach ( var connectionOpportunityConnectorGroups in connectionOpportunity.ConnectionOpportunityConnectorGroups.Where( l => !uiConnectorGroups.Contains( l.Guid ) ).ToList() ) { connectionOpportunity.ConnectionOpportunityConnectorGroups.Remove( connectionOpportunityConnectorGroups ); connectionOpportunityConnectorGroupsService.Delete( connectionOpportunityConnectorGroups ); } // Add or Update group campuses from the UI foreach ( var groupStateObj in ConnectorGroupsState ) { ConnectionOpportunityConnectorGroup connectionOpportunityConnectorGroup = connectionOpportunity.ConnectionOpportunityConnectorGroups.Where( a => a.Guid == groupStateObj.Guid ).FirstOrDefault(); if ( connectionOpportunityConnectorGroup == null ) { connectionOpportunityConnectorGroup = new ConnectionOpportunityConnectorGroup(); connectionOpportunity.ConnectionOpportunityConnectorGroups.Add( connectionOpportunityConnectorGroup ); } connectionOpportunityConnectorGroup.CampusId = groupStateObj.CampusId; connectionOpportunityConnectorGroup.ConnectorGroupId = groupStateObj.GroupId; connectionOpportunityConnectorGroup.ConnectionOpportunityId = connectionOpportunity.Id; } // remove any campuses that removed in the UI var uiCampuses = cblSelectedItemsAsInt( cblCampus ); foreach ( var connectionOpportunityCampus in connectionOpportunity.ConnectionOpportunityCampuses.Where( c => !uiCampuses.Contains( c.CampusId ) ).ToList() ) { connectionOpportunity.ConnectionOpportunityCampuses.Remove( connectionOpportunityCampus ); connectionOpportunityCampusService.Delete( connectionOpportunityCampus ); } // Add or Update campuses from the UI foreach ( var campusId in uiCampuses ) { ConnectionOpportunityCampus connectionOpportunityCampus = connectionOpportunity.ConnectionOpportunityCampuses.Where( c => c.CampusId == campusId ).FirstOrDefault(); if ( connectionOpportunityCampus == null ) { connectionOpportunityCampus = new ConnectionOpportunityCampus(); connectionOpportunity.ConnectionOpportunityCampuses.Add( connectionOpportunityCampus ); } connectionOpportunityCampus.CampusId = campusId; connectionOpportunityCampus.DefaultConnectorPersonAliasId = DefaultConnectors.ContainsKey( campusId ) ? DefaultConnectors[campusId] : (int?)null; } // Remove any groups that were removed in the UI var uiGroups = GroupsState.Select( r => r.Guid ); foreach ( var connectionOpportunityGroup in connectionOpportunity.ConnectionOpportunityGroups.Where( r => !uiGroups.Contains( r.Guid ) ).ToList() ) { connectionOpportunity.ConnectionOpportunityGroups.Remove( connectionOpportunityGroup ); connectionOpportunityGroupService.Delete( connectionOpportunityGroup ); } // Add or Update groups from the UI foreach ( var groupStateObj in GroupsState ) { ConnectionOpportunityGroup connectionOpportunityGroup = connectionOpportunity.ConnectionOpportunityGroups.Where( a => a.Guid == groupStateObj.Guid ).FirstOrDefault(); if ( connectionOpportunityGroup == null ) { connectionOpportunityGroup = new ConnectionOpportunityGroup(); connectionOpportunity.ConnectionOpportunityGroups.Add( connectionOpportunityGroup ); } connectionOpportunityGroup.GroupId = groupStateObj.GroupId; connectionOpportunityGroup.ConnectionOpportunityId = connectionOpportunity.Id; } connectionOpportunity.LoadAttributes(); Rock.Attribute.Helper.GetEditValues( phAttributes, connectionOpportunity ); if ( !Page.IsValid ) { return; } if ( !connectionOpportunity.IsValid ) { // Controls will render the error messages return; } // use WrapTransaction since SaveAttributeValues does it's own RockContext.SaveChanges() rockContext.WrapTransaction( () => { rockContext.SaveChanges(); connectionOpportunity.SaveAttributeValues( rockContext ); if ( orphanedPhotoId.HasValue ) { BinaryFileService binaryFileService = new BinaryFileService( rockContext ); var binaryFile = binaryFileService.Get( orphanedPhotoId.Value ); if ( binaryFile != null ) { string errorMessage; if ( binaryFileService.CanDelete( binaryFile, out errorMessage ) ) { binaryFileService.Delete( binaryFile ); rockContext.SaveChanges(); } } } } ); ConnectionWorkflowService.FlushCachedTriggers(); var qryParams = new Dictionary<string, string>(); qryParams["ConnectionTypeId"] = PageParameter( "ConnectionTypeId" ); NavigateToParentPage( qryParams ); } }
/// <summary> /// Handles the RowSelected event of the gConnectionRequestWorkflows control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs"/> instance containing the event data.</param> protected void gConnectionRequestWorkflows_RowSelected( object sender, Rock.Web.UI.Controls.RowEventArgs e ) { var requestWorkflow = new ConnectionRequestWorkflowService( new RockContext() ).Get( e.RowKeyValue.ToString().AsGuid() ); if ( requestWorkflow != null && requestWorkflow.Workflow != null ) { if ( requestWorkflow.Workflow.HasActiveEntryForm( CurrentPerson ) ) { var qryParam = new Dictionary<string, string>(); qryParam.Add( "WorkflowTypeId", requestWorkflow.Workflow.WorkflowTypeId.ToString() ); qryParam.Add( "WorkflowId", requestWorkflow.Workflow.Id.ToString() ); NavigateToLinkedPage( "WorkflowEntryPage", qryParam ); } else { NavigateToLinkedPage( "WorkflowDetailPage", "workflowId", requestWorkflow.Workflow.Id ); } } }
public void ConnectionOpportunity_NewSignupProducesWorkflow() { var testStartDateTime = RockDateTime.Now; var rockContext = new RockContext(); var connectionRequestService = new ConnectionRequestService(rockContext); // Serve Children's Opportunity var connectionOpportunity = new ConnectionOpportunityService(rockContext).Get("2c09211f-e19e-4685-9b67-4edaa9945a97".AsGuid()); int defaultStatusId = connectionOpportunity.ConnectionType.ConnectionStatuses .Where(s => s.IsDefault) .Select(s => s.Id) .FirstOrDefault(); var connectionRequest = new ConnectionRequest(); connectionRequest.ConnectionOpportunityId = connectionOpportunity.Id; connectionRequest.PersonAliasId = connectionOpportunity.ModifiedByPersonAliasId.Value; connectionRequest.ConnectionStatusId = defaultStatusId; ConnectionWorkflow testConnectionWorkflow = null; if (!connectionOpportunity.ConnectionWorkflows.Any()) { testConnectionWorkflow = new ConnectionWorkflow(); // Whatever workflow type id. 13 is 'External Inquiry' var workflowType = new WorkflowTypeService(rockContext).Get(13); if (!workflowType.IsPersisted) { // make sure it is Persisted so that the produced workflow gets saved to the database workflowType.IsPersisted = true; rockContext.SaveChanges(); } testConnectionWorkflow.WorkflowTypeId = 13; testConnectionWorkflow.TriggerType = ConnectionWorkflowTriggerType.RequestStarted; testConnectionWorkflow.ConnectionTypeId = connectionOpportunity.ConnectionTypeId; connectionOpportunity.ConnectionWorkflows.Add(testConnectionWorkflow); rockContext.SaveChanges(); } rockContext.WrapTransaction(() => { connectionRequestService.Add(connectionRequest); rockContext.SaveChanges(); connectionRequest.SaveAttributeValues(rockContext); // simulate the transaction taking longer. This helps test that the 'ProcessConnectionRequestChange.SendWhen' message is waiting for the database commit. Thread.Sleep(1000); }); using (var rockContext2 = new RockContext()) { var connectionRequestWorkflowService = new ConnectionRequestWorkflowService(rockContext2); bool producedConnectionRequestWorkflow = false; int retryAttempt = 0; // the connection request workflow gets produced in a seperate thread, so check for up to 10 seconds to see if it created one while (!producedConnectionRequestWorkflow && retryAttempt < 10) { var connectionRequestWorkflowQuery = connectionRequestWorkflowService.Queryable() .Where(a => a.ConnectionRequestId == connectionRequest.Id && a.CreatedDateTime > testStartDateTime); producedConnectionRequestWorkflow = connectionRequestWorkflowQuery.Any(); if (producedConnectionRequestWorkflow) { // Looks like it created it, so cleanup and break out connectionRequestWorkflowService.DeleteRange(connectionRequestWorkflowQuery.ToList()); break; } retryAttempt++; Task.Delay(1000).Wait(); } // clean up if we needed to create a testConnectionWorkflow if (testConnectionWorkflow != null) { new ConnectionWorkflowService(rockContext).Delete(testConnectionWorkflow); rockContext.SaveChanges(); } // test expected condition Assert.IsTrue(producedConnectionRequestWorkflow); } }