public void Execute()
        {
            // Validate the minimum required fields
            if (String.IsNullOrEmpty(contact.Person.Name.Trim()))
            {
                Logger.Warn("Contact for channelprofilekey [{0}] has an empty name and an invalid sourceaddress, ignoring entry", LogSource.Sync, contact.Profile.ChannelProfileKey);

                result = ContactMatchResult.Error;

                return;
            }

            if (String.IsNullOrEmpty(contact.Profile.ChannelProfileKey))
            {
                Logger.Debug("Profile for person [{0}] did not have a valid ChannelProfileKey, ignoring entry", LogSource.Sync, contact.Person.Name);

                result = ContactMatchResult.Error;

                return;
            }

            if (contact.Profile.SourceAddress == null)
            {
                Logger.Warn("Contact for channelprofilekey [{0}] has an invalid sourceaddress, ignoring entry", LogSource.Sync, contact.Profile.ChannelProfileKey);

                result = ContactMatchResult.Error;

                return;
            }

            // Some contacts (especially from gmail) don't have names but only email adrreses,
            // use the email address as name in that case
            if (String.IsNullOrEmpty(contact.Person.Name.Trim()))
            {
                contact.Person.Name = contact.Profile.SourceAddress.ToString();
                contact.IsSoft = true;
            }

            // Try to match the person to our addressbook based on unique channelprofilekey
            profile = dataService.SelectBy<Profile>(new { ChannelProfileKey = contact.Profile.ChannelProfileKey });

            if (profile != null)
            {
                // Find person belonging to profile
                EnsurePerson();

                result = ContactMatchResult.MatchedOnProfile;

                return;
            }

            // Profile not found on channel profile key, perform a match on person name
            FindPersonByName();

            // Person has been redirected
            if (person != null && person.RedirectPersonId.HasValue)
                person = dataService.SelectByKey<Person>(person.RedirectPersonId.Value);

            if (person != null)
            {
                var matchOnAddress = dataService.SelectBy<Profile>(new { Address = contact.Profile.SourceAddress.Address });

                if (matchOnAddress != null)
                {
                    Logger.Debug("Found soft profile [{0}] for person [{1}] on address match [{2}], removing from mailbox and recreating new one...", LogSource.Sync,
                        matchOnAddress.ProfileId, contact.Person.Name, contact.Profile.SourceAddress.Address);

                    // We have a soft profile on the same address that we are now receiving a hard profile for,
                    // remove the soft profile so that the matcher will create a new hard profile.
                    ClientState.Current.DataService.Delete(matchOnAddress);
                }

                // Doesn't have this profile yet, add it
                Logger.Debug("Found new profile [{0}] for person [{1}]", LogSource.Sync, contact.Profile.ChannelProfileKey, contact.Person.Name);

                // Append new profile
                SaveProfile(contact);

                result = ContactMatchResult.MatchedOnPerson;
            }
            else
            {
                profile = dataService.SelectBy<Profile>(
                    String.Format("select * from Profiles where Address like '{0}'", contact.Profile.SourceAddress.Address.AddSQLiteSlashes()));

                // Try to match to profile address
                if (profile != null)
                {
                    EnsurePerson();

                    result = ContactMatchResult.MatchedOnProfile;

                    return;
                }

                // Unable to match, create new person
                SavePerson(contact);

                // Create new profile
                SaveProfile(contact);
            }
        }
Exemple #2
0
        public void Execute()
        {
            // Validate the minimum required fields
            if (String.IsNullOrEmpty(contact.Person.Name.Trim()))
            {
                Logger.Warn("Contact for channelprofilekey [{0}] has an empty name and an invalid sourceaddress, ignoring entry", LogSource.Sync, contact.Profile.ChannelProfileKey);

                result = ContactMatchResult.Error;

                return;
            }

            if (String.IsNullOrEmpty(contact.Profile.ChannelProfileKey))
            {
                Logger.Debug("Profile for person [{0}] did not have a valid ChannelProfileKey, ignoring entry", LogSource.Sync, contact.Person.Name);

                result = ContactMatchResult.Error;

                return;
            }

            if (contact.Profile.SourceAddress == null)
            {
                Logger.Warn("Contact for channelprofilekey [{0}] has an invalid sourceaddress, ignoring entry", LogSource.Sync, contact.Profile.ChannelProfileKey);

                result = ContactMatchResult.Error;

                return;
            }

            // Some contacts (especially from gmail) don't have names but only email adrreses,
            // use the email address as name in that case
            if (String.IsNullOrEmpty(contact.Person.Name.Trim()))
            {
                contact.Person.Name = contact.Profile.SourceAddress.ToString();
                contact.IsSoft      = true;
            }

            // Try to match the person to our addressbook based on unique channelprofilekey
            profile = dataService.SelectBy <Profile>(new { ChannelProfileKey = contact.Profile.ChannelProfileKey });

            if (profile != null)
            {
                // Find person belonging to profile
                EnsurePerson();

                result = ContactMatchResult.MatchedOnProfile;

                return;
            }

            // Profile not found on channel profile key, perform a match on person name
            FindPersonByName();

            // Person has been redirected
            if (person != null && person.RedirectPersonId.HasValue)
            {
                person = dataService.SelectByKey <Person>(person.RedirectPersonId.Value);
            }

            if (person != null)
            {
                var matchOnAddress = dataService.SelectBy <Profile>(new { Address = contact.Profile.SourceAddress.Address });

                if (matchOnAddress != null)
                {
                    Logger.Debug("Found soft profile [{0}] for person [{1}] on address match [{2}], removing from mailbox and recreating new one...", LogSource.Sync,
                                 matchOnAddress.ProfileId, contact.Person.Name, contact.Profile.SourceAddress.Address);

                    // We have a soft profile on the same address that we are now receiving a hard profile for,
                    // remove the soft profile so that the matcher will create a new hard profile.
                    ClientState.Current.DataService.Delete(matchOnAddress);
                }

                // Doesn't have this profile yet, add it
                Logger.Debug("Found new profile [{0}] for person [{1}]", LogSource.Sync, contact.Profile.ChannelProfileKey, contact.Person.Name);

                // Append new profile
                SaveProfile(contact);

                result = ContactMatchResult.MatchedOnPerson;
            }
            else
            {
                profile = dataService.SelectBy <Profile>(
                    String.Format("select * from Profiles where Address like '{0}'", contact.Profile.SourceAddress.Address.AddSQLiteSlashes()));

                // Try to match to profile address
                if (profile != null)
                {
                    EnsurePerson();

                    result = ContactMatchResult.MatchedOnProfile;

                    return;
                }

                // Unable to match, create new person
                SavePerson(contact);

                // Create new profile
                SaveProfile(contact);
            }
        }