protected void StartImportingContacts() { var importOptions = new ContactImportOptions { FileName = RealFilename, PlanId = Tracker.DefinitionDatabase.GetItem(StateId).ParentID, StateId = StateId, ContactIdentifierField = ContactIdentifier.Value, ContactFirstNameField = ContactFirstName.Value, ContactLastNameField = ContactLastName.Value, ContactEmailField = ContactEmail.Value }; StartJob("Import Contacts", "DoImportContacts", this, importOptions); CheckImport(); }
protected string DoImportContacts(ContactImportOptions options) { Assert.ArgumentNotNull(options, "options"); var contactManager = Factory.CreateObject("tracking/contactManager", true) as ContactManager; var contactRepository = Factory.CreateObject("tracking/contactRepository", true) as ContactRepository; var numOfImported = 0; var numOfContactExists = 0; var numOfBadContacts = 0; var numOfContactsAddedToState = 0; using (var csvFileReader = new CsvFileReader(options.FileName)) { try { var columnNameFields = csvFileReader.ReadLine(); var contactIdentifierIndex = columnNameFields.FindIndex(h => string.Equals(options.ContactIdentifierField, h)); if (contactIdentifierIndex < 0) { return(string.Empty); } var contactFirstNameIndex = columnNameFields.FindIndex(x => string.Equals(options.ContactFirstNameField, x)); var contactLastNameIndex = columnNameFields.FindIndex(x => string.Equals(options.ContactLastNameField, x)); var contactEmailIndex = columnNameFields.FindIndex(x => string.Equals(options.ContactEmailField, x)); var valueFields = csvFileReader.ReadLine(); while (valueFields != null) { var contactIdentifier = valueFields[contactIdentifierIndex]; if (string.IsNullOrWhiteSpace(contactIdentifier)) { numOfBadContacts++; } else { var leaseOwner = new LeaseOwner("AddContacts-" + Guid.NewGuid(), LeaseOwnerType.OutOfRequestWorker); LockAttemptResult <Contact> lockAttemptResult; var contact = contactManager.LoadContactReadOnly(contactIdentifier); if (contact != null) { numOfContactExists++; lockAttemptResult = contactRepository.TryLoadContact(contact.ContactId, leaseOwner, TimeSpan.FromSeconds(3)); if (lockAttemptResult.Status == LockAttemptStatus.Success) { contact = lockAttemptResult.Object; contact.ContactSaveMode = ContactSaveMode.AlwaysSave; } else { Log.Error("Cannot lock contact! " + lockAttemptResult.Status, this); } } else { contact = contactRepository.CreateContact(ID.NewID); contact.Identifiers.Identifier = contactIdentifier; contact.System.Value = 0; contact.System.VisitCount = 0; contact.ContactSaveMode = ContactSaveMode.AlwaysSave; lockAttemptResult = new LockAttemptResult <Contact>(LockAttemptStatus.Success, contact, leaseOwner); } UpdateContactPersonalInfo(contact, contactFirstNameIndex, valueFields, contactLastNameIndex); UpdateContactEmailAddress(contactEmailIndex, contact, valueFields); if ((lockAttemptResult.Status != LockAttemptStatus.AlreadyLocked) && (lockAttemptResult.Status != LockAttemptStatus.NotFound)) { if (contact.AutomationStates().IsInEngagementPlan(options.PlanId)) { contact.AutomationStates().MoveToEngagementState(options.PlanId, options.StateId); Log.Info( string.Format("Move contact: {0} to engagement plan stateId: {1}", contact.ContactId, options.StateId), this); } else { contact.AutomationStates().EnrollInEngagementPlan(options.PlanId, options.StateId); Log.Info( string.Format("Enrolled contact: {0} to engagement plan stateId: {1}", contact.ContactId, options.StateId), this); } contactRepository.SaveContact(contact, new ContactSaveOptions(true, leaseOwner, null)); numOfContactsAddedToState++; } else { Log.Error( string.Format("Failed to enroll contact: {0} in engagement plan stateId: {1}", contact.ContactId, options.StateId), this); } numOfImported++; } valueFields = csvFileReader.ReadLine(); } } catch (Exception ex) { Log.Error(ex.Message, ex, this); } } Log.Info( string.Format( "Import Contacts Finished: Imported: {0}, Contact Exists: {1}, Bad Contact Data: {2}, Added to State: {3}", numOfImported, numOfContactExists, numOfBadContacts, numOfContactsAddedToState), GetType()); return(numOfImported + "|" + numOfContactExists + "|" + numOfBadContacts + "|" + "|" + numOfContactsAddedToState + "|"); }