/// <summary> /// Gets the expression. /// </summary> /// <param name="context">The context.</param> /// <param name="entityIdProperty">The entity identifier property.</param> /// <param name="selection">The selection.</param> /// <returns></returns> public override Expression GetExpression(RockContext context, MemberExpression entityIdProperty, string selection) { var groupType = GroupTypeCache.Get(selection.AsGuid()); int groupTypeId = (groupType != null) ? groupType.Id : 0; var qry = new PersonService(context).Queryable() .Select(p => RockUdfHelper.ufnGroup_GetGeofencingGroupNames(p.Id, groupTypeId)); return(SelectExpressionExtractor.Extract(qry, entityIdProperty, "p")); }
/// <summary> /// Builds the expression. /// </summary> /// <param name="serviceInstance">The service instance.</param> /// <param name="idQuery">The id query.</param> /// <param name="parameterExpression">The parameter expression.</param> /// <returns></returns> private Expression BuildExpression(IService serviceInstance, IQueryable <int> idQuery, ParameterExpression parameterExpression) { var rockContext = ( RockContext )serviceInstance.Context; var personSpouseQuery = idQuery.Select(a => RockUdfHelper.ufnCrm_GetSpousePersonIdFromPersonId(a)); int maritalStatusMarriedId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_MARITAL_STATUS_MARRIED.AsGuid()).Id; var qry = new PersonService(( RockContext )serviceInstance.Context).Queryable().Where(p => p.MaritalStatusValueId == maritalStatusMarriedId && personSpouseQuery.Contains(p.Id)); return(FilterExpressionExtractor.Extract <Rock.Model.Person>(qry, parameterExpression, "p")); }
/// <summary> /// Gets the expression. /// </summary> /// <param name="context">The context.</param> /// <param name="entityIdProperty">The entity identifier property.</param> /// <param name="selection">The selection.</param> /// <returns></returns> public override Expression GetExpression(RockContext context, MemberExpression entityIdProperty, string selection) { string[] values = selection.Split('|'); Guid groupLocationTypeValueGuid = Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_HOME.AsGuid(); AddressNamePart addressNamePart = AddressNamePart.Full; if (values.Length >= 1) { groupLocationTypeValueGuid = values[0].AsGuidOrNull() ?? Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_HOME.AsGuid(); } if (values.Length >= 2) { addressNamePart = values[1].ConvertToEnumOrNull <AddressNamePart>() ?? AddressNamePart.Full; } string addressTypeId = DefinedValueCache.Read(groupLocationTypeValueGuid).Id.ToString(); string addressComponent = addressNamePart.ConvertToString(false); var personLocationQuery = new PersonService(context).Queryable() .Select(p => RockUdfHelper.ufnCrm_GetAddress(p.Id, addressTypeId, addressComponent)); return(SelectExpressionExtractor.Extract <Rock.Model.Person>(personLocationQuery, entityIdProperty, "p")); }
/// <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("GroupRole,Person").AsNoTracking() .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, // Order People to match ordering in the GroupMembers.ascx block. Persons = // Adult Male x.Where(xx => xx.GroupMember.GroupRole.Guid.Equals(new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT)) && xx.GroupMember.Person.Gender == Gender.Male).OrderByDescending(xx => xx.GroupMember.Person.BirthDate).Select(xx => xx.Person) // Adult Female .Concat(x.Where(xx => xx.GroupMember.GroupRole.Guid.Equals(new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT)) && xx.GroupMember.Person.Gender != Gender.Male).OrderByDescending(xx => xx.GroupMember.Person.BirthDate).Select(xx => xx.Person)) // non-adults .Concat(x.Where(xx => !xx.GroupMember.GroupRole.Guid.Equals(new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT))) .OrderByDescending(xx => xx.GroupMember.Person.BirthDate).Select(xx => xx.Person)) }); 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.AddOrIgnore("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; // if the mergeValueObject is a JArray (JSON Object), convert it into an ExpandoObject or List<ExpandoObject> so that Lava will work on it if (mergeValueObject is JArray) { var jsonOfObject = mergeValueObject.ToJson(); try { mergeValueObject = Rock.Lava.RockFilters.FromJSON(jsonOfObject); } catch (Exception ex) { LogException(new Exception("MergeTemplateEntry couldn't do a FromJSON", ex)); } } 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()); }
private void BindGrid() { var rockContext = new RockContext(); var groupMemberService = new GroupMemberService(rockContext).Queryable().AsNoTracking(); List <int> groupIdsMulti = gpGroups.SelectedValuesAsInt().ToList(); var groupMembers = groupMemberService.Where(gm => groupIdsMulti.Contains(gm.GroupId)); SortProperty sortProperty = groupMemberGrid.SortProperty; groupMembers = FilterByBirthday(groupMembers); Guid groupLocationTypeValueGuid = Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_HOME.AsGuid(); RockUdfHelper.AddressNamePart addressNamePart = RockUdfHelper.AddressNamePart.Full; string addressTypeId = DefinedValueCache.Get(groupLocationTypeValueGuid).Id.ToString(); string addressComponent = addressNamePart.ConvertToString(false); if (sortProperty != null) { groupMemberGrid.DataSource = groupMembers.Select(gm => new MemberData() { FirstName = gm.Person.FirstName, LastName = gm.Person.LastName, Id = gm.Person.Id, BirthMonth = gm.Person.BirthMonth, BirthDay = gm.Person.BirthDay, BirthYear = gm.Person.BirthYear, Birthdate = gm.Person.BirthDate, AnniversaryMonth = gm.Person.AnniversaryDate.HasValue ? gm.Person.AnniversaryDate.Value.Month : (int?)null, AnniversaryDay = gm.Person.AnniversaryDate.HasValue ? gm.Person.AnniversaryDate.Value.Day : (int?)null, AnniversaryYear = gm.Person.AnniversaryDate.HasValue ? gm.Person.AnniversaryDate.Value.Year : (int?)null, Anniversary = gm.Person.AnniversaryDate, Email = gm.Person.Email, Address = RockUdfHelper.ufnCrm_GetAddress(gm.Person.Id, addressTypeId, addressComponent) }).Distinct().Sort(sortProperty).ToList(); } else { groupMemberGrid.DataSource = groupMembers.Select(gm => new MemberData() { FirstName = gm.Person.FirstName, LastName = gm.Person.LastName, Id = gm.Person.Id, BirthMonth = gm.Person.BirthMonth, BirthDay = gm.Person.BirthDay, BirthYear = gm.Person.BirthYear, Birthdate = gm.Person.BirthDate, AnniversaryMonth = gm.Person.AnniversaryDate.HasValue ? gm.Person.AnniversaryDate.Value.Month : (int?)null, AnniversaryDay = gm.Person.AnniversaryDate.HasValue ? gm.Person.AnniversaryDate.Value.Day : (int?)null, AnniversaryYear = gm.Person.AnniversaryDate.HasValue ? gm.Person.AnniversaryDate.Value.Year : (int?)null, Anniversary = gm.Person.AnniversaryDate, Email = gm.Person.Email, Address = RockUdfHelper.ufnCrm_GetAddress(gm.Person.Id, addressTypeId, addressComponent) }).Distinct().OrderBy(s => s.FirstName).ToList(); } groupMemberGrid.DataBind(); }
/// <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 (isPersonEntityType && combineFamilyMembers) { var 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 (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>(); foreach (var item in groupMember.ToDictionary()) { if (item.Key == "Id") { person.AdditionalLavaFields.AddOrIgnore("GroupMemberId", item.Value); } else if (item.Key == "Guid") { person.AdditionalLavaFields.AddOrIgnore("GroupMemberGuid", item.Value.ToStringSafe()); } else { person.AdditionalLavaFields.AddOrIgnore(item.Key, item.Value); } } 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()); }