private static object GetSearchKey(Person person, RockContext rockContext) { var alternateIdSearchTypeValueId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_SEARCH_KEYS_ALTERNATE_ID.AsGuid()).Id; var searchKey = person.GetPersonSearchKeys(rockContext).Where(k => k.SearchTypeValueId == alternateIdSearchTypeValueId).FirstOrDefault(); if (searchKey != null) { return(searchKey.SearchValue); } //Create Search Key if there is none. var personSearchKeyService = new PersonSearchKeyService(rockContext); var personService = new PersonService(rockContext); var personAlias = personService.Get(person.Id).Aliases.First(); PersonSearchKey personSearchKey = new PersonSearchKey() { PersonAlias = personAlias, SearchTypeValueId = alternateIdSearchTypeValueId, SearchValue = PersonSearchKeyService.GenerateRandomAlternateId(true, rockContext) }; personSearchKeyService.Add(personSearchKey); rockContext.SaveChanges(); return(personSearchKey.SearchValue); }
/// <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); } } }
/// <summary> /// Handles the Click event of the lbGenerate 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 lbGenerate_Click(object sender, EventArgs e) { AlternateId = PersonSearchKeyService.GenerateRandomAlternateId(true); }