예제 #1
0
        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;
        }
예제 #2
0
        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;
        }
예제 #3
0
        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;
        }
예제 #4
0
        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);
        }