private void AddOrUpdateContactsToDatabase() { if (isUserCancel) { return; } var list = new List<Contact>(); int count = listContact.Count(); LiveContact lContact; for (int i = 0; i < count; i++) { lContact = listContact[i]; var contact = new Contact(); contact.AccountId = account.Id; contact.Id = -1; contact.FirstName = lContact.First_name; contact.LastName = lContact.Last_name; // add list email List<Email> listEmail = new List<Email>(); List<TypeAndValue> emails = lContact.Emails; int size = emails.Count(); TypeAndValue typeAndValue; for (int j = 0; j < size; j++) { typeAndValue = emails[j]; var emailDB = new Email(); emailDB.Value = typeAndValue.Value; EmailType emailType = new EmailType(); string type = typeAndValue.Type; if (type.Equals("personal")) { emailType.Name = EmailTypeDB.LIVE_EMAIL_TYPE_PERSONAL_STRING; } else if (type.Equals("business")) { emailType.Name = EmailTypeDB.LIVE_EMAIL_TYPE_BUSINESS_STRING; } else { emailType.Name = EmailTypeDB.LIVE_EMAIL_TYPE_OTHER_STRING; } emailType.AccountType = new AccountType { Name = AccountTypeDB.TYPE_LIVE_HOTMAIL_STRING }; emailDB.EmailType = emailType; listEmail.Add(emailDB); } contact.Emails = listEmail; // add list fax var listFax = new List<Fax>(); List<TypeAndValue> phones = lContact.Phones; size = phones.Count(); for (int j = 0; j < size; j++) { typeAndValue = phones[j]; string type = typeAndValue.Type; if (type.EndsWith("fax")) { var faxDB = new Fax(); faxDB.Value = typeAndValue.Value; FaxType faxType = new FaxType(); if (type.Equals("home_fax")) { faxType.Name = FaxTypeDB.LIVE_FAX_TYPE_HOME_STRING; } else if (type.Equals("work_fax")) { faxType.Name = FaxTypeDB.LIVE_FAX_TYPE_WORK_STRING; } faxType.AccountType = new AccountType { Name = AccountTypeDB.TYPE_LIVE_HOTMAIL_STRING }; faxDB.FaxType = faxType; listFax.Add(faxDB); } } contact.Faxes = listFax; list.Add(contact); } CheckMaximumAndImport(list); }
private void AddOrUpdateContactsToDatabase() { if (isUserCancel) { return; } var list = new List<Contact>(); AddOrUpdateAccountWhenImportingFromAccount(); foreach (var item in listContact) { var contact = new Contact { AccountId = account.Id, FirstName = item.GivenName != null ? item.GivenName : string.Empty, MiddleName = item.MiddleName != null ? item.MiddleName : string.Empty, LastName = item.FamilyName != null ? item.FamilyName : string.Empty }; contact.Emails = new List<Email>(); var hasEmail = false; var hasPersonalEmail = false; var hasWorkEmail = false; foreach(var em in item.Emails) { var email = new Email { Value = em.Value }; email.EmailType = new EmailType(); if(em.Type.Equals("PERSONAL")) { if (!hasPersonalEmail) { hasPersonalEmail = true; email.EmailType.Name = EmailTypeDB.YAHOO_EMAIL_TYPE_PERSONAL_STRING; } } else if (em.Type.Equals("WORK")) { if (!hasWorkEmail) { hasWorkEmail = true; email.EmailType.Name = EmailTypeDB.YAHOO_EMAIL_TYPE_WORK_STRING; } } else { if(!hasEmail) { hasEmail = true; email.EmailType.Name = EmailTypeDB.YAHOO_EMAIL_TYPE_EMAIL_STRING; } } if(email.EmailType.Name != null) contact.Emails.Add(email); } contact.Faxes = new List<Fax>(); var hasFax = false; foreach(var fa in item.Phones) { if (fa.Type.Equals("FAX") && !hasFax) { hasFax = true; var fax = new Fax { Value = fa.Value }; fax.FaxType = new FaxType(); fax.FaxType.Name = FaxTypeDB.YAHOO_FAX_TYPE_FAX_STRING; contact.Faxes.Add(fax); } } list.Add(contact); } CheckMaximumAndImport(list); }
/// <summary> /// Import from CSV NEW /// </summary> /// <param name="list"></param> /// <param name="file"></param> /// <returns></returns> /// public FileImportResult ImportFromCSVMultiHeader(string data, DuplicateContactMode mode) { FileImportResult result = new FileImportResult(); string[] contactSplits = data.Split(new string[] { "\r", "\r\n", "\n" }, StringSplitOptions.None); var CSVHeader = contactSplits[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().Select(n => GroupDB.TrimSpace(GroupDB.RemoveControlChars(n))).ToArray(); int size = contactSplits.Count(); EmailType emailType = dbManager.GetPreferredEmailTypeByAccountTypeNameEF(AccountTypeDB.TYPE_CSV_STRING); FaxType faxType = dbManager.GetPreferredFaxTypeByAccountTypeNameEF(AccountTypeDB.TYPE_CSV_STRING); Account account = dbManager.GetCSVAccountEF(); string accTypeName = AccountTypeDB.TYPE_CSV_STRING; List<CMData.Contact> listContact = new List<CMData.Contact>(); CMData.Contact contact; if (! (CSVHeader.Contains<string>("first_name") || CSVHeader.Contains<string>("last_name")) || ! (CSVHeader.Contains<string>("email") || CSVHeader.Contains<string>("Business Fax"))) { result.errorCode = FileImExportController.CODE_FILE_STRUCTURE_NOT_SUITABLE; return result; } for (int i = 1; i < size; i++) { string bareData = contactSplits[i]; var valueSplits = splitCSVLine(bareData); if (valueSplits.Count() <= 1) continue; var last_name = String.Empty; var first_name = String.Empty; var middle_name = String.Empty; var email = String.Empty; var fax = String.Empty; for (int j = 0; j < valueSplits.Count(); j++) { var value = valueSplits.ElementAt(j); value = formatQuoteCommaToImport(value); string column = CSVHeader[j]; if (column.Equals("first_name")) { first_name = value; } if (column.Equals("last_name")) { last_name = value; } if (column.Equals("middle_name")) { middle_name = value; } if (column.Equals("email")) { email = value; //if (!validateEmail(email)) // email = ""; } if (column.Equals("Business Fax")) { fax = value; } } if (String.IsNullOrEmpty(first_name) && String.IsNullOrEmpty(last_name) && String.IsNullOrEmpty(email) && String.IsNullOrEmpty(fax)) { continue; } List<Email> listEmail = new List<Email>(); if (!String.IsNullOrEmpty(email)) { Email em = new Email(); em.Value = email; em.EmailType = emailType; em.EmailTypeId = emailType.Id; listEmail.Add(em); } List<Fax> listFax = new List<Fax>(); if (!String.IsNullOrEmpty(fax)) { Fax faxDB = new Fax(); faxDB.Value = fax; faxDB.FaxType = faxType; faxDB.FaxTypeId = faxType.Id; listFax.Add(faxDB); } contact = new CMData.Contact(); contact.AccountId = account.Id; contact.FirstName = first_name; contact.LastName = last_name; contact.MiddleName = middle_name; contact.Emails = listEmail; contact.Faxes = listFax; listContact.Add(contact); } result = AddOrUpdateContact(listContact, accTypeName, mode); return result; }
private void GetContacts(string line) { EmailType VCFemailType = dbManager.GetPreferredEmailTypeByAccountTypeNameEF(AccountTypeDB.TYPE_VCF_STRING); string begin = "BEGIN:VCARD"; string version = "VERSION:4.0"; string end = "END:VCARD"; string ms_design = "X-MS-OL-DESIGN"; string ms_picture = "X-MS-CARDPICTURE"; if (line.Equals(begin) || line.Equals(end) || line.Equals(version) || line.Contains(ms_picture) || line.Contains(ms_design)) { isVCFFormat = true; return; } if (contactVCF == null) { List<Email> listEmail = new List<Email>(); List<Fax> listFax = new List<Fax>(); var accountvcf = dbManager.GetVCFAccount(); contactVCF = new CMData.Contact { Emails = listEmail, Faxes = listFax, AccountId = accountvcf.Id, Accounts = accountvcf }; } if (line.Contains("FN;CHARSET=utf-8:")) { return; } else if (line.Contains("N;CHARSET=utf-8:") || line.Contains("LANGUAGE=en-us:") || (line.Contains("N:") && line.Contains(";"))) { var tmp = line.Split(':'); if (tmp.Count() > 0) { try { var name = tmp[1].Split(new string[] { ";" }, StringSplitOptions.None); if (name.Count() > 0) { contactVCF.FirstName = name[1] !=null ? name[1] : "" ; contactVCF.LastName = name[0] != null ? name[0] : ""; contactVCF.MiddleName = name[2] != null ? name[2] : ""; } } catch { isVCFFormat = false; return; } } } else if (line.Contains("EMAIL;") || line.Contains("EMAIL;type=INTERNET")) { var prefered = -1; Email email = new Email(); EmailType emType = new EmailType(); if (line.Contains("type=HOME")) { emType.Name = EmailTypeDB.VCF_EMAIL_TYPE_HOME_STRING; } else if (line.Contains("type=WORK")) { emType.Name = EmailTypeDB.VCF_EMAIL_TYPE_WORK_STRING; } else { //Others emType.Name = EmailTypeDB.VCF_EMAIL_TYPE_OTHER_STRING; } if (line.Contains("PREF") || line.Contains("pref") || line.Contains("type=pref") || line.Contains("type=PREF")) { prefered = 1; emType.Preferred = prefered; //set Email Type = UI setting of VCF emType.Name = VCFemailType.Name; } var emailSplit = line.Split(':'); if (emailSplit.Count() > 0) { if (!string.IsNullOrEmpty(emailSplit[1])) { emType.AccountType = new AccountType { Name = AccountTypeDB.TYPE_VCF_STRING }; email.Value = emailSplit[1]; email.EmailType = emType; email.EmailTypeId = emType.Id; contactVCF.Emails.Add(email); } } } else if (line.Contains("TEL") && (line.Contains("FAX:") || line.Contains("type=FAX"))) { Fax fax = new Fax(); FaxType fxType = new FaxType(); if (line.Contains("HOME;") || line.Contains("type=HOME")) { fxType.Name = FaxTypeDB.VCF_FAX_TYPE_HOME_STRING; } else if (line.Contains("WORK;") || line.Contains("type=WORK")) { fxType.Name = FaxTypeDB.VCF_FAX_TYPE_WORK_STRING; } else { // Others fxType.Name = FaxTypeDB.VCF_FAX_TYPE_OTHER_STRING; } var tmp = line.Split(':'); if (tmp.Count() > 0) { fxType.AccountType = new AccountType { Name = AccountTypeDB.TYPE_VCF_STRING }; fax.Value = tmp[1]; fax.FaxType = fxType; contactVCF.Faxes.Add(fax); } } }
public FileImportResult ImportFromCSV(string data, DuplicateContactMode mode) { FileImportResult result = new FileImportResult(); //get db EmailType emailType = dbManager.GetPreferredEmailTypeByAccountTypeNameEF(AccountTypeDB.TYPE_CSV_STRING); FaxType faxType = dbManager.GetPreferredFaxTypeByAccountTypeNameEF(AccountTypeDB.TYPE_CSV_STRING); Account account = dbManager.GetCSVAccountEF(); string accTypeName = AccountTypeDB.TYPE_CSV_STRING; //split stream from file string[] contactSplits = data.Split(new string[] { "\r", "\r\n", "\n" }, StringSplitOptions.None); var CSVHeader = contactSplits[0].Split(new char[] { ',' }).ToList().Select(n => GroupDB.TrimSpace(GroupDB.RemoveControlChars(n)).ToLower()).ToList(); //define var int size = contactSplits.Count(); List<CMData.Contact> listContact = new List<CMData.Contact>(); //header index int IdFirstName = -1; int IdLastName = -1; int IdMidName = -1; int IdFullName = -1; int IdFax = -1; int IdEmail = -1; //check header //step 1: first last mid IdFirstName = CSVHeader.FindIndex(n => n.Equals("first name") || n.Equals("first") || n.Equals("first_name")); IdLastName = CSVHeader.FindIndex(n => n.Equals("last name") || n.Equals("last") || n.Equals("last_name")); IdMidName = CSVHeader.FindIndex(n => n.Equals("middle name") || n.Equals("middle") || n.Equals("middle_name")); //step 2: fullname if(IdFirstName < 0 && IdLastName < 0 && IdMidName < 0) { IdFullName = CSVHeader.FindIndex(n => n.Equals("full name") || n.Equals("name") || n.Equals("full_name") || n.Equals("fullname")); } //step3: Email & Fax if(IdFirstName >= 0 || IdLastName >= 0 || IdMidName >= 0 || IdFullName >= 0) { IdEmail = CSVHeader.FindIndex(n => n.Equals("email address") || n.Equals("email_address") || n.Equals("e-mail_address") || n.Equals("e-mail") || n.Equals("email") || n.Equals("home email") || n.Equals("home_email") || n.Equals("work email") || n.Equals("work_email") || n.Equals("personal email") || n.Equals("personal_email")); IdFax = CSVHeader.FindIndex(n => n.Equals("fax number") || n.Equals("fax_number") || n.Equals("business fax") || n.Equals("business_fax") || n.Equals("fax") || n.Equals("home fax") || n.Equals("home_fax") || n.Equals("work fax") || n.Equals("work_fax")); } else { result.errorCode = FileImExportController.CODE_FILE_STRUCTURE_NOT_SUITABLE; return result; } //parse data fore each row // i=0 is headers row for (int i = 1; i < size; i++) { string bareData = contactSplits[i]; var valueSplits = splitCSVLine(bareData); var maxColumnIndex = valueSplits.Count() - 1; if (valueSplits.Count() < 1) continue; var valueFullName = String.Empty; var valueLastName = String.Empty; var valueFirstName = String.Empty; var valueMidName = String.Empty; var valueEmail = String.Empty; var valueFax = String.Empty; //parse //get full name or detail FLM name if (IdFullName >= 0) { if(IdFullName > maxColumnIndex) { continue; } valueFullName = valueSplits.ElementAt(IdFullName); if (string.IsNullOrEmpty(valueFullName)) //specify full name, but it's empty continue; var cv = new ContactView(); cv.Name = valueFullName; cv.SplitFirstLastMidFromFullName(ContactDB.NAME_ORDER_FIRST_LAST_MID); valueFirstName = cv.FirstName; valueLastName = cv.LastName; valueMidName = cv.MiddleName; } else { if(IdFirstName >= 0 && IdFirstName <= maxColumnIndex) valueFirstName = valueSplits.ElementAt(IdFirstName); if (IdLastName >= 0 && IdLastName <= maxColumnIndex) valueLastName = valueSplits.ElementAt(IdLastName); if (IdMidName >= 0 && IdMidName <= maxColumnIndex) valueMidName = valueSplits.ElementAt(IdMidName); } //get Fax, Email if (IdEmail >= 0 && IdEmail <= maxColumnIndex) valueEmail = valueSplits.ElementAt(IdEmail); if (IdFax >= 0 && IdFax <= maxColumnIndex) valueFax = valueSplits.ElementAt(IdFax); //check to skip row when specify FLM name, but they're empty if (String.IsNullOrEmpty(valueFirstName) && String.IsNullOrEmpty(valueLastName) && String.IsNullOrEmpty(valueMidName)) { continue; } //Mapping Email / Fax List<Email> listEmail = new List<Email>(); if (!String.IsNullOrEmpty(valueEmail)) { Email em = new Email(); em.Value = valueEmail; em.EmailType = emailType; em.EmailTypeId = emailType.Id; listEmail.Add(em); } List<Fax> listFax = new List<Fax>(); if (!String.IsNullOrEmpty(valueFax)) { Fax faxDB = new Fax(); faxDB.Value = valueFax; faxDB.FaxType = faxType; faxDB.FaxTypeId = faxType.Id; listFax.Add(faxDB); } //Create Contact obj CMData.Contact contact; contact = new CMData.Contact(); contact.AccountId = account.Id; contact.FirstName = valueFirstName; contact.LastName = valueLastName; contact.MiddleName = valueMidName; contact.Emails = listEmail; contact.Faxes = listFax; listContact.Add(contact); } //write to DB result = AddOrUpdateContact(listContact, accTypeName, mode); return result; }
private async void AddOrUpdateContactsToDatabase() { if (isUserCancel) { return; } AddOrUpdateAccountWhenImportingFromAccount(); List<Contact> list = new List<Contact>(); foreach (GoogleContact gContact in listContact) { Contact contact = new Contact(); contact.AccountId = account.Id; //contact.AccountTypeName = AccountTypeDB.TYPE_GMAIL_STRING; //contact.Id = -1; contact.FirstName = gContact.GivenName != null ? gContact.GivenName : string.Empty; contact.MiddleName = gContact.MiddleName != null ? gContact.MiddleName : string.Empty; contact.LastName = gContact.FamilyName != null ? gContact.FamilyName : string.Empty; // add list email List<Email> listEmail = new List<Email>(); var hasHomeEmail = false; var hasWorkEmail = false; var hasOtherEmail = false; foreach (TypeAndValue email in gContact.Emails) { Email emailDB = new Email(); emailDB.Value = email.Value; EmailType emailType = new EmailType(); string type = email.Type; if (String.Compare(type, "home") == 0) { if (!hasHomeEmail) { hasHomeEmail = true; emailType.Name = EmailTypeDB.GMAIL_EMAIL_TYPE_HOME_STRING; } } else if (String.Compare(type, "work") == 0) { if (!hasWorkEmail) { hasWorkEmail = true; emailType.Name = EmailTypeDB.GMAIL_EMAIL_TYPE_WORK_STRING; } } else { if (!hasOtherEmail) { hasOtherEmail = true; emailType.Name = EmailTypeDB.GMAIL_EMAIL_TYPE_OTHER_STRING; } } if (emailType.Name != null) { emailDB.EmailType = emailType; emailType.AccountType = new AccountType { Name = AccountTypeDB.TYPE_GMAIL_STRING }; listEmail.Add(emailDB); } } contact.Emails = listEmail; // add list fax List<Fax> listFax = new List<Fax>(); var hasHomeFax = false; var hasWorkFax = false; foreach (TypeAndValue phone in gContact.Phones) { string type = phone.Type; if (type.EndsWith("fax")) { Fax fax = new Fax(); fax.Value = phone.Value; FaxType faxType = new FaxType(); if (String.Compare(type, "home_fax") == 0) { if (!hasHomeFax) { hasHomeFax = true; faxType.Name = FaxTypeDB.GMAIL_FAX_TYPE_HOME_STRING; } } else if (String.Compare(type, "work_fax") == 0) { if (!hasWorkFax) { hasWorkFax = true; faxType.Name = FaxTypeDB.GMAIL_FAX_TYPE_WORK_STRING; } } if (faxType.Name != null) { faxType.AccountType = new AccountType { Name = AccountTypeDB.TYPE_GMAIL_STRING }; //fax.AccountTypeName = AccountTypeDB.TYPE_GMAIL_STRING; fax.FaxType = faxType; listFax.Add(fax); } } } contact.Faxes = listFax; list.Add(contact); } await Task.Factory.StartNew(() => { CheckMaximumAndImport(list); }); }