internal static IList <ContactInfoForSuggestion> ConvertAll(IList <IStorePropertyBag> inputContacts)
        {
            Util.ThrowOnNullArgument(inputContacts, "inputContacts");
            List <ContactInfoForSuggestion> list = new List <ContactInfoForSuggestion>(inputContacts.Count);

            foreach (IStorePropertyBag propertyBag in inputContacts)
            {
                list.Add(ContactInfoForSuggestion.Create(propertyBag));
            }
            return(list);
        }
        private static HashSet <string> GetStringSet(Func <string, string> normalize, IStorePropertyBag propertyBag, params PropertyDefinition[] properties)
        {
            HashSet <string> hashSet = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (PropertyDefinition property in properties)
            {
                string @string = ContactInfoForSuggestion.GetString(normalize, propertyBag, property);
                if (@string != null)
                {
                    hashSet.Add(@string);
                }
            }
            return(hashSet);
        }
        private static int GetPartialAliasOfEmailAddressMatch(ContactInfoForSuggestion a, ContactInfoForSuggestion b)
        {
            int num = 0;

            if (a.AliasOfEmailAddresses != null && b.AliasOfEmailAddresses != null)
            {
                foreach (string a2 in a.AliasOfEmailAddresses)
                {
                    foreach (string b2 in b.AliasOfEmailAddresses)
                    {
                        num = Math.Max(num, ContactLinkingSuggestionMatching.AliasOrEmailAddress.GetPartialMatchCount(CultureInfo.InvariantCulture, a2, b2));
                    }
                }
            }
            return(num);
        }
        internal static ContactInfoForSuggestion Create(IStorePropertyBag propertyBag)
        {
            Util.ThrowOnNullArgument(propertyBag, "propertyBag");
            StoreId  valueOrDefault  = propertyBag.GetValueOrDefault <StoreId>(ItemSchema.Id, null);
            PersonId valueOrDefault2 = propertyBag.GetValueOrDefault <PersonId>(ContactSchema.PersonId, null);

            PersonId[]       valueOrDefault3 = propertyBag.GetValueOrDefault <PersonId[]>(ContactSchema.LinkRejectHistory, null);
            string           @string         = ContactInfoForSuggestion.GetString(new Func <string, string>(ContactInfoForSuggestion.NormalizeName), propertyBag, ContactSchema.GivenName);
            string           string2         = ContactInfoForSuggestion.GetString(new Func <string, string>(ContactInfoForSuggestion.NormalizeName), propertyBag, ContactSchema.Surname);
            string           valueOrDefault4 = propertyBag.GetValueOrDefault <string>(ContactSchema.PartnerNetworkId, null);
            string           valueOrDefault5 = propertyBag.GetValueOrDefault <string>(ItemSchema.ParentDisplayName, null);
            HashSet <string> stringSet       = ContactInfoForSuggestion.GetStringSet(new Func <string, string>(ContactInfoForSuggestion.NormalizeEmailAddress), propertyBag, new PropertyDefinition[]
            {
                ContactSchema.Email1EmailAddress,
                ContactSchema.Email2EmailAddress,
                ContactSchema.Email3EmailAddress,
                ContactSchema.PrimarySmtpAddress
            });
            HashSet <string> aliasOfEmailAddresses = ContactInfoForSuggestion.GetAliasOfEmailAddresses(stringSet);
            HashSet <string> stringSet2            = ContactInfoForSuggestion.GetStringSet(new Func <string, string>(Util.NormalizePhoneNumber), propertyBag, new PropertyDefinition[]
            {
                ContactSchema.BusinessPhoneNumber,
                ContactSchema.BusinessPhoneNumber2,
                ContactSchema.HomePhone,
                ContactSchema.MobilePhone,
                ContactSchema.OtherTelephone
            });

            return(new ContactInfoForSuggestion
            {
                ItemId = valueOrDefault,
                PersonId = valueOrDefault2,
                LinkRejectHistory = valueOrDefault3,
                GivenName = @string,
                Surname = string2,
                EmailAddresses = stringSet,
                AliasOfEmailAddresses = aliasOfEmailAddresses,
                PhoneNumbers = stringSet2,
                PartnerNetworkId = valueOrDefault4,
                ParentDisplayName = valueOrDefault5
            });
        }
 private static bool IsPhoneNumberMatch(ContactInfoForSuggestion a, ContactInfoForSuggestion b)
 {
     return(a.PhoneNumbers != null && b.PhoneNumbers != null && a.PhoneNumbers.Overlaps(b.PhoneNumbers));
 }
 private static bool IsAliasOfEmailAddressMatch(ContactInfoForSuggestion a, ContactInfoForSuggestion b)
 {
     return(a.AliasOfEmailAddresses != null && b.AliasOfEmailAddresses != null && a.AliasOfEmailAddresses.Overlaps(b.AliasOfEmailAddresses));
 }
        internal static ContactLinkingSuggestion Create(CultureInfo culture, ContactInfoForSuggestion personContact, ContactInfoForSuggestion otherContact)
        {
            Util.ThrowOnNullArgument(culture, "culture");
            Util.ThrowOnNullArgument(personContact, "personContact");
            Util.ThrowOnNullArgument(otherContact, "otherContact");
            if (otherContact.PersonId == null)
            {
                ContactLinkingSuggestion.Tracer.TraceError <StoreId>(0L, "Cannot use contact without PersonId: {0}", otherContact.ItemId);
                return(null);
            }
            if (otherContact.PersonId.Equals(personContact.PersonId))
            {
                ContactLinkingSuggestion.Tracer.TraceDebug <StoreId>(0L, "Ignoring contact {0} because it has same PersonId of the person we are looking suggestions for", otherContact.ItemId);
                return(null);
            }
            if (otherContact.LinkRejectHistory != null && Array.Exists <PersonId>(otherContact.LinkRejectHistory, (PersonId otherContactLinkReject) => personContact.PersonId.Equals(otherContactLinkReject)))
            {
                ContactLinkingSuggestion.Tracer.TraceDebug <StoreId, PersonId>(0L, "Ignoring contact {0} because its LinkRejectHistory has PersonId of the person we are looking suggestions for: {1}", otherContact.ItemId, personContact.PersonId);
                return(null);
            }
            if (personContact.LinkRejectHistory != null && Array.Exists <PersonId>(personContact.LinkRejectHistory, (PersonId personPersonId) => personPersonId.Equals(otherContact.PersonId)))
            {
                ContactLinkingSuggestion.Tracer.TraceDebug <StoreId, PersonId>(0L, "Ignoring contact {0} because its PersonId is present in LinkRejectHistory of the person we are looking suggestions for: {1}", otherContact.ItemId, personContact.PersonId);
                return(null);
            }
            ContactLinkingSuggestion.NameCompareResult nameCompareResult  = ContactLinkingSuggestion.CompareNames(culture, personContact.GivenName, personContact.Surname, otherContact.GivenName, otherContact.Surname);
            ContactLinkingSuggestion.NameCompareResult nameCompareResult2 = ContactLinkingSuggestion.CompareNames(culture, personContact.GivenName, personContact.Surname, otherContact.Surname, otherContact.GivenName);
            if (nameCompareResult.IsFullMatch || nameCompareResult2.IsFullMatch)
            {
                return(new ContactLinkingSuggestion
                {
                    PersonId = otherContact.PersonId,
                    SurnameMatch = true,
                    GivenNameMatch = true
                });
            }
            if (ContactLinkingSuggestion.IsAliasOfEmailAddressMatch(personContact, otherContact))
            {
                return(new ContactLinkingSuggestion
                {
                    PersonId = otherContact.PersonId,
                    AliasOfEmailAddressMatch = true
                });
            }
            if (ContactLinkingSuggestion.IsPhoneNumberMatch(personContact, otherContact))
            {
                return(new ContactLinkingSuggestion
                {
                    PersonId = otherContact.PersonId,
                    PhoneNumberMatch = true
                });
            }
            if (nameCompareResult.IsPartialMatch)
            {
                return(new ContactLinkingSuggestion
                {
                    PersonId = otherContact.PersonId,
                    GivenNameMatch = nameCompareResult.FullGivenName,
                    SurnameMatch = nameCompareResult.FullSurname,
                    PartialSurnameMatch = nameCompareResult.PartialSurname,
                    PartialGivenNameMatch = nameCompareResult.PartialGivenName
                });
            }
            if (nameCompareResult2.IsPartialMatch)
            {
                return(new ContactLinkingSuggestion
                {
                    PersonId = otherContact.PersonId,
                    GivenNameMatch = nameCompareResult2.FullGivenName,
                    SurnameMatch = nameCompareResult2.FullSurname,
                    PartialSurnameMatch = nameCompareResult2.PartialSurname,
                    PartialGivenNameMatch = nameCompareResult2.PartialGivenName
                });
            }
            int partialAliasOfEmailAddressMatch = ContactLinkingSuggestion.GetPartialAliasOfEmailAddressMatch(personContact, otherContact);

            if (partialAliasOfEmailAddressMatch > 0)
            {
                return(new ContactLinkingSuggestion
                {
                    PersonId = otherContact.PersonId,
                    PartialAliasOfEmailAddressMatch = partialAliasOfEmailAddressMatch
                });
            }
            ContactLinkingSuggestion.Tracer.TraceDebug <StoreId>(0L, "Ignoring contact {0} because its doesn't match any criteria for suggestion.", otherContact.ItemId);
            return(null);
        }