public void RunJob() { try { CoreContext.TenantManager.SetCurrentTenant(_tenantID); SecurityContext.AuthenticateMe(_author); using (var scope = DIHelper.Resolve()) { var daoFactory = scope.Resolve <DaoFactory>(); var userCulture = CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID).GetCulture(); System.Threading.Thread.CurrentThread.CurrentCulture = userCulture; System.Threading.Thread.CurrentThread.CurrentUICulture = userCulture; ImportDataCache.Insert(_entityType, (ImportDataOperation)Clone()); switch (_entityType) { case EntityType.Contact: ImportContactsData(daoFactory); break; case EntityType.Opportunity: ImportOpportunityData(daoFactory); break; case EntityType.Case: ImportCaseData(daoFactory); break; case EntityType.Task: ImportTaskData(daoFactory); break; default: throw new ArgumentException(CRMErrorsResource.EntityTypeUnknown); } } } catch (OperationCanceledException) { _log.Debug("Queue canceled"); } finally { ImportDataCache.ResetAll(_entityType, _tenantID); } }
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(); }
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(); }
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)) { 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(); }
public void RunJob() { CoreContext.TenantManager.SetCurrentTenant(_tenantID); SecurityContext.AuthenticateMe(_author); var userCulture = CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID).GetCulture(); System.Threading.Thread.CurrentThread.CurrentCulture = userCulture; System.Threading.Thread.CurrentThread.CurrentUICulture = userCulture; //Fake http context allows fearlessly use shared DbManager. bool fakeContext = HttpContext.Current == null; ImportDataCache.Insert(_entityType, (ImportDataOperation)Clone()); try { if (fakeContext) { HttpContext.Current = new HttpContext( new HttpRequest("fake", CommonLinkUtility.GetFullAbsolutePath(PathProvider.BaseAbsolutePath), string.Empty), new HttpResponse(new StringWriter())); } switch (_entityType) { case EntityType.Contact: ImportContactsData(); break; case EntityType.Opportunity: ImportOpportunityData(); break; case EntityType.Case: ImportCaseData(); break; case EntityType.Task: ImportTaskData(); break; default: throw new ArgumentException(CRMErrorsResource.EntityTypeUnknown); } } catch (OperationCanceledException) { _log.Debug("Queue canceled"); } finally { ImportDataCache.ResetAll(_entityType); if (fakeContext && HttpContext.Current != null) { new DisposableHttpContext(HttpContext.Current).Dispose(); HttpContext.Current = null; } } }