public PersonInfo AddNewPersonOrUpdate(string personName, string account, EAccountType accountType, EPersonNameTrust trust)
        {
            lock (_lockObj)
            {
                PersonInfo person = null;
                if (IsKnownAccount(account, accountType))
                {
                    person = GetPerson(account, accountType);
                    Debug.Assert(person != null, String.Format("PersonInfo is null. The account {0} does not seem to be associated with a contact.", account));
                    person.SetName(personName, trust);
                }
                else
                {
                    Debug.Assert(!String.IsNullOrEmpty(personName), String.Format("Invalid name for the contact with account {0}. Empty name is not valid.", account));
                    Debug.Assert(!String.IsNullOrEmpty(account), String.Format("Invalid account name for contact {0}. Empty account name is not valid.", personName));
                    Debug.Assert(!IsKnownAccount(account, accountType), "The account is already associated with a person.");
                    //Trace.WriteLineIf(account.ToLower() != account, String.Format("!!! Warning: added account {0} was not in lower case. Did you forget to put it in lower case?", account));

                    person = new PersonInfo(MailData, personName, trust);
                    _personIdToPerson[person.PersonId] = person;
                    AssignAccount(account, accountType, person);
                }

                return person;
            }
        }
 internal void CopyDataFromContact(PersonInfo fromPerson)
 {
     if (fromPerson.NameTrust > this.NameTrust)
         this.SetName(fromPerson.Name, fromPerson.NameTrust);
     else if (fromPerson.NameTrust == this.NameTrust)
     {
         int fromNameParts = fromPerson.Name.Split(new char[] { ' ' }).Length;
         int toNameParts = this.Name.Split(new char[] { ' ' }).Length;
         if (fromNameParts > toNameParts)
             this.SetName(fromPerson.Name, fromPerson.NameTrust);
     }
     SaveContact();
 }
        public void AssignAccount(string account, EAccountType accountType, PersonInfo person)
        {
            lock (_lockObj)
            {
                Debug.Assert(person != null);

                // create a new account
                Debug.Assert(!IsKnownAccount(account, accountType));
                int accountId = SQLAddNewAccount(account, (int)accountType, person.PersonId, person.Name);
                string accountKey = GetAccountKey(account, accountType);
                _accountToAccountId[accountKey] = accountId;
                _accountIdToAccount[accountId] = accountKey;
                _accountIdToPersonName[accountId] = person.Name;

                person.AddAccount(accountId);
                _accountIdToPersonId[accountId] = person.PersonId;		// use the same id as was set for the first email of the person
                SQLSetPersonId(accountId, person.PersonId);
            }
        }
 public PersonInfo ReassignAccountToNewAccount(int accountId)
 {
     PersonInfo fromPerson = (PersonInfo)GetPersonFromAccount(accountId);
     Debug.Assert(fromPerson != null);
     PersonInfo newPerson = null;
     lock (_lockObj) {
         newPerson = new PersonInfo(MailData, fromPerson.Name, fromPerson.NameTrust);
         _personIdToPerson[newPerson.PersonId] = newPerson;
     }
     Debug.Assert(newPerson != null);
     ReassignAccountToExistingAccount(accountId, fromPerson, newPerson);
     return newPerson;
 }
 public bool TryGetPerson(string account, EAccountType accountType, out PersonInfo person)
 {
     person = GetPerson(account, accountType);
     return person != null;
 }
        public void ReassignAccountToExistingAccount(int accountId, PersonInfo fromPerson, PersonInfo toPerson)
        {
            Debug.Assert(fromPerson != null);
            Debug.Assert(toPerson != null);
            if (fromPerson.PersonId == toPerson.PersonId)
                return;
            lock (_lockObj) {
                fromPerson.RemoveAccount(accountId);
                toPerson.AddAccount(accountId);

                _accountIdToPersonId[accountId] = toPerson.PersonId;
                SQLSetPersonId(accountId, toPerson.PersonId);

                // update person info based on account type
                EAccountType accountType = GetAccountType(accountId);

                // if the fromPerson has no more email contacts then we remove the reference to the fromPerson
                bool noMoreAccounts = (fromPerson.GetAccountIds().Count() == 0);
                if (noMoreAccounts) {
                    _personIdToPerson.Remove(fromPerson.PersonId);
                    SQLRemovePerson(fromPerson.PersonId);
                }
            }
        }
 public void MergePersons(PersonInfo fromPerson, PersonInfo toPerson)
 {
     Debug.Assert(fromPerson != null);
     Debug.Assert(toPerson != null);
     foreach (int id in fromPerson.GetAccountIds())
         ReassignAccountToExistingAccount(id, fromPerson, toPerson);
     lock (_lockObj) {
         toPerson.CopyDataFromContact(fromPerson);
     }
 }
        public bool LoadData()
        {
            DateTime start = DateTime.Now;

            lock (_lockObj)
            {
                try
                {
                    if (!SQLTablesCompatible())
                    {
                        SQLClearTables();
                        return false;
                    }

                    // get info about all accounts
                    DataTable table = SQLGetAccounts();
                    string accountKey;
                    int accountId;
                    string account;
                    string personName;
                    string metaData;
                    foreach (DataRow row in table.Rows)
                    {
                        try
                        {
                            accountId = (int)(long)row[0];
                            account = (string)row[1];
                            EAccountType accountType = (EAccountType)(byte)row[2];
                            int personId = (int)(long)row[3];
                            personName = (string)row[4];
                            metaData = (string)row[5];
                            Debug.Assert(!_accountIdToAccount.ContainsKey(accountId), String.Format("Account id {0} is not unique", accountId));		// account id has to be unique
                            Debug.Assert(!_accountToAccountId.ContainsKey(account), String.Format("Account name {0} is not unique", account));			// account has to be unique
                            Debug.Assert(accountId >= 0, "Account id is negative");
                            Debug.Assert(!string.IsNullOrEmpty(account), String.Format("Account for id {0} is empty or null", accountId));					// we should not allow empty accounts
                            accountKey = GetAccountKey(account, accountType);
                            _accountIdToAccount[accountId] = accountKey;
                            _accountToAccountId[accountKey] = accountId;
                            _accountIdToPersonId[accountId] = personId;
                            _accountIdToPersonName[accountId] = personName;
                            _accountIdToUUID[accountId] = metaData;
                            //if (!string.IsNullOrEmpty(metaData)) {
                            //	if (!_UUIDToAccountIdList.ContainsKey(metaData))
                            //		_UUIDToAccountIdList[metaData] = new List<int>();
                            //	_UUIDToAccountIdList[metaData].Add(accountId);
                            //}
                        }
                        catch (Exception e)
                        {
                            GenLib.Log.LogService.LogException("Failed to load a data row from EmailAddressed table. ", e);
                        }
                    }

                    table = SQLGetPeopleData();
                    foreach (DataRow row in table.Rows)
                    {
                        PersonInfo person = new PersonInfo(MailData, row);
                        _personIdToPerson[person.PersonId] = person;
                    }
                }
                catch (System.Exception ex)
                {
                    GenLib.Log.LogService.LogException("Failed to load contact information.", ex);
                    return false;
                }
            }

            GenLib.Log.LogService.LogInfo(String.Format("--- Loading people data needed {0:f0} miliseconds ---", (DateTime.Now - start).TotalMilliseconds));
            return true;
        }
 public int GetPersonTotalToCount(PersonInfo person)
 {
     if (person == null) return 0;
     int tot = 0;
     foreach (int id in person.GetAccountIds())
         tot += GetEmailToCount(id);
     return tot;
 }
 public int GetPersonTotalFromToCount(PersonInfo fromPerson, PersonInfo toPerson)
 {
     if (fromPerson == null) return 0;
     if (toPerson == null) return 0;
     int tot = 0;
     foreach (int e1 in fromPerson.GetAccountIds())
         foreach (int e2 in toPerson.GetAccountIds())
             tot += GetEmailFromToCount(e1, e2);
     return tot;
 }