Ejemplo n.º 1
0
        /// <summary>
        /// Adds any missing person alternate ids; limited to 150k records per run
        /// to avoid any possible memory issues. Processes about 150k records
        /// in 52 seconds.
        /// </summary>
        private static void AddMissingAlternateIds()
        {
            using (var personRockContext = new Rock.Data.RockContext())
            {
                var personService          = new PersonService(personRockContext);
                int alternateValueId       = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_SEARCH_KEYS_ALTERNATE_ID.AsGuid()).Id;
                var personSearchKeyService = new PersonSearchKeyService(personRockContext);
                var alternateKeyQuery      = personSearchKeyService.Queryable().AsNoTracking().Where(a => a.SearchTypeValueId == alternateValueId);

                IQueryable <Person> personQuery = personService.Queryable(includeDeceased: true).AsNoTracking();

                // Make a list of items that we're going to bulk insert.
                var itemsToInsert = new List <PersonSearchKey>();

                // Get all existing keys so we can keep track and quickly check them while we're bulk adding new ones.
                var keys = new HashSet <string>(personSearchKeyService.Queryable().AsNoTracking()
                                                .Where(a => a.SearchTypeValueId == alternateValueId)
                                                .Select(a => a.SearchValue)
                                                .ToList());

                string alternateId = string.Empty;

                // Find everyone who does not yet have an alternateKey.
                foreach (var person in personQuery = personQuery
                                                     .Where(p => !alternateKeyQuery.Any(f => f.PersonAlias.PersonId == p.Id))
                                                     .Take(150000))
                {
                    // Regenerate key if it already exists.
                    do
                    {
                        alternateId = PersonSearchKeyService.GenerateRandomAlternateId();
                    } while (keys.Contains(alternateId));

                    keys.Add(alternateId);

                    itemsToInsert.Add(
                        new PersonSearchKey()
                    {
                        PersonAliasId     = person.PrimaryAliasId,
                        SearchTypeValueId = alternateValueId,
                        SearchValue       = alternateId
                    }
                        );
                }

                if (itemsToInsert.Count > 0)
                {
                    // Now add them in one bulk insert.
                    personRockContext.BulkInsert(itemsToInsert);
                }
            }
        }
        public HttpResponseMessage GetEntityGuid(string code)
        {
            RockContext            rockContext            = new RockContext();
            PersonSearchKeyService personSearchKeyService = new PersonSearchKeyService(rockContext);
            var key = personSearchKeyService.Queryable().Where(k => k.SearchValue == code);

            if (!key.Any())
            {
                throw new Exception("Invalid Key");
            }

            var qr = GenerateQR(code);

            if (qr == null)
            {
                throw new Exception("Code Invalid");
            }
            MemoryStream stream = new MemoryStream();

            qr.Save(stream, ImageFormat.Png);
            var buffer   = stream.ToArray();
            var response = Request.CreateResponse(HttpStatusCode.OK);

            response.Content = new StreamContent(new MemoryStream(buffer));

            response.Content.Headers.ContentType   = new MediaTypeHeaderValue("image/png");
            response.Content.Headers.ContentLength = stream.Length;

            return(response);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// If the JWT is valid, the person claimed by that token will be returned. This method uses the configured validation parameters from the
        /// JSON Web Token Configuration Defined Type.
        /// </summary>
        /// <param name="jwtString"></param>
        /// <returns></returns>
        public async static Task <Person> GetPerson(string jwtString)
        {
            if (jwtString.IsNullOrWhiteSpace())
            {
                return(null);
            }

            // Get the configs from the JSON Web Token Configuration defined values
            var configs = GetJwtConfigs();

            if (configs == null)
            {
                return(null);
            }

            // The configs are required to specify a person search key type. The subject of the JWT should match a search key value so that we know
            // which Person the sender of the token claims to be.
            var rockContext            = new RockContext();
            var personSearchKeyService = new PersonSearchKeyService(rockContext);
            var query = personSearchKeyService.Queryable().AsNoTracking();

            // Try each config in order, which are pre-ordered. The SearchTypeValueId is required (if null we couldn't match a person even if the
            // token validates)
            foreach (var config in configs.Where(c => c.SearchTypeValueId.HasValue))
            {
                // If the token is valid, this method will return it as an object that we can pull subject from. Even if the token is valid,
                // if the subject is not set, we cannot match it to a person search key
                var jwt = await ValidateToken(config, jwtString);

                if (jwt == null || jwt.Subject.IsNullOrWhiteSpace())
                {
                    continue;
                }

                // Get all the people (it is possible to get more than one) that have the matching search key
                var people = query
                             .Where(psk => psk.SearchTypeValueId == config.SearchTypeValueId.Value && psk.SearchValue == jwt.Subject)
                             .Select(psk => psk.PersonAlias.Person)
                             .ToList();

                // If there are more than one match, then the Rock admin needs to take action and fix the data as this could be a security hole and we
                // cannot tell which person is the bearer of the JWT
                if (people.Count > 1)
                {
                    ExceptionLogService.LogException($"{people.Count} people matched the JWT subject '{jwt.Subject}' for search value id '{config.SearchTypeValueId.Value}'");
                    continue;
                }

                // If there is a single match, then this method is done and there is no need to check more configurations
                if (people.Count == 1)
                {
                    return(people.Single());
                }
            }

            // None of the configurations was able to validate the token and find a matching person
            return(null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Delete the test data
        /// </summary>
        private static void DeleteTestData()
        {
            using (var rockContext = new RockContext())
            {
                var stepService = new StepService(rockContext);
                var stepQuery   = stepService.Queryable().Where(s => s.ForeignKey == ForeignKey);
                stepService.DeleteRange(stepQuery);
                rockContext.SaveChanges();
            }

            using (var rockContext = new RockContext())
            {
                var stepProgramService = new StepProgramService(rockContext);
                var stepProgramQuery   = stepProgramService.Queryable().Where(sp => sp.ForeignKey == ForeignKey);
                stepProgramService.DeleteRange(stepProgramQuery);
                rockContext.SaveChanges();
            }

            using (var rockContext = new RockContext())
            {
                var personSearchKeyService = new PersonSearchKeyService(rockContext);
                var personSearchKeyQuery   = personSearchKeyService.Queryable()
                                             .Where(psk =>
                                                    psk.PersonAlias.Person.ForeignKey == ForeignKey ||
                                                    PersonGuids.Contains(psk.PersonAlias.Person.Guid));
                personSearchKeyService.DeleteRange(personSearchKeyQuery);
                rockContext.SaveChanges();
            }

            using (var rockContext = new RockContext())
            {
                var personAliasService = new PersonAliasService(rockContext);
                var personAliasQuery   = personAliasService.Queryable()
                                         .Where(pa =>
                                                pa.Person.ForeignKey == ForeignKey ||
                                                PersonGuids.Contains(pa.Person.Guid));
                personAliasService.DeleteRange(personAliasQuery);
                rockContext.SaveChanges();
            }

            using (var rockContext = new RockContext())
            {
                var personService = new PersonService(rockContext);
                var personQuery   = personService.Queryable()
                                    .Where(p =>
                                           p.ForeignKey == ForeignKey ||
                                           PersonGuids.Contains(p.Guid));
                personService.DeleteRange(personQuery);
                rockContext.SaveChanges();
            }
        }
Ejemplo n.º 5
0
        public HttpResponseMessage GetEntityGuid(string code)
        {
            if (code.StartsWith("MCR"))
            {
                var mobileCheckinRecord = MobileCheckinRecordCache.GetByAccessKey(code);
                if (mobileCheckinRecord == null)
                {
                    throw new Exception("Invalid Key");
                }
            }
            else
            {
                //Check to see if is a person's search key
                RockContext            rockContext            = new RockContext();
                PersonSearchKeyService personSearchKeyService = new PersonSearchKeyService(rockContext);
                var key = personSearchKeyService.Queryable().Where(k => k.SearchValue == code);
                if (!key.Any())
                {
                    throw new Exception("Invalid Key");
                }
            }

            var qr = GenerateQR(code);

            if (qr == null)
            {
                throw new Exception("Code Invalid");
            }
            MemoryStream stream = new MemoryStream();

            qr.Save(stream, ImageFormat.Png);
            var buffer   = stream.ToArray();
            var response = Request.CreateResponse(HttpStatusCode.OK);

            response.Content = new StreamContent(new MemoryStream(buffer));

            response.Content.Headers.ContentType   = new MediaTypeHeaderValue("image/png");
            response.Content.Headers.ContentLength = stream.Length;

            return(response);
        }
        /// <summary>
        /// Delete the test data
        /// </summary>
        private static void DeleteTestData()
        {
            var rockContext = new RockContext();

            var stepProgramService = new StepProgramService(rockContext);
            var stepProgramQuery   = stepProgramService.Queryable().Where(sp => sp.ForeignKey == ForeignKey);

            stepProgramService.DeleteRange(stepProgramQuery);
            rockContext.SaveChanges();

            var dataViewFilterService = new DataViewFilterService(rockContext);
            var dvfQuery = dataViewFilterService.Queryable().Where(dvf => dvf.DataView.ForeignKey == ForeignKey || dvf.ForeignKey == ForeignKey);

            dataViewFilterService.DeleteRange(dvfQuery);
            rockContext.SaveChanges();

            var dataViewService = new DataViewService(rockContext);
            var dvQuery         = dataViewService.Queryable().Where(dv => dv.ForeignKey == ForeignKey);

            dataViewService.DeleteRange(dvQuery);
            rockContext.SaveChanges();

            var personSearchKeyService = new PersonSearchKeyService(rockContext);
            var personSearchKeyQuery   = personSearchKeyService.Queryable().Where(psk => psk.PersonAlias.Person.ForeignKey == ForeignKey);

            personSearchKeyService.DeleteRange(personSearchKeyQuery);

            var personAliasService = new PersonAliasService(rockContext);
            var personAliasQuery   = personAliasService.Queryable().Where(pa => pa.Person.ForeignKey == ForeignKey || pa.ForeignKey == ForeignKey);

            personAliasService.DeleteRange(personAliasQuery);
            rockContext.SaveChanges();

            var personService = new PersonService(rockContext);
            var personQuery   = personService.Queryable().Where(p => p.ForeignKey == ForeignKey);

            personService.DeleteRange(personQuery);
            rockContext.SaveChanges();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The workflow action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override bool Execute(RockContext rockContext, Model.WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            var checkInState = GetCheckInState(entity, out errorMessages);

            if (checkInState != null && checkInState.CheckIn.SearchType != null)
            {
                checkInState.CheckIn.Families = new List <CheckInFamily>();

                if (!string.IsNullOrWhiteSpace(checkInState.CheckIn.SearchValue))
                {
                    var personService = new PersonService(rockContext);
                    var memberService = new GroupMemberService(rockContext);
                    var groupService  = new GroupService(rockContext);

                    int personRecordTypeId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_RECORD_TYPE_PERSON.AsGuid()).Id;
                    int familyGroupTypeId  = GroupTypeCache.Get(Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY.AsGuid()).Id;
                    var dvInactive         = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_INACTIVE.AsGuid());

                    IQueryable <int> familyIdQry = null;

                    if (checkInState.CheckIn.SearchType.Guid.Equals(SystemGuid.DefinedValue.CHECKIN_SEARCH_TYPE_PHONE_NUMBER.AsGuid()))
                    {
                        string numericPhone = checkInState.CheckIn.SearchValue.AsNumeric();

                        var phoneQry = new PhoneNumberService(rockContext).Queryable().AsNoTracking();
                        if (checkInState.CheckInType == null || checkInState.CheckInType.PhoneSearchType == PhoneSearchType.EndsWith)
                        {
                            char[] charArray = numericPhone.ToCharArray();
                            Array.Reverse(charArray);
                            phoneQry = phoneQry.Where(o =>
                                                      o.NumberReversed.StartsWith(new string( charArray )));
                        }
                        else
                        {
                            phoneQry = phoneQry.Where(o =>
                                                      o.Number.Contains(numericPhone));
                        }

                        var tmpQry = phoneQry.Join(personService.Queryable().AsNoTracking(),
                                                   o => new { PersonId = o.PersonId, IsDeceased = false, RecordTypeValueId = personRecordTypeId },
                                                   p => new { PersonId = p.Id, IsDeceased = p.IsDeceased, RecordTypeValueId = p.RecordTypeValueId.Value },
                                                   (pn, p) => new { Person = p, PhoneNumber = pn })
                                     .Join(memberService.Queryable().AsNoTracking(),
                                           pn => pn.Person.Id,
                                           m => m.PersonId,
                                           (o, m) => new { PersonNumber = o.PhoneNumber, GroupMember = m });

                        familyIdQry = groupService.Queryable().Where(g => tmpQry.Any(o => o.GroupMember.GroupId == g.Id) && g.GroupTypeId == familyGroupTypeId)
                                      .Select(g => g.Id)
                                      .Distinct();
                    }
                    else
                    {
                        var familyMemberQry = memberService
                                              .Queryable().AsNoTracking()
                                              .Where(m =>
                                                     m.Group.GroupTypeId == familyGroupTypeId &&
                                                     m.Person.RecordTypeValueId == personRecordTypeId);

                        if (checkInState.CheckIn.SearchType.Guid.Equals(SystemGuid.DefinedValue.CHECKIN_SEARCH_TYPE_NAME.AsGuid()))
                        {
                            var personIds = personService.GetByFullName(checkInState.CheckIn.SearchValue, false).AsNoTracking().Select(p => p.Id);
                            familyMemberQry = familyMemberQry.Where(f => personIds.Contains(f.PersonId));
                        }
                        else if (checkInState.CheckIn.SearchType.Guid.Equals(SystemGuid.DefinedValue.CHECKIN_SEARCH_TYPE_SCANNED_ID.AsGuid()))
                        {
                            var personIds = new List <int>();

                            var dv = DefinedValueCache.Get(SystemGuid.DefinedValue.PERSON_SEARCH_KEYS_ALTERNATE_ID.AsGuid());
                            if (dv != null)
                            {
                                var searchValueService = new PersonSearchKeyService(rockContext);
                                var personAliases      = searchValueService.Queryable().AsNoTracking()
                                                         .Where(v =>
                                                                v.SearchTypeValueId == dv.Id &&
                                                                v.SearchValue == checkInState.CheckIn.SearchValue)
                                                         .Select(v => v.PersonAlias);

                                if (personAliases.Any())
                                {
                                    checkInState.CheckIn.CheckedInByPersonAliasId = personAliases.First().Id;
                                    personIds = personAliases.Select(a => a.PersonId).ToList();
                                }
                            }

                            if (personIds.Any())
                            {
                                familyMemberQry = familyMemberQry.Where(f => personIds.Contains(f.PersonId));
                            }
                            else
                            {
                                // if there were no matches, try to find a family check-in identifier. V8 has a "run once" job that moves the family identifiers
                                // to person search values, but in case the job has not yet completed, will still do the check for family ids.
                                var entityIds = new List <int>();

                                var attributeValueService = new AttributeValueService(rockContext);
                                var attr = AttributeCache.Get("8F528431-A438-4488-8DC3-CA42E66C1B37".AsGuid());
                                if (attr != null)
                                {
                                    entityIds = new AttributeValueService(rockContext)
                                                .Queryable().AsNoTracking()
                                                .Where(v =>
                                                       v.AttributeId == attr.Id &&
                                                       v.EntityId.HasValue &&
                                                       ("|" + v.Value + "|").Contains("|" + checkInState.CheckIn.SearchValue + "|"))
                                                .Select(v => v.EntityId.Value)
                                                .ToList();
                                }

                                familyMemberQry = familyMemberQry.Where(f => entityIds.Contains(f.GroupId));
                            }
                        }
                        else if (checkInState.CheckIn.SearchType.Guid.Equals(SystemGuid.DefinedValue.CHECKIN_SEARCH_TYPE_FAMILY_ID.AsGuid()))
                        {
                            List <int> searchFamilyIds = checkInState.CheckIn.SearchValue.SplitDelimitedValues().AsIntegerList();
                            familyMemberQry = familyMemberQry.Where(f => searchFamilyIds.Contains(f.GroupId));
                        }
                        else
                        {
                            errorMessages.Add("Invalid Search Type");
                            return(false);
                        }

                        familyIdQry = familyMemberQry
                                      .Select(m => m.GroupId)
                                      .Distinct();
                    }

                    int maxResults = checkInState.CheckInType != null ? checkInState.CheckInType.MaxSearchResults : 100;
                    if (maxResults > 0)
                    {
                        familyIdQry = familyIdQry.Take(maxResults);
                    }

                    var familyIds = familyIdQry.ToList();

                    // Load the family members
                    var familyMembers = memberService
                                        .Queryable("Group,GroupRole,Person").AsNoTracking()
                                        .Where(m => familyIds.Contains(m.GroupId))
                                        .ToList();

                    // Add each family
                    foreach (int familyId in familyIds)
                    {
                        // Get each of the members for this family
                        var familyMemberQry = familyMembers
                                              .Where(m =>
                                                     m.GroupId == familyId &&
                                                     m.Person.NickName != null);

                        if (checkInState.CheckInType != null && checkInState.CheckInType.PreventInactivePeople && dvInactive != null)
                        {
                            familyMemberQry = familyMemberQry
                                              .Where(m =>
                                                     m.Person.RecordStatusValueId != dvInactive.Id);
                        }

                        var thisFamilyMembers = familyMemberQry.ToList();

                        if (thisFamilyMembers.Any())
                        {
                            var group = thisFamilyMembers
                                        .Select(m => m.Group)
                                        .FirstOrDefault();

                            var firstNames = thisFamilyMembers
                                             .OrderBy(m => m.GroupRole.Order)
                                             .ThenBy(m => m.Person.BirthYear)
                                             .ThenBy(m => m.Person.BirthMonth)
                                             .ThenBy(m => m.Person.BirthDay)
                                             .ThenBy(m => m.Person.Gender)
                                             .Select(m => m.Person.NickName)
                                             .ToList();

                            var family = new CheckInFamily();
                            family.Group      = group.Clone(false);
                            family.Caption    = group.ToString();
                            family.SubCaption = firstNames.AsDelimited(", ");
                            checkInState.CheckIn.Families.Add(family);
                        }
                    }
                }

                return(true);
            }

            errorMessages.Add("Invalid Check-in State");
            return(false);
        }
Ejemplo n.º 8
0
        /// <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)
        {
            var rockContext = new RockContext();

            var    personService = new PersonService(rockContext);
            Person business      = null;

            if (int.Parse(hfBusinessId.Value) != 0)
            {
                business = personService.Get(int.Parse(hfBusinessId.Value));
            }

            if (business == null)
            {
                business = new Person();
                personService.Add(business);
                tbBusinessName.Text = tbBusinessName.Text.FixCase();
            }

            // Business Name
            business.LastName = tbBusinessName.Text;

            // Phone Number
            var businessPhoneTypeId = new DefinedValueService(rockContext).GetByGuid(new Guid(Rock.SystemGuid.DefinedValue.PERSON_PHONE_TYPE_WORK)).Id;

            var phoneNumber = business.PhoneNumbers.FirstOrDefault(n => n.NumberTypeValueId == businessPhoneTypeId);

            if (!string.IsNullOrWhiteSpace(PhoneNumber.CleanNumber(pnbPhone.Number)))
            {
                if (phoneNumber == null)
                {
                    phoneNumber = new PhoneNumber {
                        NumberTypeValueId = businessPhoneTypeId
                    };
                    business.PhoneNumbers.Add(phoneNumber);
                }
                phoneNumber.CountryCode        = PhoneNumber.CleanNumber(pnbPhone.CountryCode);
                phoneNumber.Number             = PhoneNumber.CleanNumber(pnbPhone.Number);
                phoneNumber.IsMessagingEnabled = cbSms.Checked;
                phoneNumber.IsUnlisted         = cbUnlisted.Checked;
            }
            else
            {
                if (phoneNumber != null)
                {
                    business.PhoneNumbers.Remove(phoneNumber);
                    new PhoneNumberService(rockContext).Delete(phoneNumber);
                }
            }

            // Record Type - this is always "business". it will never change.
            business.RecordTypeValueId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_RECORD_TYPE_BUSINESS.AsGuid()).Id;

            // Record Status
            business.RecordStatusValueId = dvpRecordStatus.SelectedValueAsInt();;

            // Record Status Reason
            int?newRecordStatusReasonId = null;

            if (business.RecordStatusValueId.HasValue && business.RecordStatusValueId.Value == DefinedValueCache.Get(new Guid(Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_INACTIVE)).Id)
            {
                newRecordStatusReasonId = dvpReason.SelectedValueAsInt();
            }
            business.RecordStatusReasonValueId = newRecordStatusReasonId;

            // Email
            business.IsEmailActive   = true;
            business.Email           = tbEmail.Text.Trim();
            business.EmailPreference = rblEmailPreference.SelectedValue.ConvertToEnum <EmailPreference>();

            avcEditAttributes.GetEditValues(business);

            if (!business.IsValid)
            {
                // Controls will render the error messages
                return;
            }

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

                // Add/Update Family Group
                var familyGroupType = GroupTypeCache.GetFamilyGroupType();
                int adultRoleId     = familyGroupType.Roles
                                      .Where(r => r.Guid.Equals(Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT.AsGuid()))
                                      .Select(r => r.Id)
                                      .FirstOrDefault();
                var adultFamilyMember = UpdateGroupMember(business.Id, familyGroupType, business.LastName + " Business", ddlCampus.SelectedValueAsInt(), adultRoleId, rockContext);
                business.GivingGroup  = adultFamilyMember.Group;

                // Add/Update Known Relationship Group Type
                var knownRelationshipGroupType   = GroupTypeCache.Get(Rock.SystemGuid.GroupType.GROUPTYPE_KNOWN_RELATIONSHIPS.AsGuid());
                int knownRelationshipOwnerRoleId = knownRelationshipGroupType.Roles
                                                   .Where(r => r.Guid.Equals(Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_OWNER.AsGuid()))
                                                   .Select(r => r.Id)
                                                   .FirstOrDefault();
                var knownRelationshipOwner = UpdateGroupMember(business.Id, knownRelationshipGroupType, "Known Relationship", null, knownRelationshipOwnerRoleId, rockContext);

                // Add/Update Implied Relationship Group Type
                var impliedRelationshipGroupType   = GroupTypeCache.Get(Rock.SystemGuid.GroupType.GROUPTYPE_PEER_NETWORK.AsGuid());
                int impliedRelationshipOwnerRoleId = impliedRelationshipGroupType.Roles
                                                     .Where(r => r.Guid.Equals(Rock.SystemGuid.GroupRole.GROUPROLE_PEER_NETWORK_OWNER.AsGuid()))
                                                     .Select(r => r.Id)
                                                     .FirstOrDefault();
                var impliedRelationshipOwner = UpdateGroupMember(business.Id, impliedRelationshipGroupType, "Implied Relationship", null, impliedRelationshipOwnerRoleId, rockContext);

                rockContext.SaveChanges();

                // Location
                int workLocationTypeId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_WORK).Id;

                var groupLocationService = new GroupLocationService(rockContext);
                var workLocation         = groupLocationService.Queryable("Location")
                                           .Where(gl =>
                                                  gl.GroupId == adultFamilyMember.Group.Id &&
                                                  gl.GroupLocationTypeValueId == workLocationTypeId)
                                           .FirstOrDefault();

                if (string.IsNullOrWhiteSpace(acAddress.Street1))
                {
                    if (workLocation != null)
                    {
                        if (cbSaveFormerAddressAsPreviousAddress.Checked)
                        {
                            GroupLocationHistorical.CreateCurrentRowFromGroupLocation(workLocation, RockDateTime.Now);
                        }

                        groupLocationService.Delete(workLocation);
                    }
                }
                else
                {
                    var newLocation = new LocationService(rockContext).Get(acAddress.Street1, acAddress.Street2, acAddress.City, acAddress.State, acAddress.PostalCode, acAddress.Country);
                    if (workLocation == null)
                    {
                        workLocation = new GroupLocation();
                        groupLocationService.Add(workLocation);
                        workLocation.GroupId = adultFamilyMember.Group.Id;
                        workLocation.GroupLocationTypeValueId = workLocationTypeId;
                    }
                    else
                    {
                        // Save this to history if the box is checked and the new info is different than the current one.
                        if (cbSaveFormerAddressAsPreviousAddress.Checked && newLocation.Id != workLocation.Location.Id)
                        {
                            new GroupLocationHistoricalService(rockContext).Add(GroupLocationHistorical.CreateCurrentRowFromGroupLocation(workLocation, RockDateTime.Now));
                        }
                    }

                    workLocation.Location          = newLocation;
                    workLocation.IsMailingLocation = true;
                }

                rockContext.SaveChanges();

                hfBusinessId.Value = business.Id.ToString();
            });

            /* Ethan Drotning 2022-01-11
             * Need save the PersonSearchKeys outside of the transaction since the DB might not have READ_COMMITTED_SNAPSHOT enabled.
             */

            // PersonSearchKey
            var personSearchKeyService = new PersonSearchKeyService(rockContext);
            var validSearchTypes       = GetValidSearchKeyTypes();
            var databaseSearchKeys     = personSearchKeyService.Queryable().Where(a => a.PersonAlias.PersonId == business.Id && validSearchTypes.Contains(a.SearchTypeValue.Guid)).ToList();

            foreach (var deletedSearchKey in databaseSearchKeys.Where(a => !PersonSearchKeysState.Any(p => p.Guid == a.Guid)))
            {
                personSearchKeyService.Delete(deletedSearchKey);
            }

            foreach (var personSearchKey in PersonSearchKeysState.Where(a => !databaseSearchKeys.Any(d => d.Guid == a.Guid)))
            {
                personSearchKey.PersonAliasId = business.PrimaryAliasId.Value;
                personSearchKeyService.Add(personSearchKey);
            }

            rockContext.SaveChanges();

            business.SaveAttributeValues();

            var queryParams = new Dictionary <string, string>
            {
                { "BusinessId", hfBusinessId.Value }
            };

            NavigateToCurrentPage(queryParams);
        }