Exemple #1
0
        public FileUploadResult ProcessUpload(HttpContext context)
        {
            var fileUploadResult = new FileUploadResult();

            if (!FileToUpload.HasFilesToUpload(context))
            {
                return(fileUploadResult);
            }

            var file = new FileToUpload(context);

            String assignedPath;

            Global.GetStore().SaveTemp("temp", out assignedPath, file.InputStream);

            file.InputStream.Position = 0;

            var jObject = ImportFromCSV.GetInfo(file.InputStream, context.Request["importSettings"]);

            jObject.Add("assignedPath", assignedPath);

            fileUploadResult.Success = true;
            fileUploadResult.Data    = Global.EncodeTo64(jObject.ToString());

            return(fileUploadResult);
        }
        public FileUploadResult ProcessUpload(HttpContext context)
        {
            if (!WebItemSecurity.IsAvailableForUser(ProductEntryPoint.ID.ToString(), SecurityContext.CurrentAccount.ID))
                throw CRMSecurity.CreateSecurityException();

            var fileUploadResult = new FileUploadResult();

            if (!FileToUpload.HasFilesToUpload(context)) return fileUploadResult;

            var file = new FileToUpload(context);

            String assignedPath;

            Global.GetStore().SaveTemp("temp", out assignedPath, file.InputStream);

            file.InputStream.Position = 0;

            var jObject = ImportFromCSV.GetInfo(file.InputStream, context.Request["importSettings"]);

            jObject.Add("assignedPath", assignedPath);

            fileUploadResult.Success = true;
            fileUploadResult.Data = Global.EncodeTo64(jObject.ToString());

            return fileUploadResult;
        }
Exemple #3
0
        public void StartImport(EntityType entityType, String CSVFileURI, String importSettingsJSON)
        {
            ImportFromCSV.Start(entityType, CSVFileURI, importSettingsJSON);

            var action = GetMessageAction(entityType);

            MessageService.Send(HttpContext.Current.Request, action);
        }
Exemple #4
0
 public ImportFromCSVManager(Global global,
                             ImportFromCSV importFromCSV,
                             MessageService messageService)
 {
     _global         = global;
     _importFromCSV  = importFromCSV;
     _messageService = messageService;
 }
Exemple #5
0
 public ImportDataOperation(TenantUtil tenantUtil,
                            ImportFromCSV importFromCSV,
                            DisplayUserSettingsHelper displayUserSettingsHelper)
 {
     _tenantUtil                = tenantUtil;
     _importFromCSV             = importFromCSV;
     _displayUserSettingsHelper = displayUserSettingsHelper;
 }
Exemple #6
0
        public FileUploadResult ProcessUploadFake(string fileTemp, string importSettingsJSON)
        {
            var fileUploadResult = new FileUploadResult();

            if (String.IsNullOrEmpty(fileTemp) || String.IsNullOrEmpty(importSettingsJSON))
            {
                return(fileUploadResult);
            }

            if (!Global.GetStore().IsFile("temp", fileTemp))
            {
                return(fileUploadResult);
            }

            JObject jObject;

            //Read contents
            using (Stream storeStream = Global.GetStore().GetReadStream("temp", fileTemp))
            {
                using (var CSVFileStream = new MemoryStream())
                {
                    //Copy
                    var buffer = new byte[4096];
                    int readed;
                    while ((readed = storeStream.Read(buffer, 0, 4096)) != 0)
                    {
                        CSVFileStream.Write(buffer, 0, readed);
                    }
                    CSVFileStream.Position = 0;

                    jObject = ImportFromCSV.GetInfo(CSVFileStream, importSettingsJSON);
                }
            }

            jObject.Add("assignedPath", fileTemp);

            fileUploadResult.Success = true;
            fileUploadResult.Data    = Global.EncodeTo64(jObject.ToString());

            return(fileUploadResult);
        }
Exemple #7
0
        private void ImportContactsData()
        {
            var index = 0;

            var personFakeIdCompanyNameHash = new Dictionary <int, String>();

            var contactDao     = _daoFactory.GetContactDao();
            var contactInfoDao = _daoFactory.GetContactInfoDao();
            var customFieldDao = _daoFactory.GetCustomFieldDao();
            var tagDao         = _daoFactory.GetTagDao();

            var findedContacts     = new Dictionary <int, Contact>();
            var findedTags         = new Dictionary <int, List <String> >();
            var findedCustomField  = new List <CustomField>();
            var findedContactInfos = new List <ContactInfo>();

            #region Read csv
            using (var CSVFileStream = _dataStore.GetReadStream("temp", _CSVFileURI))
                using (CsvReader csv = ImportFromCSV.CreateCsvReaderInstance(CSVFileStream, _importSettings))
                {
                    int currentIndex = 0;

                    while (csv.ReadNextRecord())
                    {
                        _columns = csv.GetCurrentRowFields(false);
                        Contact contact = null;

                        #region Common data

                        if (!_CommonData(currentIndex, ref contact, ref personFakeIdCompanyNameHash))
                        {
                            continue;
                        }

                        findedContacts.Add(contact.ID, contact);

                        #endregion

                        #region Read tags

                        _ReadTags(ref findedTags, contact);

                        #endregion

                        #region Custom fields Y contact infos

                        var primaryFields = new List <int>();

                        foreach (JProperty jToken in _importSettings.ColumnMapping.Children())
                        {
                            var propertyValue = GetPropertyValue(jToken.Name);
                            if (String.IsNullOrEmpty(propertyValue))
                            {
                                continue;
                            }

                            if (jToken.Name.StartsWith("customField_"))
                            {
                                _ReadCustomField(jToken, propertyValue, contact, ref findedCustomField, customFieldDao);
                            }
                            else if (jToken.Name.StartsWith("contactInfo_"))
                            {
                                var addressTemplate = new JObject();
                                foreach (String addressPartName in Enum.GetNames(typeof(AddressPart)))
                                {
                                    addressTemplate.Add(addressPartName.ToLower(), "");
                                }

                                var addressTemplateStr = addressTemplate.ToString();

                                _ReadContactInfo(jToken, propertyValue, contact, ref findedContactInfos, ref primaryFields, addressTemplateStr);
                            }
                        }
                        #endregion

                        if (currentIndex + 1 > ImportFromCSV.MaxRoxCount)
                        {
                            break;
                        }
                        currentIndex++;
                    }
                }
            _log.InfoFormat("ImportContactsData. Reading {0} findedContacts complete", findedContacts.Count);

            #endregion

            Percentage = 37.5;

            #region Check Cancel flag | Insert Operation InCache
            if (ImportDataCache.CheckCancelFlag(EntityType.Contact))
            {
                ImportDataCache.ResetAll(EntityType.Contact);

                throw new OperationCanceledException();
            }

            ImportDataCache.Insert(EntityType.Contact, (ImportDataOperation)Clone());
            #endregion

            #region Processing duplicate rule

            _DuplicateRecordRuleProcess(ref findedContacts, ref personFakeIdCompanyNameHash, ref findedContactInfos, ref findedCustomField, ref findedTags);

            _log.Info("ImportContactsData. _DuplicateRecordRuleProcess. End");

            if (IsCompleted)
            {
                return;
            }

            #endregion

            Percentage += 12.5;

            #region Check Cancel flag | Insert Operation InCache
            if (ImportDataCache.CheckCancelFlag(EntityType.Contact))
            {
                ImportDataCache.ResetAll(EntityType.Contact);

                throw new OperationCanceledException();
            }

            ImportDataCache.Insert(EntityType.Contact, (ImportDataOperation)Clone());
            #endregion

            #region Manipulation for saving Companies for persons + CRMSecurity

            var findedCompanies = findedContacts.Where(x => x.Value is Company).ToDictionary(x => x.Key, y => y.Value);
            var findedPeoples   = findedContacts.Where(x => x.Value is Person).ToDictionary(x => x.Key, y => y.Value);

            var fakeRealContactIdHash = new Dictionary <int, int>();
            var companyNameRealIdHash = new Dictionary <String, int>();


            var findedCompaniesList = findedCompanies.Values.ToList();
            if (findedCompaniesList.Count != 0)
            {
                index = 0;
                while (index < findedCompaniesList.Count)
                {
                    var portion = findedCompaniesList.Skip(index).Take(DaoIterationStep).ToList();// Get next step

                    fakeRealContactIdHash = fakeRealContactIdHash.Union(
                        contactDao.SaveContactList(portion))
                                            .ToDictionary(item => item.Key, item => item.Value);


                    #region CRMSecurity set -by every item-

                    portion.ForEach(ct => CRMSecurity.SetAccessTo(ct, _importSettings.ContactManagers));

                    #endregion


                    index += DaoIterationStep;
                    if (index > findedCompaniesList.Count)
                    {
                        index = findedCompaniesList.Count;
                    }
                }
            }


            foreach (Company item in findedCompanies.Values)
            {
                if (companyNameRealIdHash.ContainsKey(item.CompanyName))
                {
                    continue;
                }

                companyNameRealIdHash.Add(item.CompanyName, item.ID);
            }

            foreach (var item in personFakeIdCompanyNameHash)
            {
                var person = (Person)findedPeoples[item.Key];

                if (companyNameRealIdHash.ContainsKey(item.Value))
                {
                    person.CompanyID = companyNameRealIdHash[item.Value];
                }
                else
                {
                    var findedCompany = contactDao.GetContactsByName(item.Value, true).FirstOrDefault();

                    // Why ???
                    if (findedCompany == null)
                    {
                        #region create COMPANY for person in csv

                        findedCompany = new Company
                        {
                            CompanyName = item.Value,
                            ShareType   = _importSettings.ShareType
                        };
                        findedCompany.ID = contactDao.SaveContact(findedCompany);

                        person.CompanyID = findedCompany.ID;
                        CRMSecurity.SetAccessTo(findedCompany, _importSettings.ContactManagers);

                        if (_importSettings.Tags.Count != 0)
                        {
                            tagDao.SetTagToEntity(EntityType.Contact, person.CompanyID, _importSettings.Tags.ToArray());
                        }

                        #endregion
                    }
                    else
                    {
                        person.CompanyID = findedCompany.ID;
                    }

                    companyNameRealIdHash.Add(item.Value, person.CompanyID);
                }
            }
            #endregion

            #region Saving People common data -by portions- + CRMSecurity

            var findedPeopleList = findedPeoples.Values.ToList();
            if (findedPeopleList.Count != 0)
            {
                index = 0;
                while (index < findedPeopleList.Count)
                {
                    var portion = findedPeopleList.Skip(index).Take(DaoIterationStep).ToList();// Get next step

                    fakeRealContactIdHash = fakeRealContactIdHash.Union(
                        contactDao.SaveContactList(portion))
                                            .ToDictionary(item => item.Key, item => item.Value);

                    #region CRMSecurity set -by every item-

                    portion.ForEach(ct => CRMSecurity.SetAccessTo(ct, _importSettings.ContactManagers));

                    #endregion


                    index += DaoIterationStep;
                    if (index > findedPeopleList.Count)
                    {
                        index = findedPeopleList.Count;
                    }
                }
            }
            _log.Info("ImportContactsData. Contacts common data saved");
            #endregion

            Percentage += 12.5;

            #region Check Cancel flag | Insert Operation InCache
            if (ImportDataCache.CheckCancelFlag(EntityType.Contact))
            {
                ImportDataCache.ResetAll(EntityType.Contact);

                throw new OperationCanceledException();
            }

            ImportDataCache.Insert(EntityType.Contact, (ImportDataOperation)Clone());
            #endregion

            #region Save contact infos -by portions-

            if (findedContactInfos.Count != 0)
            {
                findedContactInfos.ForEach(item => item.ContactID = fakeRealContactIdHash[item.ContactID]);

                index = 0;
                while (index < findedContactInfos.Count)
                {
                    var portion = findedContactInfos.Skip(index).Take(DaoIterationStep).ToList();// Get next step

                    contactInfoDao.SaveList(portion);

                    index += DaoIterationStep;
                    if (index > findedContactInfos.Count)
                    {
                        index = findedContactInfos.Count;
                    }
                }
            }
            _log.Info("ImportContactsData. Contacts infos saved");
            #endregion

            Percentage += 12.5;

            #region Check Cancel flag | Insert Operation InCache
            if (ImportDataCache.CheckCancelFlag(EntityType.Contact))
            {
                ImportDataCache.ResetAll(EntityType.Contact);

                throw new OperationCanceledException();
            }

            ImportDataCache.Insert(EntityType.Contact, (ImportDataOperation)Clone());
            #endregion

            #region Save custom fields -by portions-

            if (findedCustomField.Count != 0)
            {
                findedCustomField.ForEach(item => item.EntityID = fakeRealContactIdHash[item.EntityID]);

                index = 0;
                while (index < findedCustomField.Count)
                {
                    var portion = findedCustomField.Skip(index).Take(DaoIterationStep).ToList();// Get next step

                    customFieldDao.SaveList(portion);

                    index += DaoIterationStep;
                    if (index > findedCustomField.Count)
                    {
                        index = findedCustomField.Count;
                    }
                }
            }
            _log.Info("ImportContactsData. Custom fields saved");
            #endregion

            Percentage += 12.5;

            #region Check Cancel flag | Insert Operation InCache
            if (ImportDataCache.CheckCancelFlag(EntityType.Contact))
            {
                ImportDataCache.ResetAll(EntityType.Contact);

                throw new OperationCanceledException();
            }

            ImportDataCache.Insert(EntityType.Contact, (ImportDataOperation)Clone());
            #endregion

            #region Save tags

            var findedTagsValues = new List <string>();

            findedTags.Values.ToList().ForEach(t => { findedTagsValues.AddRange(t); });
            findedTagsValues = findedTagsValues.Distinct().ToList();

            var allTagsForImport = tagDao.GetAndAddTags(EntityType.Contact, findedTagsValues.Distinct().ToArray());

            foreach (var findedTagKey in findedTags.Keys)
            {
                var curTagNames = findedTags[findedTagKey];
                var curTagIds   = curTagNames.ConvertAll(n => allTagsForImport.ContainsKey(n) ? allTagsForImport[n] : 0).Where(id => id != 0).ToArray();

                tagDao.AddTagToEntity(EntityType.Contact, fakeRealContactIdHash[findedTagKey], curTagIds);
            }
            _log.Info("ImportContactsData. Tags saved");
            #endregion

            Percentage += 12.5;

            #region Check Cancel flag | Insert Operation InCache
            if (ImportDataCache.CheckCancelFlag(EntityType.Contact))
            {
                ImportDataCache.ResetAll(EntityType.Contact);

                throw new OperationCanceledException();
            }

            ImportDataCache.Insert(EntityType.Contact, (ImportDataOperation)Clone());
            #endregion

            Complete();
        }
Exemple #8
0
        private void ImportTaskData(DaoFactory _daoFactory)
        {
            using (var CSVFileStream = _dataStore.GetReadStream("temp", _CSVFileURI))
                using (CsvReader csv = ImportFromCSV.CreateCsvReaderInstance(CSVFileStream, _importSettings))
                {
                    int currentIndex = 0;

                    var contactDao  = _daoFactory.ContactDao;
                    var listItemDao = _daoFactory.ListItemDao;
                    var taskDao     = _daoFactory.TaskDao;

                    var findedTasks    = new List <Task>();
                    var taskCategories = listItemDao.GetItems(ListType.TaskCategory);

                    var allUsers = ASC.Core.CoreContext.UserManager.GetUsers(EmployeeStatus.All).ToList();

                    while (csv.ReadNextRecord())
                    {
                        _columns = csv.GetCurrentRowFields(false);

                        var obj = new Task();

                        obj.ID = currentIndex;

                        obj.Title = GetPropertyValue("title");

                        if (String.IsNullOrEmpty(obj.Title))
                        {
                            continue;
                        }

                        obj.Description = GetPropertyValue("description");

                        DateTime deadline;

                        if (DateTime.TryParse(GetPropertyValue("due_date"), out deadline))
                        {
                            obj.DeadLine = deadline;
                        }
                        else
                        {
                            obj.DeadLine = TenantUtil.DateTimeNow();
                        }

                        var csvResponsibleValue = GetPropertyValue("responsible");
                        var responsible         = allUsers.Where(n => n.DisplayUserName().Equals(csvResponsibleValue)).FirstOrDefault();

                        if (responsible != null)
                        {
                            obj.ResponsibleID = responsible.ID;
                        }
                        else
                        {
                            obj.ResponsibleID = Constants.LostUser.ID;
                        }

                        var categoryTitle = GetPropertyValue("taskCategory");

                        if (!String.IsNullOrEmpty(categoryTitle))
                        {
                            var findedCategory = taskCategories.Find(item => String.Compare(item.Title, categoryTitle) == 0);

                            if (findedCategory == null)
                            {
                                obj.CategoryID = taskCategories[0].ID;
                            }
                            else
                            {
                                obj.CategoryID = findedCategory.ID;
                            }
                        }
                        else
                        {
                            obj.CategoryID = taskCategories[0].ID;
                        }

                        var contactName = GetPropertyValue("contact");

                        if (!String.IsNullOrEmpty(contactName))
                        {
                            var contacts = contactDao.GetContactsByName(contactName, true);

                            if (contacts.Count > 0)
                            {
                                obj.ContactID = contacts[0].ID;
                            }
                            else
                            {
                                contacts = contactDao.GetContactsByName(contactName, false);
                                if (contacts.Count > 0)
                                {
                                    obj.ContactID = contacts[0].ID;
                                }
                            }
                        }

                        obj.IsClosed = false;

                        var taskStatus = GetPropertyValue("status");

                        if (!String.IsNullOrEmpty(taskStatus))
                        {
                            if (String.Compare(taskStatus, CRMTaskResource.TaskStatus_Closed, true) == 0)
                            {
                                obj.IsClosed = true;
                            }
                        }

                        var alertValue  = GetPropertyValue("alertValue");
                        int alertIntVal = 0;

                        if (Int32.TryParse(alertValue, out alertIntVal))
                        {
                            obj.AlertValue = alertIntVal;
                        }
                        else
                        {
                            obj.AlertValue = 0;
                        }


                        findedTasks.Add(obj);

                        if ((currentIndex + 1) > ImportFromCSV.MaxRoxCount)
                        {
                            break;
                        }

                        currentIndex++;
                    }

                    Percentage = 50;

                    taskDao.SaveTaskList(findedTasks);

                    Percentage += 12.5;

                    Complete();
                }
        }
        private void ImportOpportunityData(DaoFactory _daoFactory)
        {
            var allUsers = ASC.Core.CoreContext.UserManager.GetUsers(EmployeeStatus.All).ToList();

            using (var CSVFileStream = _dataStore.GetReadStream("temp", _CSVFileURI))
                using (CsvReader csv = ImportFromCSV.CreateCsvReaderInstance(CSVFileStream, _importSettings))
                {
                    int currentIndex = 0;

                    var customFieldDao   = _daoFactory.CustomFieldDao;
                    var contactDao       = _daoFactory.ContactDao;
                    var tagDao           = _daoFactory.TagDao;
                    var dealDao          = _daoFactory.DealDao;
                    var dealMilestoneDao = _daoFactory.DealMilestoneDao;

                    var findedTags        = new Dictionary <int, List <String> >();
                    var findedCustomField = new List <CustomField>();
                    var findedDeals       = new List <Deal>();
                    var findedDealMembers = new Dictionary <int, List <int> >();

                    var dealMilestones = dealMilestoneDao.GetAll();

                    while (csv.ReadNextRecord())
                    {
                        _columns = csv.GetCurrentRowFields(false);

                        var obj = new Deal();

                        obj.ID = currentIndex;

                        obj.Title = GetPropertyValue("title");

                        if (String.IsNullOrEmpty(obj.Title))
                        {
                            continue;
                        }

                        obj.Description = GetPropertyValue("description");

                        var csvResponsibleValue = GetPropertyValue("responsible");
                        var responsible         = allUsers.Where(n => n.DisplayUserName().Equals(csvResponsibleValue)).FirstOrDefault();

                        if (responsible != null)
                        {
                            obj.ResponsibleID = responsible.ID;
                        }
                        else
                        {
                            obj.ResponsibleID = Constants.LostUser.ID;
                        }

                        DateTime actualCloseDate;

                        DateTime expectedCloseDate;

                        if (DateTime.TryParse(GetPropertyValue("actual_close_date"), out actualCloseDate))
                        {
                            obj.ActualCloseDate = actualCloseDate;
                        }

                        if (DateTime.TryParse(GetPropertyValue("expected_close_date"), out expectedCloseDate))
                        {
                            obj.ExpectedCloseDate = expectedCloseDate;
                        }

                        var currency = CurrencyProvider.Get(GetPropertyValue("bid_currency"));

                        if (currency != null)
                        {
                            obj.BidCurrency = currency.Abbreviation;
                        }
                        else
                        {
                            obj.BidCurrency = Global.TenantSettings.DefaultCurrency.Abbreviation;
                        }

                        decimal bidValue;

                        var bidValueStr = GetPropertyValue("bid_amount");

                        if (Decimal.TryParse(bidValueStr, NumberStyles.Number, CultureInfo.InvariantCulture, out bidValue))
                        {
                            obj.BidValue = bidValue;
                        }
                        else
                        {
                            obj.BidValue = 0;
                        }


                        var bidTypeStr = GetPropertyValue("bid_type");

                        BidType bidType = BidType.FixedBid;

                        if (!String.IsNullOrEmpty(bidTypeStr))
                        {
                            if (String.Compare(CRMDealResource.BidType_FixedBid, bidTypeStr, true) == 0)
                            {
                                bidType = BidType.FixedBid;
                            }
                            else if (String.Compare(CRMDealResource.BidType_PerDay, bidTypeStr, true) == 0)
                            {
                                bidType = BidType.PerDay;
                            }
                            else if (String.Compare(CRMDealResource.BidType_PerHour, bidTypeStr, true) == 0)
                            {
                                bidType = BidType.PerHour;
                            }
                            else if (String.Compare(CRMDealResource.BidType_PerMonth, bidTypeStr, true) == 0)
                            {
                                bidType = BidType.PerMonth;
                            }
                            else if (String.Compare(CRMDealResource.BidType_PerWeek, bidTypeStr, true) == 0)
                            {
                                bidType = BidType.PerWeek;
                            }
                            else if (String.Compare(CRMDealResource.BidType_PerYear, bidTypeStr, true) == 0)
                            {
                                bidType = BidType.PerYear;
                            }
                        }

                        obj.BidType = bidType;

                        if (obj.BidType != BidType.FixedBid)
                        {
                            int perPeriodValue;

                            if (int.TryParse(GetPropertyValue("per_period_value"), out perPeriodValue))
                            {
                                obj.PerPeriodValue = perPeriodValue;
                            }
                        }

                        int probabilityOfWinning;

                        if (int.TryParse(GetPropertyValue("probability_of_winning"), out probabilityOfWinning))
                        {
                            obj.DealMilestoneProbability = probabilityOfWinning;
                        }

                        var dealMilestoneTitle = GetPropertyValue("deal_milestone");

                        var tag = GetPropertyValue("tag");


                        if (!String.IsNullOrEmpty(tag))
                        {
                            var tagList = tag.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                            tagList.AddRange(_importSettings.Tags);
                            tagList = tagList.Distinct().ToList();
                            findedTags.Add(obj.ID, tagList);
                        }
                        else if (_importSettings.Tags.Count != 0)
                        {
                            findedTags.Add(obj.ID, _importSettings.Tags);
                        }


                        if (String.IsNullOrEmpty(dealMilestoneTitle))
                        {
                            obj.DealMilestoneID = dealMilestones[0].ID;
                        }
                        else
                        {
                            var dealMilestone = dealMilestones.Find(item => String.Compare(item.Title, dealMilestoneTitle, true) == 0);

                            if (dealMilestone == null)
                            {
                                obj.DealMilestoneID = dealMilestones[0].ID;
                            }
                            else
                            {
                                obj.DealMilestoneID = dealMilestone.ID;
                            }
                        }

                        var contactName = GetPropertyValue("client");

                        var localMembersDeal = new List <int>();

                        if (!String.IsNullOrEmpty(contactName))
                        {
                            var contacts = contactDao.GetContactsByName(contactName, true);

                            if (contacts.Count > 0)
                            {
                                obj.ContactID = contacts[0].ID;
                                localMembersDeal.Add(obj.ContactID);
                            }
                            else
                            {
                                contacts = contactDao.GetContactsByName(contactName, false);
                                if (contacts.Count > 0)
                                {
                                    obj.ContactID = contacts[0].ID;
                                    localMembersDeal.Add(obj.ContactID);
                                }
                            }
                        }

                        var members = GetPropertyValue("member");

                        if (!String.IsNullOrEmpty(members))
                        {
                            var membersList = members.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                            foreach (var item in membersList)
                            {
                                var findedMember = contactDao.GetContactsByName(item, true);

                                if (findedMember.Count > 0)
                                {
                                    localMembersDeal.Add(findedMember[0].ID);
                                }
                                else
                                {
                                    findedMember = _daoFactory.ContactDao.GetContactsByName(item, false);
                                    if (findedMember.Count > 0)
                                    {
                                        localMembersDeal.Add(findedMember[0].ID);
                                    }
                                }
                            }
                        }

                        if (localMembersDeal.Count > 0)
                        {
                            findedDealMembers.Add(obj.ID, localMembersDeal);
                        }


                        foreach (JProperty jToken in _importSettings.ColumnMapping.Children())
                        {
                            var propertyValue = GetPropertyValue(jToken.Name);

                            if (String.IsNullOrEmpty(propertyValue))
                            {
                                continue;
                            }

                            if (!jToken.Name.StartsWith("customField_"))
                            {
                                continue;
                            }

                            var fieldID = Convert.ToInt32(jToken.Name.Split(new[] { '_' })[1]);
                            var field   = customFieldDao.GetFieldDescription(fieldID);

                            if (field != null)
                            {
                                findedCustomField.Add(new CustomField
                                {
                                    EntityID   = obj.ID,
                                    EntityType = EntityType.Opportunity,
                                    ID         = fieldID,
                                    Value      = field.FieldType == CustomFieldType.CheckBox ? (propertyValue == "on" || propertyValue == "true" ? "true" : "false") : propertyValue
                                });
                            }
                        }

                        Percentage += 1.0 * 100 / (ImportFromCSV.MaxRoxCount * 2);

                        if (ImportDataCache.CheckCancelFlag(EntityType.Opportunity))
                        {
                            ImportDataCache.ResetAll(EntityType.Opportunity);

                            throw new OperationCanceledException();
                        }

                        ImportDataCache.Insert(EntityType.Opportunity, (ImportDataOperation)Clone());



                        findedDeals.Add(obj);

                        if (currentIndex + 1 > ImportFromCSV.MaxRoxCount)
                        {
                            break;
                        }

                        currentIndex++;
                    }


                    Percentage = 50;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Opportunity))
                    {
                        ImportDataCache.ResetAll(EntityType.Opportunity);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Opportunity, (ImportDataOperation)Clone());

                    var newDealIDs = dealDao.SaveDealList(findedDeals);
                    findedDeals.ForEach(d => d.ID = newDealIDs[d.ID]);

                    Percentage += 12.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Opportunity))
                    {
                        ImportDataCache.ResetAll(EntityType.Opportunity);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Opportunity, (ImportDataOperation)Clone());

                    findedCustomField.ForEach(item => item.EntityID = newDealIDs[item.EntityID]);

                    customFieldDao.SaveList(findedCustomField);

                    Percentage += 12.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Opportunity))
                    {
                        ImportDataCache.ResetAll(EntityType.Opportunity);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Opportunity, (ImportDataOperation)Clone());

                    foreach (var findedDealMemberKey in findedDealMembers.Keys)
                    {
                        dealDao.SetMembers(newDealIDs[findedDealMemberKey], findedDealMembers[findedDealMemberKey].ToArray());
                    }

                    Percentage += 12.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Opportunity))
                    {
                        ImportDataCache.ResetAll(EntityType.Opportunity);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Opportunity, (ImportDataOperation)Clone());

                    foreach (var findedTagKey in findedTags.Keys)
                    {
                        tagDao.SetTagToEntity(EntityType.Opportunity, newDealIDs[findedTagKey], findedTags[findedTagKey].ToArray());
                    }

                    if (_importSettings.IsPrivate)
                    {
                        findedDeals.ForEach(dealItem => CRMSecurity.SetAccessTo(dealItem, _importSettings.AccessList));
                    }

                    Percentage += 12.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Opportunity))
                    {
                        ImportDataCache.ResetAll(EntityType.Opportunity);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Opportunity, (ImportDataOperation)Clone());
                }

            Complete();
        }
Exemple #10
0
        private void ImportCaseData()
        {
            using (var CSVFileStream = _dataStore.GetReadStream("temp", _CSVFileURI))
                using (CsvReader csv = ImportFromCSV.CreateCsvReaderInstance(CSVFileStream, _importSettings))
                {
                    int currentIndex = 0;

                    var casesDao       = _daoFactory.GetCasesDao();
                    var customFieldDao = _daoFactory.GetCustomFieldDao();
                    var tagDao         = _daoFactory.GetTagDao();

                    var findedTags         = new Dictionary <int, List <String> >();
                    var findedCustomField  = new List <CustomField>();
                    var findedCases        = new List <ASC.CRM.Core.Entities.Cases>();
                    var findedCasesMembers = new Dictionary <int, List <int> >();

                    while (csv.ReadNextRecord())
                    {
                        _columns = csv.GetCurrentRowFields(false);

                        var objCases = new ASC.CRM.Core.Entities.Cases();

                        objCases.ID = currentIndex;

                        objCases.Title = GetPropertyValue("title");

                        if (String.IsNullOrEmpty(objCases.Title))
                        {
                            continue;
                        }

                        foreach (JProperty jToken in _importSettings.ColumnMapping.Children())
                        {
                            var propertyValue = GetPropertyValue(jToken.Name);

                            if (String.IsNullOrEmpty(propertyValue))
                            {
                                continue;
                            }

                            if (!jToken.Name.StartsWith("customField_"))
                            {
                                continue;
                            }

                            var fieldID = Convert.ToInt32(jToken.Name.Split(new[] { '_' })[1]);

                            var field = customFieldDao.GetFieldDescription(fieldID);

                            if (field != null)
                            {
                                findedCustomField.Add(new CustomField
                                {
                                    EntityID   = objCases.ID,
                                    EntityType = EntityType.Case,
                                    ID         = fieldID,
                                    Value      = field.FieldType == CustomFieldType.CheckBox ? (propertyValue == "on" || propertyValue == "true" ? "true" : "false") : propertyValue
                                });
                            }
                        }

                        var tag = GetPropertyValue("tag");

                        if (!String.IsNullOrEmpty(tag))
                        {
                            var tagList = tag.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                            tagList.AddRange(_importSettings.Tags);
                            tagList = tagList.Distinct().ToList();
                            findedTags.Add(objCases.ID, tagList);
                        }
                        else if (_importSettings.Tags.Count != 0)
                        {
                            findedTags.Add(objCases.ID, _importSettings.Tags);
                        }

                        var localMembersCases = new List <int>();

                        var members = GetPropertyValue("member");

                        if (!String.IsNullOrEmpty(members))
                        {
                            var membersList = members.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                            foreach (var item in membersList)
                            {
                                var findedMember = _daoFactory.GetContactDao().GetContactsByName(item, true);

                                if (findedMember.Count > 0)
                                {
                                    localMembersCases.Add(findedMember[0].ID);
                                }
                                else
                                {
                                    findedMember = _daoFactory.GetContactDao().GetContactsByName(item, false);
                                    if (findedMember.Count > 0)
                                    {
                                        localMembersCases.Add(findedMember[0].ID);
                                    }
                                }
                            }
                        }

                        if (localMembersCases.Count > 0)
                        {
                            findedCasesMembers.Add(objCases.ID, localMembersCases);
                        }

                        objCases.ID = currentIndex;

                        findedCases.Add(objCases);

                        if (currentIndex + 1 > ImportFromCSV.MaxRoxCount)
                        {
                            break;
                        }

                        currentIndex++;
                    }

                    Percentage = 62.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Case))
                    {
                        ImportDataCache.ResetAll(EntityType.Case);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Case, (ImportDataOperation)Clone());

                    var newIDs = casesDao.SaveCasesList(findedCases);
                    findedCases.ForEach(d => d.ID = newIDs[d.ID]);

                    findedCustomField.ForEach(item => item.EntityID = newIDs[item.EntityID]);

                    customFieldDao.SaveList(findedCustomField);

                    Percentage += 12.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Case))
                    {
                        ImportDataCache.ResetAll(EntityType.Case);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Case, (ImportDataOperation)Clone());

                    foreach (var findedCasesMemberKey in findedCasesMembers.Keys)
                    {
                        _daoFactory.GetDealDao().SetMembers(newIDs[findedCasesMemberKey], findedCasesMembers[findedCasesMemberKey].ToArray());
                    }

                    Percentage += 12.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Case))
                    {
                        ImportDataCache.ResetAll(EntityType.Case);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Case, (ImportDataOperation)Clone());


                    foreach (var findedTagKey in findedTags.Keys)
                    {
                        tagDao.SetTagToEntity(EntityType.Case, newIDs[findedTagKey], findedTags[findedTagKey].ToArray());
                    }

                    if (_importSettings.IsPrivate)
                    {
                        findedCases.ForEach(dealItem => CRMSecurity.SetAccessTo(dealItem, _importSettings.AccessList));
                    }


                    Percentage += 12.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Case))
                    {
                        ImportDataCache.ResetAll(EntityType.Case);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Case, (ImportDataOperation)Clone());
                }

            Complete();
        }
        private void ImportContactsData()
        {
            using (var CSVFileStream = _dataStore.GetReadStream("temp", _CSVFileURI))
                using (CsvReader csv = ImportFromCSV.CreateCsvReaderInstance(CSVFileStream, _importSettings))
                {
                    var countactCount = ImportFromCSV.MaxRoxCount;

                    var addressTemplate = new JObject();

                    foreach (String addressPartName in Enum.GetNames(typeof(AddressPart)))
                    {
                        addressTemplate.Add(addressPartName.ToLower(), "");
                    }

                    var addressTemplateStr = addressTemplate.ToString();

                    var personFakeIdCompanyNameHash = new Dictionary <int, String>();

                    var contactDao     = _daoFactory.GetContactDao();
                    var contactInfoDao = _daoFactory.GetContactInfoDao();
                    var customFieldDao = _daoFactory.GetCustomFieldDao();
                    var tagDao         = _daoFactory.GetTagDao();
                    var listItemDao    = _daoFactory.GetListItemDao();

                    var findedContacts     = new Dictionary <int, Contact>();
                    var findedTags         = new Dictionary <int, List <String> >();
                    var findedCustomField  = new List <CustomField>();
                    var findedContactInfos = new List <ContactInfo>();

                    int currentIndex = 0;

                    while (csv.ReadNextRecord())
                    {
                        _columns = csv.GetCurrentRowFields(false);

                        var firstName   = GetPropertyValue("firstName");
                        var lastName    = GetPropertyValue("lastName");
                        var companyName = GetPropertyValue("companyName");

                        if ((String.IsNullOrEmpty(firstName) || String.IsNullOrEmpty(lastName)) &&
                            String.IsNullOrEmpty(companyName))
                        {
                            continue;
                        }

                        Percentage += 1.0 * 100 / (countactCount * 2);

                        Contact contact;

                        if (!(String.IsNullOrEmpty(firstName) || String.IsNullOrEmpty(lastName)))
                        {
                            var person = new Person
                            {
                                ID        = currentIndex,
                                FirstName = firstName,
                                LastName  = lastName,
                                JobTitle  = GetPropertyValue("title")
                            };

                            if (!(String.IsNullOrEmpty(companyName)))
                            {
                                personFakeIdCompanyNameHash.Add(person.ID, companyName);
                            }

                            contact = person;
                        }
                        else
                        {
                            contact = new Company
                            {
                                ID = currentIndex
                            };

                            ((Company)contact).CompanyName = companyName;
                        }

                        contact.About    = GetPropertyValue("notes");
                        contact.IsShared = _importSettings.IsShared;

                        var contactStageName = GetPropertyValue("contactStage");
                        var contactTypeName  = GetPropertyValue("contactType");
                        if (!String.IsNullOrEmpty(contactStageName))
                        {
                            var contactStage = listItemDao.GetByTitle(ListType.ContactStatus, contactStageName);
                            if (contactStage != null)
                            {
                                contact.StatusID = contactStage.ID;
                            }
                            else
                            {
                                contact.StatusID = listItemDao.CreateItem(ListType.ContactStatus, new ListItem()
                                {
                                    Title       = contactStageName,
                                    Color       = "#77cf9a",
                                    Description = ""
                                });
                            }
                        }

                        if (!String.IsNullOrEmpty(contactTypeName))
                        {
                            var contactType = listItemDao.GetByTitle(ListType.ContactType, contactTypeName);
                            if (contactType != null)
                            {
                                contact.ContactTypeID = contactType.ID;
                            }
                            else
                            {
                                contact.ContactTypeID = listItemDao.CreateItem(ListType.ContactType, new ListItem()
                                {
                                    Title       = contactTypeName,
                                    Description = ""
                                });
                            }
                        }


                        findedContacts.Add(contact.ID, contact);


                        var tag = GetPropertyValue("tag");

                        if (!String.IsNullOrEmpty(tag))
                        {
                            findedTags.Add(contact.ID, tag.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList());
                        }

                        var primaryFields = new List <int>();

                        foreach (JProperty jToken in _importSettings.ColumnMapping.Children())
                        {
                            var propertyValue = GetPropertyValue(jToken.Name);

                            if (String.IsNullOrEmpty(propertyValue))
                            {
                                continue;
                            }

                            if (jToken.Name.StartsWith("customField_"))
                            {
                                var fieldID = Convert.ToInt32(jToken.Name.Split(new[] { '_' })[1]);

                                findedCustomField.Add(new CustomField
                                {
                                    EntityID   = contact.ID,
                                    EntityType = contact is Person ? EntityType.Person : EntityType.Company,
                                    ID         = fieldID,
                                    Value      = propertyValue
                                });
                            }
                            else if (jToken.Name.StartsWith("contactInfo_"))
                            {
                                var nameParts       = jToken.Name.Split(new[] { '_' }).Skip(1).ToList();
                                var contactInfoType =
                                    (ContactInfoType)Enum.Parse(typeof(ContactInfoType), nameParts[0]);

                                var category = Convert.ToInt32(nameParts[1]);

                                bool isPrimary = false;

                                if ((contactInfoType == ContactInfoType.Email ||
                                     contactInfoType == ContactInfoType.Phone ||
                                     contactInfoType == ContactInfoType.Address) &&
                                    (!primaryFields.Contains((int)contactInfoType)))
                                {
                                    isPrimary = true;

                                    primaryFields.Add((int)contactInfoType);
                                }

                                if (contactInfoType == ContactInfoType.Address)
                                {
                                    var addressPart = (AddressPart)Enum.Parse(typeof(AddressPart), nameParts[2]);

                                    var findedAddress =
                                        findedContactInfos.Find(
                                            item =>
                                            (category == item.Category) && (item.InfoType == ContactInfoType.Address) &&
                                            (item.ContactID == contact.ID));

                                    if (findedAddress == null)
                                    {
                                        findedAddress = new ContactInfo
                                        {
                                            Category  = category,
                                            InfoType  = contactInfoType,
                                            Data      = addressTemplateStr,
                                            ContactID = contact.ID,
                                            IsPrimary = isPrimary
                                        };

                                        findedContactInfos.Add(findedAddress);
                                    }

                                    var addressParts = JObject.Parse(findedAddress.Data);

                                    addressParts[addressPart.ToString().ToLower()] = propertyValue;

                                    findedAddress.Data = addressParts.ToString();

                                    continue;
                                }

                                findedContactInfos.Add(new ContactInfo
                                {
                                    Category  = category,
                                    InfoType  = contactInfoType,
                                    Data      = propertyValue,
                                    ContactID = contact.ID,
                                    IsPrimary = isPrimary
                                });
                            }
                        }


                        if (currentIndex + 1 > ImportFromCSV.MaxRoxCount)
                        {
                            break;
                        }

                        currentIndex++;
                    }

                    Percentage = 37.5;

                    switch (_importSettings.DuplicateRecordRule)
                    {
                    case 1:  // Skip
                    {
                        var emails = findedContactInfos.Where(item => item.InfoType == ContactInfoType.Email).ToList();

                        var duplicateContactsID = contactDao.FindDuplicateByEmail(emails).Select(
                            row => Convert.ToInt32(row[0])).Distinct().ToList();

                        if (duplicateContactsID.Count == 0)
                        {
                            break;
                        }

                        findedContacts = findedContacts.Where(item => !duplicateContactsID.Contains(item.Key)).ToDictionary(x => x.Key, y => y.Value);

                        personFakeIdCompanyNameHash = personFakeIdCompanyNameHash.Where(item => !duplicateContactsID.Contains(item.Key)).ToDictionary(x => x.Key, y => y.Value);

                        if (findedContacts.Count == 0)
                        {
                            Complete();

                            return;
                        }

                        findedContactInfos = findedContactInfos.Where(item => !duplicateContactsID.Contains(item.ContactID)).ToList();
                        findedCustomField  = findedCustomField.Where(item => !duplicateContactsID.Contains(item.EntityID)).ToList();

                        foreach (var exceptID in duplicateContactsID)
                        {
                            if (findedTags.ContainsKey(exceptID))
                            {
                                findedTags.Remove(exceptID);
                            }
                        }
                    }
                    break;

                    case 2:  // Overwrite
                    {
                        var emailContactInfos = findedContactInfos.Where(item => item.InfoType == ContactInfoType.Email).ToList();

                        var duplicateContactsID = contactDao.FindDuplicateByEmail(emailContactInfos).Select(
                            row => Convert.ToInt32(row[2])).Distinct().ToArray();

                        contactDao.DeleteBatchContact(duplicateContactsID);

                        break;
                    }

                    case 3: // Clone
                        break;

                    default:
                        break;
                    }

                    Percentage += 12.5;

                    var findedCompanies = findedContacts.Where(x => x.Value is Company).ToDictionary(x => x.Key, y => y.Value);
                    var findedPeoples   = findedContacts.Where(x => x.Value is Person).ToDictionary(x => x.Key, y => y.Value);

                    var fakeRealContactIdHash = contactDao.SaveContactList(findedCompanies.Values.ToList())
                                                .ToDictionary(item => item.Key, item => item.Value);

                    var companyNameRealIdHash = new Dictionary <String, int>();

                    foreach (Company item in findedCompanies.Values)
                    {
                        if (companyNameRealIdHash.ContainsKey(item.CompanyName))
                        {
                            continue;
                        }

                        companyNameRealIdHash.Add(item.CompanyName, item.ID);
                    }

                    foreach (var item in personFakeIdCompanyNameHash)
                    {
                        var person = (Person)findedPeoples[item.Key];


                        if (companyNameRealIdHash.ContainsKey(item.Value))
                        {
                            person.CompanyID = companyNameRealIdHash[item.Value];
                        }
                        else
                        {
                            var findedCompany = contactDao.GetContactsByName(item.Value).FirstOrDefault(x => x is Company);

                            // Why ???
                            if (findedCompany == null)
                            {
                                person.CompanyID = contactDao.SaveContact(new Company
                                {
                                    CompanyName = item.Value
                                });
                            }
                            else
                            {
                                person.CompanyID = findedCompany.ID;
                            }

                            companyNameRealIdHash.Add(item.Value, person.CompanyID);
                        }
                    }

                    fakeRealContactIdHash = fakeRealContactIdHash.Union(contactDao.SaveContactList(findedPeoples.Values.ToList())).ToDictionary(item => item.Key, item => item.Value);

                    Percentage += 12.5;

                    findedContactInfos.ForEach(item => item.ContactID = fakeRealContactIdHash[item.ContactID]);

                    contactInfoDao.SaveList(findedContactInfos);

                    Percentage += 12.5;

                    findedCustomField.ForEach(item => item.EntityID = fakeRealContactIdHash[item.EntityID]);

                    customFieldDao.SaveList(findedCustomField);

                    Percentage += 12.5;

                    foreach (var findedTagKey in findedTags.Keys)
                    {
                        tagDao.SetTagToEntity(EntityType.Contact, fakeRealContactIdHash[findedTagKey], findedTags[findedTagKey].ToArray());
                    }

                    findedContacts.Values.ToList().ForEach(contact => CRMSecurity.SetAccessTo(contact, _importSettings.ContactManagers));

                    Percentage += 12.5;
                }

            Complete();
        }
        private void ImportContactsData()
        {
            using (var CSVFileStream = _dataStore.GetReadStream("temp", _CSVFileURI))
                using (CsvReader csv = ImportFromCSV.CreateCsvReaderInstance(CSVFileStream, _importSettings))
                {
                    int currentIndex = 0;

                    var personFakeIdCompanyNameHash = new Dictionary <int, String>();

                    var contactDao     = _daoFactory.GetContactDao();
                    var contactInfoDao = _daoFactory.GetContactInfoDao();
                    var customFieldDao = _daoFactory.GetCustomFieldDao();
                    var tagDao         = _daoFactory.GetTagDao();

                    var findedContacts     = new Dictionary <int, Contact>();
                    var findedTags         = new Dictionary <int, List <String> >();
                    var findedCustomField  = new List <CustomField>();
                    var findedContactInfos = new List <ContactInfo>();

                    while (csv.ReadNextRecord())
                    {
                        _columns = csv.GetCurrentRowFields(false);
                        Contact contact = null;

                        #region Common data

                        if (!_CommonData(currentIndex, ref contact, ref personFakeIdCompanyNameHash))
                        {
                            continue;
                        }

                        findedContacts.Add(contact.ID, contact);

                        #endregion

                        #region Read tags

                        _ReadTags(ref findedTags, contact);

                        #endregion

                        #region Custom fields Y contact infos

                        var primaryFields = new List <int>();

                        foreach (JProperty jToken in _importSettings.ColumnMapping.Children())
                        {
                            var propertyValue = GetPropertyValue(jToken.Name);
                            if (String.IsNullOrEmpty(propertyValue))
                            {
                                continue;
                            }

                            if (jToken.Name.StartsWith("customField_"))
                            {
                                _ReadCustomField(jToken, propertyValue, contact, ref findedCustomField, customFieldDao);
                            }
                            else if (jToken.Name.StartsWith("contactInfo_"))
                            {
                                var addressTemplate = new JObject();
                                foreach (String addressPartName in Enum.GetNames(typeof(AddressPart)))
                                {
                                    addressTemplate.Add(addressPartName.ToLower(), "");
                                }

                                var addressTemplateStr = addressTemplate.ToString();

                                _ReadContactInfo(jToken, propertyValue, contact, ref findedContactInfos, ref primaryFields, addressTemplateStr);
                            }
                        }
                        #endregion

                        if (currentIndex + 1 > ImportFromCSV.MaxRoxCount)
                        {
                            break;
                        }
                        currentIndex++;
                    }

                    Percentage = 37.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Contact))
                    {
                        ImportDataCache.ResetAll(EntityType.Contact);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Contact, (ImportDataOperation)Clone());


                    #region Processing duplicate rule

                    _DuplicateRecordRuleProcess(ref findedContacts, ref personFakeIdCompanyNameHash, ref findedContactInfos, ref findedCustomField, ref findedTags);

                    #endregion

                    Percentage += 12.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Contact))
                    {
                        ImportDataCache.ResetAll(EntityType.Contact);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Contact, (ImportDataOperation)Clone());

                    var findedCompanies = findedContacts.Where(x => x.Value is Company).ToDictionary(x => x.Key, y => y.Value);
                    var findedPeoples   = findedContacts.Where(x => x.Value is Person).ToDictionary(x => x.Key, y => y.Value);

                    var fakeRealContactIdHash = contactDao.SaveContactList(findedCompanies.Values.ToList())
                                                .ToDictionary(item => item.Key, item => item.Value);

                    var companyNameRealIdHash = new Dictionary <String, int>();

                    foreach (Company item in findedCompanies.Values)
                    {
                        if (companyNameRealIdHash.ContainsKey(item.CompanyName))
                        {
                            continue;
                        }

                        companyNameRealIdHash.Add(item.CompanyName, item.ID);
                    }

                    foreach (var item in personFakeIdCompanyNameHash)
                    {
                        var person = (Person)findedPeoples[item.Key];


                        if (companyNameRealIdHash.ContainsKey(item.Value))
                        {
                            person.CompanyID = companyNameRealIdHash[item.Value];
                        }
                        else
                        {
                            var findedCompany = contactDao.GetContactsByName(item.Value, true).FirstOrDefault();

                            // Why ???
                            if (findedCompany == null)
                            {
                                #region create COMPANY for person in csv

                                findedCompany = new Company
                                {
                                    CompanyName = item.Value,
                                    ShareType   = _importSettings.ShareType
                                };
                                findedCompany.ID = contactDao.SaveContact(findedCompany);

                                person.CompanyID = findedCompany.ID;
                                CRMSecurity.SetAccessTo(findedCompany, _importSettings.ContactManagers);

                                if (_importSettings.Tags.Count != 0)
                                {
                                    tagDao.SetTagToEntity(EntityType.Contact, person.CompanyID, _importSettings.Tags.ToArray());
                                }

                                #endregion
                            }
                            else
                            {
                                person.CompanyID = findedCompany.ID;
                            }

                            companyNameRealIdHash.Add(item.Value, person.CompanyID);
                        }
                    }

                    fakeRealContactIdHash = fakeRealContactIdHash.Union(contactDao.SaveContactList(findedPeoples.Values.ToList()))
                                            .ToDictionary(item => item.Key, item => item.Value);

                    Percentage += 12.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Contact))
                    {
                        ImportDataCache.ResetAll(EntityType.Contact);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Contact, (ImportDataOperation)Clone());


                    #region Save contact infos

                    findedContactInfos.ForEach(item => item.ContactID = fakeRealContactIdHash[item.ContactID]);
                    contactInfoDao.SaveList(findedContactInfos);

                    #endregion

                    Percentage += 12.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Contact))
                    {
                        ImportDataCache.ResetAll(EntityType.Contact);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Contact, (ImportDataOperation)Clone());

                    #region Save custom fields

                    findedCustomField.ForEach(item => item.EntityID = fakeRealContactIdHash[item.EntityID]);
                    customFieldDao.SaveList(findedCustomField);

                    #endregion

                    Percentage += 12.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Contact))
                    {
                        ImportDataCache.ResetAll(EntityType.Contact);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Contact, (ImportDataOperation)Clone());


                    #region Save tags
                    foreach (var findedTagKey in findedTags.Keys)
                    {
                        tagDao.SetTagToEntity(EntityType.Contact, fakeRealContactIdHash[findedTagKey], findedTags[findedTagKey].ToArray());
                    }
                    #endregion

                    #region CRMSecurity set

                    findedContacts.Values.ToList().ForEach(contact => CRMSecurity.SetAccessTo(contact, _importSettings.ContactManagers));

                    #endregion

                    Percentage += 12.5;

                    if (ImportDataCache.CheckCancelFlag(EntityType.Contact))
                    {
                        ImportDataCache.ResetAll(EntityType.Contact);

                        throw new OperationCanceledException();
                    }

                    ImportDataCache.Insert(EntityType.Contact, (ImportDataOperation)Clone());
                }

            Complete();
        }