Beispiel #1
0
        /// <summary>
        /// Cleanups the financial transaction null currency.
        /// </summary>
        /// <param name="dataMap">The data map.</param>
        /// <returns></returns>
        private int CleanupFinancialTransactionNullCurrency(JobDataMap dataMap)
        {
            int totalRowsUpdated = 0;
            var rockContext      = new Rock.Data.RockContext();

            int?currencyTypeUnknownId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.CURRENCY_TYPE_UNKNOWN.AsGuid())?.Id;

            if (currencyTypeUnknownId.HasValue)
            {
                var financialPaymentDetailsToUpdate = new FinancialPaymentDetailService(rockContext).Queryable().Where(a => a.CurrencyTypeValueId == null);

                totalRowsUpdated = rockContext.BulkUpdate(financialPaymentDetailsToUpdate, a => new FinancialPaymentDetail {
                    CurrencyTypeValueId = currencyTypeUnknownId.Value
                });
            }

            return(totalRowsUpdated);
        }
Beispiel #2
0
        /// <summary>
        /// Does cleanup of Person Aliases and Metaphones
        /// </summary>
        /// <param name="dataMap">The data map.</param>
        private void PersonCleanup(JobDataMap dataMap)
        {
            // Add any missing person aliases
            using (var personRockContext = new Rock.Data.RockContext())
            {
                PersonService      personService      = new PersonService(personRockContext);
                PersonAliasService personAliasService = new PersonAliasService(personRockContext);
                var personAliasServiceQry             = personAliasService.Queryable();
                foreach (var person in personService.Queryable("Aliases")
                         .Where(p => !p.Aliases.Any() && !personAliasServiceQry.Any(pa => pa.AliasPersonId == p.Id))
                         .Take(300))
                {
                    person.Aliases.Add(new PersonAlias {
                        AliasPersonId = person.Id, AliasPersonGuid = person.Guid
                    });
                }

                personRockContext.SaveChanges();
            }

            AddMissingAlternateIds();

            using (var personRockContext = new Rock.Data.RockContext())
            {
                PersonService personService = new PersonService(personRockContext);
                // Add any missing metaphones
                int namesToProcess = dataMap.GetString("MaxMetaphoneNames").AsIntegerOrNull() ?? 500;
                if (namesToProcess > 0)
                {
                    var firstNameQry = personService.Queryable().Select(p => p.FirstName).Where(p => p != null);
                    var nickNameQry  = personService.Queryable().Select(p => p.NickName).Where(p => p != null);
                    var lastNameQry  = personService.Queryable().Select(p => p.LastName).Where(p => p != null);
                    var nameQry      = firstNameQry.Union(nickNameQry.Union(lastNameQry));

                    var metaphones    = personRockContext.Metaphones;
                    var existingNames = metaphones.Select(m => m.Name).Distinct();

                    // Get the names that have not yet been processed
                    var namesToUpdate = nameQry
                                        .Where(n => !existingNames.Contains(n))
                                        .Take(namesToProcess)
                                        .ToList();

                    foreach (string name in namesToUpdate)
                    {
                        string mp1 = string.Empty;
                        string mp2 = string.Empty;
                        Rock.Utility.DoubleMetaphone.doubleMetaphone(name, ref mp1, ref mp2);

                        var metaphone = new Metaphone();
                        metaphone.Name       = name;
                        metaphone.Metaphone1 = mp1;
                        metaphone.Metaphone2 = mp2;

                        metaphones.Add(metaphone);
                    }

                    personRockContext.SaveChanges(disablePrePostProcessing: true);
                }
            }

            // Ensures the PrimaryFamily is correct for all person records in the database
            using (var personRockContext = new Rock.Data.RockContext())
            {
                int primaryFamilyUpdates = PersonService.UpdatePrimaryFamilyAll(personRockContext);
            }

            // update any updated or incorrect age classifications on persons
            using (var personRockContext = new Rock.Data.RockContext())
            {
                int ageClassificationUpdates = PersonService.UpdatePersonAgeClassificationAll(personRockContext);
            }

            //// Add any missing Implied/Known relationship groups
            // Known Relationship Group
            AddMissingRelationshipGroups(GroupTypeCache.Get(Rock.SystemGuid.GroupType.GROUPTYPE_KNOWN_RELATIONSHIPS), Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_OWNER.AsGuid());

            // Implied Relationship Group
            AddMissingRelationshipGroups(GroupTypeCache.Get(Rock.SystemGuid.GroupType.GROUPTYPE_PEER_NETWORK), Rock.SystemGuid.GroupRole.GROUPROLE_PEER_NETWORK_OWNER.AsGuid());

            // Find family groups that have no members or that have only 'inactive' people (record status) and mark the groups inactive.
            using (var familyRockContext = new Rock.Data.RockContext())
            {
                int familyGroupTypeId           = GroupTypeCache.GetFamilyGroupType().Id;
                int recordStatusInactiveValueId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_INACTIVE.AsGuid()).Id;

                var activeFamilyWithNoActiveMembers = new GroupService(familyRockContext).Queryable()
                                                      .Where(a => a.GroupTypeId == familyGroupTypeId && a.IsActive == true)
                                                      .Where(a => !a.Members.Where(m => m.Person.RecordStatusValueId != recordStatusInactiveValueId).Any());

                var currentDateTime = RockDateTime.Now;

                familyRockContext.BulkUpdate(activeFamilyWithNoActiveMembers, x => new Rock.Model.Group
                {
                    IsActive = false
                });
            }
        }