public FileImportResult ImportFromFile(string path) { string lowerPath = path.ToLower(); if (lowerPath.EndsWith(".csv")) // .csv file { return ImportFromCSVFile(path); } else if (lowerPath.EndsWith(".vcf")) // .vcf file { return ImportFromVCFFile(path); } FileImportResult result = new FileImportResult(); result.errorCode = CODE_FILE_CAN_NOT_IMPORT; return result; }
public FileImportResult ImportFromCSVFile(string path) { FileImportResult result = new FileImportResult(); // the structure is name,email,fax if (File.Exists(path)) { string data = ""; try { data = File.ReadAllText(path); } catch (Exception e) { result.errorCode = CODE_FILE_IN_USE; return result; } string[] contactSpits = data.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); int size = contactSpits.Count(); EmailTypeDB emailType = dbManager.GetPreferredEmailTypeByAccountTypeName(AccountTypeDB.TYPE_CSV_STRING); FaxTypeDB faxType = dbManager.GetPreferredFaxTypeByAccountTypeName(AccountTypeDB.TYPE_CSV_STRING); AccountDB account = dbManager.GetCSVAccount(); string accTypeName = AccountTypeDB.TYPE_CSV_STRING; List<ContactDB> listContact = new List<ContactDB>(); ContactDB contact; for (int i = 1; i < size; i++) // begin with 1 because first line is header { string bareData = contactSpits[i]; string[] valueSplits = bareData.Split(','); if (valueSplits.Count() == 3) { string name = valueSplits[0]; name = name.Replace(COMMA_REPLACE, ","); string email = valueSplits[1]; email = email.Replace(COMMA_REPLACE, ","); string fax = valueSplits[2]; fax = fax.Replace(COMMA_REPLACE, ","); if (String.IsNullOrEmpty(name) && String.IsNullOrEmpty(email) && String.IsNullOrEmpty(fax)) { continue; } List<EmailDB> listEmail = new List<EmailDB>(); if (!String.IsNullOrEmpty(email)) { EmailDB emailDB = new EmailDB(); emailType.AccountTypeName = AccountTypeDB.TYPE_CSV_STRING; emailDB.AccountTypeName = AccountTypeDB.TYPE_CSV_STRING; emailDB.Value = email; emailDB.EmailType = emailType; listEmail.Add(emailDB); } List<FaxDB> listFax = new List<FaxDB>(); if (!String.IsNullOrEmpty(fax)) { FaxDB faxDB = new FaxDB(); faxType.AccountTypeName = AccountTypeDB.TYPE_CSV_STRING; faxDB.AccountTypeName = AccountTypeDB.TYPE_CSV_STRING; faxDB.Value = fax; faxDB.FaxType = faxType; listFax.Add(faxDB); } contact = new ContactDB(); contact.AccountId = account.Id; contact.AccountTypeName = accTypeName; contact.Id = -1; contact.FirstName = name; contact.ListEmail = listEmail; contact.ListFax = listFax; listContact.Add(contact); } } return AddOrUpdateContact(listContact); } result.errorCode = CODE_FILE_CAN_NOT_IMPORT; return result; }
private FileImportResult AddOrUpdateContact(List<ContactDB> listContact) { FileImportResult result = new FileImportResult(); int totalContact = dbManager.GetTotalContactCount(); if (listContact.Count() + totalContact > ContactDB.MAX_CONTACT) { result.errorCode = CODE_FILE_EXCEED_MAXIMUM; return result; } int applied = dbManager.AddOrUpdateContacts(listContact); result.downloaded = listContact.Count(); result.applied = applied; return result; }
public FileImportResult ImportFromVCFFile(string path) { FileImportResult result = new FileImportResult(); try { File.ReadAllText(path); } catch (Exception e) { result.errorCode = CODE_FILE_IN_USE; return result; } vCard card = null; try { card = new vCard(path); } catch (Exception ex) { result.errorCode = CODE_FILE_STRUCTURE_NOT_SUITABLE; return result; } List<EmailDB> listEmail = new List<EmailDB>(); List<FaxDB> listFax = new List<FaxDB>(); EmailTypeDB preferEmailTypeFile = dbManager.GetPreferEmailFromCSVSetting(); // get email vCardEmailAddressCollection listVEmail = card.EmailAddresses; if (listVEmail != null && listVEmail.Count() > 0) { EmailDB preferEmailDB = new EmailDB(); EmailTypeDB preferEmailType = new EmailTypeDB(); //process Email1 from VCard foreach (vCardEmailAddress vEmail in listVEmail) { if (vEmail.IsPreferred == true && preferEmailTypeFile.Name.Equals("Home")) { preferEmailType.Name = EmailTypeDB.VCF_EMAIL_TYPE_HOME_STRING; preferEmailDB.Value = vEmail.Address; preferEmailType.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; preferEmailDB.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; preferEmailDB.EmailType = preferEmailType; listEmail.Add(preferEmailDB); listVEmail.Remove(vEmail); break; } else if (vEmail.IsPreferred == true && preferEmailTypeFile.Name.Equals("Work")) { preferEmailType.Name = EmailTypeDB.VCF_EMAIL_TYPE_WORK_STRING; preferEmailDB.Value = vEmail.Address; preferEmailType.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; preferEmailDB.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; preferEmailDB.EmailType = preferEmailType; listEmail.Add(preferEmailDB); listVEmail.Remove(vEmail); break; } else if (vEmail.IsPreferred == true && preferEmailTypeFile.Name.Equals("Other")) { preferEmailType.Name = EmailTypeDB.VCF_EMAIL_TYPE_OTHER_STRING; preferEmailDB.Value = vEmail.Address; preferEmailType.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; preferEmailDB.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; preferEmailDB.EmailType = preferEmailType; listEmail.Add(preferEmailDB); listVEmail.Remove(vEmail); break; } } if (listVEmail.Count() == 1) { //process Email2 from VCard EmailDB email2DB = new EmailDB(); email2DB.Value = listVEmail[0].Address; email2DB.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; email2DB.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; EmailTypeDB email2Type = new EmailTypeDB(); switch (preferEmailTypeFile.Name) { case "Home": email2Type.Name = EmailTypeDB.VCF_EMAIL_TYPE_WORK_STRING; break; case "Work": email2Type.Name = EmailTypeDB.VCF_EMAIL_TYPE_HOME_STRING; break; case "Other": email2Type.Name = EmailTypeDB.VCF_EMAIL_TYPE_HOME_STRING; break; } email2DB.EmailType = email2Type; listEmail.Add(email2DB); } else if (listVEmail.Count() == 2) { //process Email2 from VCard EmailDB email2DB = new EmailDB(); email2DB.Value = listVEmail[0].Address; email2DB.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; email2DB.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; //process Email3 from VCard EmailDB email3DB = new EmailDB(); email3DB.Value = listVEmail[1].Address; email3DB.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; email3DB.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; EmailTypeDB email2Type = new EmailTypeDB(); EmailTypeDB email3Type = new EmailTypeDB(); switch (preferEmailTypeFile.Name) { case "Home": email2Type.Name = EmailTypeDB.VCF_EMAIL_TYPE_WORK_STRING; email3Type.Name = EmailTypeDB.VCF_EMAIL_TYPE_OTHER_STRING; break; case "Work": email2Type.Name = EmailTypeDB.VCF_EMAIL_TYPE_HOME_STRING; email3Type.Name = EmailTypeDB.VCF_EMAIL_TYPE_OTHER_STRING; break; case "Other": email2Type.Name = EmailTypeDB.VCF_EMAIL_TYPE_HOME_STRING; email3Type.Name = EmailTypeDB.VCF_EMAIL_TYPE_WORK_STRING; break; } email2DB.EmailType = email2Type; email3DB.EmailType = email3Type; listEmail.Add(email2DB); listEmail.Add(email3DB); } } // get fax vCardPhoneCollection listVPhone = card.Phones; if (listVPhone != null && listVPhone.Count() > 0) { foreach (vCardPhone vphone in listVPhone) { if (vphone.IsFax) { FaxDB faxDB = new FaxDB(); faxDB.Value = vphone.FullNumber; FaxTypeDB faxType = new FaxTypeDB(); if (vphone.IsHome) { faxType.Name = FaxTypeDB.VCF_FAX_TYPE_HOME_STRING; } else if (vphone.IsWork) { faxType.Name = FaxTypeDB.VCF_FAX_TYPE_WORK_STRING; } else { faxType.Name = FaxTypeDB.VCF_FAX_TYPE_OTHER_STRING; } faxType.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; faxDB.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; faxDB.FaxType = faxType; listFax.Add(faxDB); } } } AccountDB account = dbManager.GetVCFAccount(); string accTypeName = AccountTypeDB.TYPE_VCF_STRING; List<ContactDB> listContact = new List<ContactDB>(); ContactDB contact = new ContactDB(); contact.AccountId = account.Id; contact.AccountTypeName = accTypeName; contact.Id = -1; contact.FirstName = card.GivenName; contact.MiddleName = card.AdditionalNames; contact.LastName = card.FamilyName; contact.ListEmail = listEmail; contact.ListFax = listFax; listContact.Add(contact); return AddOrUpdateContact(listContact); }
public FileImportResult ImportFromVCFFile(string path) { FileImportResult result = new FileImportResult(); try { File.ReadAllText(path); } catch (Exception e) { result.errorCode = CODE_FILE_IN_USE; return result; } vCard card = null; try { card = new vCard(path); } catch (Exception ex) { result.errorCode = CODE_FILE_STRUCTURE_NOT_SUITABLE; return result; } List<EmailDB> listEmail = new List<EmailDB>(); List<FaxDB> listFax = new List<FaxDB>(); // get email vCardEmailAddressCollection listVEmail = card.EmailAddresses; if (listVEmail != null && listVEmail.Count() > 0) { foreach (vCardEmailAddress vEmail in listVEmail) { EmailDB emailDB = new EmailDB(); emailDB.Value = vEmail.Address; EmailTypeDB emailType = new EmailTypeDB(); if (vEmail.ItemType.ToString().Equals("HOME")) { emailType.Name = EmailTypeDB.VCF_EMAIL_TYPE_HOME_STRING; } else if (vEmail.ItemType.ToString().Equals("WORK")) { emailType.Name = EmailTypeDB.VCF_EMAIL_TYPE_WORK_STRING; } else { emailType.Name = EmailTypeDB.VCF_EMAIL_TYPE_OTHER_STRING; } emailType.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; emailDB.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; emailDB.EmailType = emailType; listEmail.Add(emailDB); } } // get fax vCardPhoneCollection listVPhone = card.Phones; if (listVPhone != null && listVPhone.Count() > 0) { foreach (vCardPhone vphone in listVPhone) { if (vphone.IsFax) { FaxDB faxDB = new FaxDB(); faxDB.Value = vphone.FullNumber; FaxTypeDB faxType = new FaxTypeDB(); if (vphone.IsHome) { faxType.Name = FaxTypeDB.VCF_FAX_TYPE_HOME_STRING; } else if (vphone.IsWork) { faxType.Name = FaxTypeDB.VCF_FAX_TYPE_WORK_STRING; } else { faxType.Name = FaxTypeDB.VCF_FAX_TYPE_OTHER_STRING; } faxType.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; faxDB.AccountTypeName = AccountTypeDB.TYPE_VCF_STRING; faxDB.FaxType = faxType; listFax.Add(faxDB); } } } AccountDB account = dbManager.GetVCFAccount(); string accTypeName = AccountTypeDB.TYPE_VCF_STRING; List<ContactDB> listContact = new List<ContactDB>(); ContactDB contact = new ContactDB(); contact.AccountId = account.Id; contact.AccountTypeName = accTypeName; contact.Id = -1; contact.FirstName = card.GivenName; contact.MiddleName = card.AdditionalNames; contact.LastName = card.FamilyName; contact.ListEmail = listEmail; contact.ListFax = listFax; listContact.Add(contact); return AddOrUpdateContact(listContact); }
private FileImportResult AddOrUpdateContact(List<CMData.Contact> listContact, string accTypeName, DuplicateContactMode mode) { FileImportResult result = new FileImportResult(); int moreContactCount = listContact.Count(); int currentContactNumber = dbManager.GetTotalContactCount(); int totalContact = currentContactNumber + moreContactCount; if (totalContact > ContactDB.MAX_CONTACT && totalContact <= ContactDB.MAX_LIMITATION) { result.errorCode = CODE_FILE_EXCEED_CURRENT_LIMITATION; result.downloaded = totalContact; return result; } if(totalContact > ContactDB.MAX_LIMITATION) { result.errorCode = CODE_FILE_EXCEED_MAXIMUM; return result; } int applied = dbManager.AddOrUpdateContactsFromAccount(listContact, accTypeName, mode); result.downloaded = listContact.Count(); result.applied = applied; return result; }
public FileImportResult ImportFromCSVFile(string path) { FileImportResult result = new FileImportResult(); string last_name = String.Empty; string first_name = String.Empty; string middle_name = String.Empty; string name = String.Empty; string email = String.Empty; string fax = String.Empty; try { if (File.Exists(path)) { string data = ""; try { data = File.ReadAllText(path); } catch (Exception e) { result.errorCode = CODE_FILE_IN_USE; return result; } string[] contactSpits = data.Split(new string[] { "\r", "\r\n", "\n" }, StringSplitOptions.None); int size = contactSpits.Count(); EmailTypeDB emailType = dbManager.GetPreferredEmailTypeByAccountTypeName(AccountTypeDB.TYPE_CSV_STRING); FaxTypeDB faxType = dbManager.GetPreferredFaxTypeByAccountTypeName(AccountTypeDB.TYPE_CSV_STRING); AccountDB account = dbManager.GetCSVAccount(); string accTypeName = AccountTypeDB.TYPE_CSV_STRING; List<ContactDB> listContact = new List<ContactDB>(); bool isGetFullName = true; ContactDB contact; string[] headers = contactSpits[0].Split(','); for (int i = size - 1; i >= 1; i--) { string bareData = contactSpits[i]; var valueSplits = splitCSVLine(bareData); if (valueSplits.Count() <= 1) { continue; } for (int j = valueSplits.Count() - 1; j >= 0; j--) { var value = MyUtils.RemoveSpaces(valueSplits.ElementAt(j)); switch (MyUtils.RemoveSpaces(headers[j].ToLower())) { // name case COLUMN_FIRST_1: first_name = value; isGetFullName = false; break; case COLUMN_FIRST_2: first_name = value; isGetFullName = false; break; case COLUMN_FIRST_3: first_name = value; isGetFullName = false; break; case COLUMN_LAST_1: last_name = value; isGetFullName = false; break; case COLUMN_LAST_2: last_name = value; isGetFullName = false; break; case COLUMN_LAST_3: last_name = value; isGetFullName = false; break; case COLUMN_MIDDLE_1: middle_name = value; isGetFullName = false; break; case COLUMN_MIDDLE_2: middle_name = value; isGetFullName = false; break; case COLUMN_MIDDLE_3: middle_name = value; isGetFullName = false; break; case COLUMN_NAME_1: name = value; break; case COLUMN_NAME_2: name = value; break; case COLUMN_NAME_3: name = value; break; case COLUMN_NAME_4: name = value; break; // email case COLUMN_EMAIL_1: email = value; break; case COLUMN_EMAIL_2: email = value; break; case COLUMN_EMAIL_3: email = value; break; case COLUMN_EMAIL_4: email = value; break; case COLUMN_EMAIL_5: email = value; break; case COLUMN_EMAIL_6: email = value; break; case COLUMN_EMAIL_7: email = value; break; case COLUMN_EMAIL_8: email = value; break; case COLUMN_EMAIL_9: email = value; break; case COLUMN_EMAIL_10: email = value; break; case COLUMN_EMAIL_11: email = value; break; case COLUMN_EMAIL_12: email = value; break; case COLUMN_EMAIL_13: email = value; break; case COLUMN_EMAIL_14: email = value; break; // fax case COLUMN_FAX_1: fax = value; break; case COLUMN_FAX_2: fax = value; break; case COLUMN_FAX_3: fax = value; break; case COLUMN_FAX_4: fax = value; break; case COLUMN_FAX_5: fax = value; break; case COLUMN_FAX_6: fax = value; break; case COLUMN_FAX_7: fax = value; break; case COLUMN_FAX_8: fax = value; break; case COLUMN_FAX_9: fax = value; break; case COLUMN_FAX_10: fax = value; break; case COLUMN_FAX_11: fax = value; break; default: break; } } if (isGetFullName) { if (String.IsNullOrEmpty(name)) { continue; } } else { if (String.IsNullOrEmpty(first_name) && String.IsNullOrEmpty(last_name) && String.IsNullOrEmpty(middle_name)) { continue; } } List<EmailDB> listEmail = new List<EmailDB>(); if (!String.IsNullOrEmpty(email)) { EmailDB emailDB = new EmailDB(); emailType.AccountTypeName = AccountTypeDB.TYPE_CSV_STRING; emailDB.AccountTypeName = AccountTypeDB.TYPE_CSV_STRING; emailDB.Value = email; emailDB.EmailType = emailType; listEmail.Add(emailDB); } List<FaxDB> listFax = new List<FaxDB>(); if (!String.IsNullOrEmpty(fax)) { FaxDB faxDB = new FaxDB(); faxType.AccountTypeName = AccountTypeDB.TYPE_CSV_STRING; faxDB.AccountTypeName = AccountTypeDB.TYPE_CSV_STRING; faxDB.Value = fax; faxDB.FaxType = faxType; listFax.Add(faxDB); } contact = new ContactDB(); contact.AccountId = account.Id; contact.AccountTypeName = accTypeName; contact.Id = -1; if (isGetFullName) { string nameOrder = dbManager.GetPreferredHostNameOrder("CSV"); string[] names = ContactDB.GetFirstMidLastFromFullName(name, nameOrder); first_name = names[0]; middle_name = names[1]; last_name = names[2]; } else { contact.FirstName = first_name; contact.LastName = last_name; contact.MiddleName = middle_name; } contact.FirstName = first_name; contact.LastName = last_name; contact.MiddleName = middle_name; contact.ListEmail = listEmail; contact.ListFax = listFax; listContact.Add(contact); } return AddOrUpdateContact(listContact); } } catch (Exception) { result.errorCode = CODE_FILE_CAN_NOT_IMPORT; return result; } result.errorCode = CODE_FILE_CAN_NOT_IMPORT; return result; }
/// <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 FileImportResult AddOrUpdateContact(List<ContactDB> listContact) { FileImportResult result = new FileImportResult(); int moreContactCount = listContact.Count(); int currentContactNumber = dbManager.GetTotalContactCount(); int totalContact = currentContactNumber + moreContactCount; if (totalContact > ContactDB.MAX_CONTACT && totalContact <= ContactDB.MAX_LIMITATION) { result.errorCode = CODE_FILE_EXCEED_CURRENT_LIMITATION; return result; } int applied = dbManager.AddOrUpdateContacts(listContact); result.downloaded = listContact.Count(); result.applied = applied; return result; }
public async System.Threading.Tasks.Task<FileImportResult> ImportFromVCFFile(StorageFile file, DuplicateContactMode mode) { FileImportResult result = new FileImportResult(); try { isVCFFormat = false; var randomAccessStream = await file.OpenReadAsync(); Stream stream = randomAccessStream.AsStreamForRead(); using (StreamReader reader = new StreamReader(stream)) { do { string line = reader.ReadLine(); // Do something with line GetContacts(line); if(!isVCFFormat) { result.errorCode = FileImExportController.CODE_FILE_STRUCTURE_NOT_SUITABLE; reader.Dispose(); contactVCF = null; return result; } } while (reader.Peek() != -1); reader.Dispose(); if(contactVCF != null) { contactVCF.Emails = formatEmailTypeForImportVCF(contactVCF.Emails); string accTypeName = AccountTypeD.TYPE_VCF_STRING; List<CMData.Contact> list = new List<CMData.Contact> { contactVCF }; result = AddOrUpdateContact(list, accTypeName, mode); contactVCF = null; } return result; } } catch (Exception e) { result.errorCode = CODE_FILE_IN_USE; return result; } }
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; }
public FileImportResult ImportFromCSVFile(string path) { FileImportResult result = new FileImportResult(); string last_name = String.Empty; string first_name = String.Empty; string middle_name = String.Empty; string email = String.Empty; string fax = String.Empty; // the structure is name,email,fax if (File.Exists(path)) { string data = ""; try { data = File.ReadAllText(path); } catch (Exception e) { result.errorCode = CODE_FILE_IN_USE; return result; } string[] contactSpits = data.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); int size = contactSpits.Count(); EmailTypeDB emailType = dbManager.GetPreferredEmailTypeByAccountTypeName(AccountTypeDB.TYPE_CSV_STRING); FaxTypeDB faxType = dbManager.GetPreferredFaxTypeByAccountTypeName(AccountTypeDB.TYPE_CSV_STRING); AccountDB account = dbManager.GetCSVAccount(); string accTypeName = AccountTypeDB.TYPE_CSV_STRING; List<ContactDB> listContact = new List<ContactDB>(); ContactDB contact; using (CsvReader csv = new CsvReader(new StreamReader(path), true)) { int fieldCount = csv.FieldCount; string[] headers = csv.GetFieldHeaders(); while (csv.ReadNextRecord()) { for (int i = 0; i < fieldCount; i++) { if (headers[i] == "first_name") { first_name = csv[i]; } if (headers[i] == "last_name") { last_name = csv[i]; } if (headers[i] == "middle_name") { middle_name = csv[i]; } if (headers[i] == "email") { email = csv[i]; } if (headers[i] == "Business Fax") { fax = csv[i]; } } if (String.IsNullOrEmpty(first_name) && String.IsNullOrEmpty(last_name) && String.IsNullOrEmpty(email) && String.IsNullOrEmpty(fax)) { continue; } List<EmailDB> listEmail = new List<EmailDB>(); if (!String.IsNullOrEmpty(email)) { EmailDB emailDB = new EmailDB(); emailType.AccountTypeName = AccountTypeDB.TYPE_CSV_STRING; emailDB.AccountTypeName = AccountTypeDB.TYPE_CSV_STRING; emailDB.Value = email; emailDB.EmailType = emailType; listEmail.Add(emailDB); } List<FaxDB> listFax = new List<FaxDB>(); if (!String.IsNullOrEmpty(fax)) { FaxDB faxDB = new FaxDB(); faxType.AccountTypeName = AccountTypeDB.TYPE_CSV_STRING; faxDB.AccountTypeName = AccountTypeDB.TYPE_CSV_STRING; faxDB.Value = fax; faxDB.FaxType = faxType; listFax.Add(faxDB); } contact = new ContactDB(); contact.AccountId = account.Id; contact.AccountTypeName = accTypeName; contact.Id = -1; contact.FirstName = first_name; contact.LastName = last_name; contact.MiddleName = middle_name; contact.ListEmail = listEmail; contact.ListFax = listFax; listContact.Add(contact); } } return AddOrUpdateContact(listContact); } result.errorCode = CODE_FILE_CAN_NOT_IMPORT; return result; }
private async Task<FileImportResult> DoImportFromCSV() { string contents = ""; FileImportResult res; try { using (IRandomAccessStream stream = await sf.OpenAsync(FileAccessMode.Read)) { using (DataReader textReader = new DataReader(stream)) { uint textLength = (uint)stream.Size; await textReader.LoadAsync(textLength); contents = textReader.ReadString(textLength); textReader.Dispose(); } stream.Dispose(); } res = fileController.ImportFromCSV(contents, contactMode); return res; } catch(Exception importException) { //Access Denied! System.Diagnostics.Debug.WriteLine(importException.Message); res = new FileImportResult(); res.errorCode = FileImExportController.CODE_FILE_IN_USE; return res; } }
private async Task ImportContactsFromFile() { if (sf != null) { var filePath = sf.Name.ToLower(); FileImportResult result = null; if (filePath.EndsWith(".csv")) { result = await Task.Run(() => DoImportFromCSV()); } else if (filePath.EndsWith(".vcf")) { result = await Task.Run(() => fileController.ImportFromVCFFile(sf, contactMode)); } else { result = new FileImportResult(); result.errorCode = FileImExportController.CODE_FILE_INVALID_EXTENSION; } if (result.errorCode == FileImExportController.CODE_FILE_NO_ERROR) { // Success var download = result.downloaded; var apply = result.applied; if (apply != 0) { var msgImport = UC_AddressBook.ResourcesStringLoader.GetString("import_num_contacts"); //Modals.UIConfirmPopup.ShowAsyncInfoPopup(string.Format(msgImport, apply, download)); Page_LocalContacts.listContact_EF = dbController.GetAllContactViews(); Page_LocalContacts.SucceededMessage = string.Format(msgImport, apply); } else { Page_LocalContacts.SucceededMessage = UC_AddressBook.ResourcesStringLoader.GetString("no_contact_import"); } //tblock_status.Text = string.Format("Import successfully {0}/{1} contacts. Please wait while page loading ...", apply, download); if (UC_AddressBook.Instance.AppFrame.CanGoBack) { Page_LocalContacts.IsReload = true; Page_LocalContacts.IsResumeSelectContacts = false; UC_AddressBook.Instance.Go_Back(); } } else { if (result.errorCode == FileImExportController.CODE_FILE_EXCEED_CURRENT_LIMITATION) { var msg = string.Format(FileImExportController.FromCodeToString(result.errorCode), result.downloaded); contactLimit = result.downloaded; btnConfirmOk.Visibility = Visibility.Visible; btnConfirmNo.Visibility = Visibility.Visible; NotifyUser(msg, NotifyType.ErrorMessage); } else { NotifyUser(string.Format(FileImExportController.FromCodeToString(result.errorCode) + "."), NotifyType.ErrorMessage); //Modals.UIConfirmPopup.ShowAsyncInfoPopup(string.Format("Import failed! " + FileImExportController.FromCodeToString(result.errorCode) + ".")); } } } else { //Modals.UIConfirmPopup.ShowAsyncInfoPopup("No file!"); if(label_file_path.Visibility == Visibility.Collapsed || label_file_path.Text=="") { btn_browse_Click(btn_browse, new RoutedEventArgs()); return; } else { NotifyUser("No file!", NotifyType.ErrorMessage); } } }