/// <summary> /// Gets the entity query, ordered by the EntitySetItem.Order /// For example: If the EntitySet.EntityType is Person, this will return a Person Query of the items in this set /// </summary> /// <param name="entitySetId">The entity set identifier.</param> /// <returns></returns> public IQueryable <IEntity> GetEntityQuery(int entitySetId) { var entitySet = this.Get(entitySetId); if (!entitySet.EntityTypeId.HasValue) { // the EntitySet Items are not IEntity items return(null); } EntityTypeCache itemEntityType = EntityTypeCache.Read(entitySet.EntityTypeId.Value); var rockContext = this.Context as RockContext; var entitySetItemsService = new EntitySetItemService(rockContext); var entityItemQry = entitySetItemsService.Queryable().Where(a => a.EntitySetId == entitySetId).OrderBy(a => a.Order); bool isPersonEntitySet = itemEntityType.Guid == Rock.SystemGuid.EntityType.PERSON.AsGuid(); if (itemEntityType.AssemblyName != null) { Type entityType = itemEntityType.GetEntityType(); if (entityType != null) { Type[] modelType = { entityType }; Type genericServiceType = typeof(Rock.Data.Service <>); Type modelServiceType = genericServiceType.MakeGenericType(modelType); Rock.Data.IService serviceInstance = Activator.CreateInstance(modelServiceType, new object[] { rockContext }) as IService; MethodInfo qryMethod = serviceInstance.GetType().GetMethod("Queryable", new Type[] { }); var entityQry = qryMethod.Invoke(serviceInstance, new object[] { }) as IQueryable <IEntity>; var joinQry = entityItemQry.Join(entityQry, k => k.EntityId, i => i.Id, (setItem, item) => new { Item = item, ItemOrder = setItem.Order } ).OrderBy(a => a.ItemOrder).ThenBy(a => a.Item.Id); return(joinQry.Select(a => a.Item)); } } return(null); }
/// <summary> /// Gets the entity query, ordered by the EntitySetItem.Order /// For example: If the EntitySet.EntityType is Person, this will return a Person Query of the items in this set /// </summary> /// <param name="entitySetId">The entity set identifier.</param> /// <returns></returns> public IQueryable<IEntity> GetEntityQuery( int entitySetId ) { var entitySet = this.Get( entitySetId ); if (!entitySet.EntityTypeId.HasValue) { // the EntitySet Items are not IEntity items return null; } EntityTypeCache itemEntityType = EntityTypeCache.Read( entitySet.EntityTypeId.Value ); var rockContext = this.Context as RockContext; var entitySetItemsService = new EntitySetItemService( rockContext ); var entityItemQry = entitySetItemsService.Queryable().Where( a => a.EntitySetId == entitySetId ).OrderBy( a => a.Order ); bool isPersonEntitySet = itemEntityType.Guid == Rock.SystemGuid.EntityType.PERSON.AsGuid(); if ( itemEntityType.AssemblyName != null ) { Type entityType = itemEntityType.GetEntityType(); if ( entityType != null ) { Type[] modelType = { entityType }; Type genericServiceType = typeof( Rock.Data.Service<> ); Type modelServiceType = genericServiceType.MakeGenericType( modelType ); Rock.Data.IService serviceInstance = Activator.CreateInstance( modelServiceType, new object[] { rockContext } ) as IService; MethodInfo qryMethod = serviceInstance.GetType().GetMethod( "Queryable", new Type[] { } ); var entityQry = qryMethod.Invoke( serviceInstance, new object[] { } ) as IQueryable<IEntity>; var joinQry = entityItemQry.Join( entityQry, k => k.EntityId, i => i.Id, ( setItem, item ) => new { Item = item, ItemOrder = setItem.Order } ).OrderBy( a => a.ItemOrder ).ThenBy( a => a.Item.Id ); return joinQry.Select( a => a.Item ); } } return null; }
/// <summary> /// Gets the entity items with the Entity when the Type is known at design time /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public IQueryable <EntityQueryResult <T> > GetEntityItems <T>() where T : Rock.Data.Entity <T>, new() { var rockContext = this.Context as RockContext; var entitySetItemsService = new EntitySetItemService(rockContext); var entityItemQry = entitySetItemsService.Queryable(); var entityQry = new Service <T>(this.Context as RockContext).Queryable(); var joinQry = entityItemQry.Join(entityQry, k => k.EntityId, i => i.Id, (setItem, item) => new { Item = item, EntitySetId = setItem.EntitySetId, ItemOrder = setItem.Order }).OrderBy(a => a.ItemOrder).ThenBy(a => a.Item.Id); return(joinQry.Select(a => new EntityQueryResult <T> { EntitySetId = a.EntitySetId, Item = a.Item })); }
/// <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 ); // this event gets fired after block settings are updated. it's nice to repaint the screen if these settings would alter it this.BlockUpdated += Block_BlockUpdated; this.AddConfigurationUpdateTrigger( upnlContent ); var entitySetId = PageParameter( "WaitListSetId" ).AsIntegerOrNull(); if ( entitySetId.HasValue ) { // get the registrant Ids var registrantIds = new EntitySetItemService( _rockContext ) .Queryable().AsNoTracking() .Where( i => i.EntitySetId == entitySetId ) .Select( i => i.EntityId ) .ToList(); // get the registrants _registrants = new RegistrationRegistrantService( _rockContext ) .Queryable() .Where( r => registrantIds.Contains( r.Id ) ) .ToList(); // get the first registration _firstRegistration = _registrants .Where( r => r.Registration != null ) .Select( r => r.Registration ) .FirstOrDefault(); // get the template _template = _registrants .Where( r => r.Registration != null && r.Registration.RegistrationInstance != null && r.Registration.RegistrationInstance.RegistrationTemplate != null ) .Select( r => r.Registration.RegistrationInstance.RegistrationTemplate ) .FirstOrDefault(); // Bind the grid rptRecipients.DataSource = _registrants .GroupBy( r => r.Registration ) .Select( r => new RegistrationSummary { Registration = r.Key, Registrants = r.Key.Registrants.Where( g => registrantIds.Contains( g.Id ) ).ToList() } ); rptRecipients.DataBind(); } }
/// <summary> /// Raises the <see cref="E:System.Web.UI.Control.Load" /> event. /// </summary> /// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param> protected override void OnLoad( EventArgs e ) { base.OnLoad( e ); var rockContext = new RockContext(); if ( !Page.IsPostBack ) { cpCampus.Campuses = CampusCache.All(); Individuals = new List<Individual>(); SelectedFields = new List<string>(); int? setId = PageParameter( "Set" ).AsIntegerOrNull(); if ( setId.HasValue ) { var selectedPersonIds = new EntitySetItemService( rockContext ) .GetByEntitySetId( setId.Value ) .Select( i => i.EntityId ) .Distinct() .ToList(); // Get the people selected foreach ( var person in new PersonService( rockContext ).Queryable( true ) .Where( p => selectedPersonIds.Contains( p.Id ) ) .Select( p => new { p.Id, FullName = p.NickName + " " + p.LastName } ) ) { Individuals.Add( new Individual( person.Id, person.FullName ) ); } } SetControlSelection(); BuildAttributes( rockContext, true ); } else { SetControlSelection(); BuildAttributes( rockContext ); if ( ddlGroupAction.SelectedValue == "Update" ) { SetControlSelection( ddlGroupRole, "Role" ); SetControlSelection( ddlGroupMemberStatus, "Member Status" ); } BuildGroupAttributes( rockContext ); } }
/// <summary> /// Gets the merge object list for the current EntitySet /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="fetchCount">The fetch count.</param> /// <returns></returns> private List<object> GetMergeObjectList( RockContext rockContext, int? fetchCount = null ) { int entitySetId = hfEntitySetId.Value.AsInteger(); var entitySetService = new EntitySetService( rockContext ); var entitySet = entitySetService.Get( entitySetId ); Dictionary<int, object> mergeObjectsDictionary = new Dictionary<int, object>(); // If this EntitySet contains IEntity Items, add those first if ( entitySet.EntityTypeId.HasValue ) { var qryEntity = entitySetService.GetEntityQuery( entitySetId ); if ( fetchCount.HasValue ) { qryEntity = qryEntity.Take( fetchCount.Value ); } var entityTypeCache = EntityTypeCache.Read( entitySet.EntityTypeId.Value ); bool isPersonEntityType = entityTypeCache != null && entityTypeCache.Guid == Rock.SystemGuid.EntityType.PERSON.AsGuid(); bool isGroupMemberEntityType = entityTypeCache != null && entityTypeCache.Guid == Rock.SystemGuid.EntityType.GROUP_MEMBER.AsGuid(); bool combineFamilyMembers = cbCombineFamilyMembers.Visible && cbCombineFamilyMembers.Checked; if ( ( isGroupMemberEntityType || isPersonEntityType ) && combineFamilyMembers ) { IQueryable<IEntity> qryPersons; if ( isGroupMemberEntityType ) { qryPersons = qryEntity.OfType<GroupMember>().Select( a => a.Person ).Distinct(); } else { qryPersons = qryEntity; } Guid familyGroupType = Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY.AsGuid(); var qryFamilyGroupMembers = new GroupMemberService( rockContext ).Queryable() .Where( a => a.Group.GroupType.Guid == familyGroupType ) .Where( a => qryPersons.Any( aa => aa.Id == a.PersonId ) ); var qryCombined = qryFamilyGroupMembers.Join( qryPersons, m => m.PersonId, p => p.Id, ( m, p ) => new { GroupMember = m, Person = p } ) .GroupBy( a => a.GroupMember.GroupId ) .Select( x => new { GroupId = x.Key, Persons = x.Select( xx => xx.Person ).Distinct() } ); foreach ( var combinedFamilyItem in qryCombined ) { object mergeObject; string commaPersonIds = combinedFamilyItem.Persons.Select( a => a.Id ).Distinct().ToList().AsDelimited( "," ); var primaryGroupPerson = combinedFamilyItem.Persons.FirstOrDefault() as Person; if ( mergeObjectsDictionary.ContainsKey( primaryGroupPerson.Id ) ) { foreach ( var person in combinedFamilyItem.Persons ) { if ( !mergeObjectsDictionary.ContainsKey( person.Id ) ) { primaryGroupPerson = person as Person; break; } } } // if we are combining from a GroupMemberEntityType list add the GroupMember attributes of the primary person in the combined list if ( isGroupMemberEntityType ) { var groupMember = qryEntity.OfType<GroupMember>().Where( a => a.PersonId == primaryGroupPerson.Id ).FirstOrDefault(); primaryGroupPerson.AdditionalLavaFields = primaryGroupPerson.AdditionalLavaFields ?? new Dictionary<string, object>(); if ( groupMember != null ) { primaryGroupPerson.AdditionalLavaFields.Add( "GroupMember", groupMember ); } } if ( combinedFamilyItem.Persons.Count() > 1 ) { var combinedPerson = primaryGroupPerson.ToJson().FromJsonOrNull<MergeTemplateCombinedPerson>(); var familyTitle = RockUdfHelper.ufnCrm_GetFamilyTitle( rockContext, null, combinedFamilyItem.GroupId, commaPersonIds, true ); combinedPerson.FullName = familyTitle; var firstNameList = combinedFamilyItem.Persons.Select( a => ( a as Person ).FirstName ).ToList(); var nickNameList = combinedFamilyItem.Persons.Select( a => ( a as Person ).NickName ).ToList(); combinedPerson.FirstName = firstNameList.AsDelimited( ", ", " & " ); combinedPerson.NickName = nickNameList.AsDelimited( ", ", " & " ); combinedPerson.LastName = primaryGroupPerson.LastName; combinedPerson.SuffixValueId = null; combinedPerson.SuffixValue = null; mergeObject = combinedPerson; } else { mergeObject = primaryGroupPerson; } mergeObjectsDictionary.AddOrIgnore( primaryGroupPerson.Id, mergeObject ); } } else if ( isGroupMemberEntityType ) { foreach ( var groupMember in qryEntity.AsNoTracking().OfType<GroupMember>() ) { var person = groupMember.Person; person.AdditionalLavaFields = new Dictionary<string, object>(); person.AdditionalLavaFields.Add( "GroupMember", groupMember ); mergeObjectsDictionary.AddOrIgnore( groupMember.PersonId, person ); } } else { foreach ( var item in qryEntity.AsNoTracking() ) { mergeObjectsDictionary.AddOrIgnore( item.Id, item ); } } } var entitySetItemService = new EntitySetItemService( rockContext ); string[] emptyJson = new string[] { string.Empty, "{}" }; var entitySetItemMergeValuesQry = entitySetItemService.GetByEntitySetId( entitySetId, true ).Where( a => !emptyJson.Contains( a.AdditionalMergeValuesJson ) ); if ( fetchCount.HasValue ) { entitySetItemMergeValuesQry = entitySetItemMergeValuesQry.Take( fetchCount.Value ); } // the entityId to use for NonEntity objects int nonEntityId = 1; // now, add the additional MergeValues regardless of if the EntitySet contains IEntity items or just Non-IEntity items foreach ( var additionalMergeValuesItem in entitySetItemMergeValuesQry.AsNoTracking() ) { object mergeObject; int entityId; if ( additionalMergeValuesItem.EntityId > 0 ) { entityId = additionalMergeValuesItem.EntityId; } else { // not pointing to an actual EntityId, so use the nonEntityId for ti entityId = nonEntityId++; } if ( mergeObjectsDictionary.ContainsKey( entityId ) ) { mergeObject = mergeObjectsDictionary[entityId]; } else { if ( entitySet.EntityTypeId.HasValue ) { // if already have real entities in our list, don't add additional items to the mergeObjectsDictionary continue; } // non-Entity merge object, so just use Dictionary mergeObject = new Dictionary<string, object>(); mergeObjectsDictionary.AddOrIgnore( entityId, mergeObject ); } foreach ( var additionalMergeValue in additionalMergeValuesItem.AdditionalMergeValues ) { if ( mergeObject is IEntity ) { // add the additional fields to AdditionalLavaFields IEntity mergeEntity = ( mergeObject as IEntity ); mergeEntity.AdditionalLavaFields = mergeEntity.AdditionalLavaFields ?? new Dictionary<string, object>(); object mergeValueObject = additionalMergeValue.Value; mergeEntity.AdditionalLavaFields.AddOrIgnore( additionalMergeValue.Key, mergeValueObject ); } else if ( mergeObject is IDictionary<string, object> ) { // anonymous object with no fields yet IDictionary<string, object> nonEntityObject = mergeObject as IDictionary<string, object>; nonEntityObject.AddOrIgnore( additionalMergeValue.Key, additionalMergeValue.Value ); } else { throw new Exception( string.Format( "Unexpected MergeObject Type: {0}", mergeObject ) ); } } } var result = mergeObjectsDictionary.Select( a => a.Value ); if ( fetchCount.HasValue ) { // make sure the result is limited to fetchCount (even though the above queries are also limited to fetch count) result = result.Take( fetchCount.Value ); } return result.ToList(); }
/// <summary> /// Shows the merge for entity set identifier. /// </summary> /// <param name="entitySetId">The entity set identifier.</param> protected void ShowMergeForEntitySetId( int entitySetId ) { hfEntitySetId.Value = entitySetId.ToString(); var rockContext = new RockContext(); var entitySetService = new EntitySetService( rockContext ); var entitySetItemsService = new EntitySetItemService( rockContext ); var entitySet = entitySetService.Get( entitySetId ); if ( entitySet == null ) { nbWarningMessage.Text = "Merge Records not found"; nbWarningMessage.Title = "Warning"; nbWarningMessage.NotificationBoxType = NotificationBoxType.Warning; pnlEntry.Visible = false; return; } if ( entitySet.EntityTypeId.HasValue ) { bool isPersonEntitySet = entitySet.EntityTypeId.Value == EntityTypeCache.GetId<Rock.Model.Person>(); bool isGroupMemberEntitySet = entitySet.EntityTypeId.Value == EntityTypeCache.GetId<Rock.Model.GroupMember>(); cbCombineFamilyMembers.Visible = isPersonEntitySet || isGroupMemberEntitySet; } else { cbCombineFamilyMembers.Visible = false; } int itemsCount = entitySetItemsService.Queryable().Where( a => a.EntitySetId == entitySetId ).Count(); nbNumberOfRecords.Text = string.Format( "There are {0} {1} to merge", itemsCount, "row".PluralizeIf( itemsCount != 1 ) ); }
/// <summary> /// Handles the Click event of the btnShowDataPreview 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 btnShowDataPreview_Click( object sender, EventArgs e ) { if ( pnlPreview.Visible ) { pnlPreview.Visible = false; return; } var rockContext = new RockContext(); int entitySetId = hfEntitySetId.Value.AsInteger(); var entitySetService = new EntitySetService( rockContext ); var entitySet = entitySetService.Get( entitySetId ); if ( entitySet.EntityTypeId.HasValue ) { var qry = entitySetService.GetEntityQuery( entitySetId ).Take( 15 ); EntityTypeCache itemEntityType = EntityTypeCache.Read( entitySet.EntityTypeId ?? 0 ); gPreview.CreatePreviewColumns( itemEntityType.GetEntityType() ); gPreview.DataSource = qry.ToList(); gPreview.DataBind(); } else { var entitySetItemService = new EntitySetItemService( rockContext ); var qry = entitySetItemService.GetByEntitySetId( entitySetId, true ).Take( 15 ); var list = qry.ToList().Select( a => a.AdditionalMergeValuesJson.FromJsonOrNull<Dictionary<string, object>>() ).ToList(); if ( list.Any() ) { gPreview.Columns.Clear(); foreach ( var s in list[0] ) { var gridField = Grid.GetGridField( s.Value != null ? s.Value.GetType() : typeof( string ) ); gridField.HeaderText = s.Key.SplitCase(); gridField.DataField = s.Key; gPreview.Columns.Add( gridField ); } gPreview.DataSource = qry.ToList().Select( a => a.AdditionalMergeValuesJson.FromJsonOrNull<object>() ).ToList(); gPreview.DataBind(); } } pnlPreview.Visible = true; }
/// <summary> /// Loads the edit details. /// </summary> private void LoadEditDetails() { if ( !Page.IsPostBack ) { int? setId = PageParameter( "Set" ).AsIntegerOrNull(); if ( setId.HasValue ) { var selectedPersonIds = new EntitySetItemService( new RockContext() ) .GetByEntitySetId( setId.Value ) .Select( i => i.EntityId ) .Distinct() .ToList(); if ( selectedPersonIds.Count == 0 ) { ScriptManager.RegisterStartupScript( this, this.GetType(), "goBack", "history.go(-1);", true ); } // Get the people selected var people = new PersonService( new RockContext() ).Queryable( "CreatedByPersonAlias.Person,Users", true ) .Where( p => selectedPersonIds.Contains( p.Id ) ) .ToList(); // Create the data structure used to build grid MergeData = new MergeData( people, headingKeys, CurrentPerson ); MergeData.EntitySetId = setId.Value; BuildColumns(); BindGrid(); } } else { var primaryColIndex = hfSelectedColumn.Value.AsIntegerOrNull(); // Save the primary header radio button's selection foreach ( var col in gValues.Columns.OfType<PersonMergeField>() ) { col.OnDelete += personCol_OnDelete; if ( primaryColIndex.HasValue && primaryColIndex.Value == col.ColumnIndex ) { MergeData.PrimaryPersonId = col.PersonId; } } } }
/// <summary> /// Raises the <see cref="E:System.Web.UI.Control.Load" /> event. /// </summary> /// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param> protected override void OnLoad( EventArgs e ) { base.OnLoad( e ); nbPeople.Visible = false; if ( !Page.IsPostBack ) { int? setId = PageParameter( "Set" ).AsIntegerOrNull(); if (setId.HasValue) { var selectedPersonIds = new EntitySetItemService( new RockContext() ) .GetByEntitySetId( setId.Value ) .Select( i => i.EntityId ) .Distinct() .ToList(); // Get the people selected var people = new PersonService( new RockContext() ).Queryable( "CreatedByPersonAlias.Person,Users", true ) .Where( p => selectedPersonIds.Contains( p.Id ) ) .ToList(); // Create the data structure used to build grid MergeData = new MergeData( people, headingKeys ); BuildColumns(); BindGrid(); } } else { var primaryColIndex = hfSelectedColumn.Value.AsIntegerOrNull(); // Save the primary header radio button's selection foreach ( var col in gValues.Columns.OfType<PersonMergeField>() ) { col.OnDelete += personCol_OnDelete; if (primaryColIndex.HasValue && primaryColIndex.Value == col.ColumnIndex) { MergeData.PrimaryPersonId = col.PersonId; } } } }