コード例 #1
0
        private Contact CreateContact()
        {
            var identifier = CreateUniqueIdentifier();

            var contact = _contactRepository.CreateContact(ID.NewID);

            contact.Identifiers.AuthenticationLevel = AuthenticationLevel.None;
            contact.System.Classification           = 0;
            contact.ContactSaveMode               = ContactSaveMode.AlwaysSave;
            contact.Identifiers.Identifier        = identifier;
            contact.System.OverrideClassification = 0;
            contact.System.Value      = 0;
            contact.System.VisitCount = 0;

/*
 *          var contactPreferences = contact.GetFacet<IContactPreferences>("Preferences");
 *          contactPreferences.Language = _languages;
 */
            var contactPersonalInfo = contact.GetFacet <IContactPersonalInfo>("Personal");

            contactPersonalInfo.FirstName = Name.First();
            contactPersonalInfo.Surname   = Name.Last();

            var contactEmailAddresses = contact.GetFacet <IContactEmailAddresses>("Emails");

            contactEmailAddresses.Entries.Create("Work").SmtpAddress = Internet.Email($"{contactPersonalInfo.FirstName} {contactPersonalInfo.Surname}");
            contactEmailAddresses.Preferred = "Work";

            var leaseOwner = new LeaseOwner("CONTACT_CREATE", LeaseOwnerType.OutOfRequestWorker);
            var options    = new ContactSaveOptions(true, leaseOwner, null);

            _contactRepository.SaveContact(contact, options);

            return(contact);
        }
コード例 #2
0
        public override void Execute(ID formId, AdaptedResultList adaptedFields, ActionCallContext actionCallContext = null, params object[] data)
        {
            _log = Sitecore.Diagnostics.LoggerFactory.GetLogger("LogFileAppender");

            var name      = adaptedFields.GetEntryByName("Full Name");
            var contactno = adaptedFields.GetEntryByName("Contact Number");
            var email     = adaptedFields.GetEntryByName("Email Address");

            var Age         = adaptedFields.GetEntryByName("Age");
            var Company     = adaptedFields.GetEntryByName("Company");
            var Designation = adaptedFields.GetEntryByName("Designation");

            _log.Info("Writing comment to database");
            HttpContext.Current.Session["Email"] = email;

            /// var goal = new PageEventData("My Custom Goal", Guid.Parse("{47FF654B-76B2-49EF-A6AA-C61AE6093768}"));
            // Tracker.Current.CurrentPage.Register(goal);
            LockAttemptResult <Contact> lockResult;
            ContactRepository           contactRepository = Sitecore.Configuration.Factory.CreateObject("tracking/contactRepository", true) as ContactRepository;
            ContactManager contactManager = Sitecore.Configuration.Factory.CreateObject("tracking/contactManager", true) as ContactManager;

            var contact = Tracker.Current?.Contact;

            if (contact == null)
            {
                lockResult = new LockAttemptResult <Contact>(LockAttemptStatus.NotFound, null, null);
                contact    = contactRepository.CreateContact(Sitecore.Data.ID.NewID);
                contact.Identifiers.AuthenticationLevel = Sitecore.Analytics.Model.AuthenticationLevel.None;
                contact.Identifiers.Identifier          = "*****@*****.**";
                contact.Identifiers.IdentificationLevel = ContactIdentificationLevel.Known;
                contact.System.Value      = 0;
                contact.System.VisitCount = 0;
                contact.ContactSaveMode   = ContactSaveMode.AlwaysSave;
                contactManager.FlushContactToXdb(contact);
                //  VisitorIdentification s = new VisitorIdentification();
            }
            else
            {
                lockResult = contactManager.TryLoadContact(contact.ContactId);


                contact = lockResult.Object;
            }
            if (contact != null)
            {
                var contactEmailAddresses = contact.GetFacet <IContactCommentsFacet>("Contact Details");

                if (!contactEmailAddresses.CustomerDetails.Contains("Contact Details"))
                {
                    contactEmailAddresses.CustomerDetails.Create("Contact Details");
                    contactEmailAddresses.CustomerDetails["Contact Details"].Age      = "18";
                    contactEmailAddresses.CustomerDetails["Contact Details"].Company  = "Companyemarr";
                    contactEmailAddresses.CustomerDetails["Contact Details"].FullName = "Designationdsddf";
                    contactEmailAddresses.CustomerDetails["Contact Details"].Email    = "Emailaddress";
                }
                var leaseOwner = new LeaseOwner(GetType() + Guid.NewGuid().ToString(), LeaseOwnerType.OutOfRequestWorker);

                contactManager.SaveAndReleaseContactToXdb(contact);
            }
        }
        public override bool SaveContact(IContact contact, LeaseOwner leaseOwner, bool release)
        {
            var res = this.contactService.Save(
                this.modelMapper.Map<IContact, ElasticContact>(contact),
                this.modelMapper.Map<LeaseOwner, ElasticLeaseOwner>(leaseOwner),
                release,
                this.ctx);

            if (res && release)
            {
                contact.Lease = null;
            }

            return res;
        }
        public void SaveContact_ContactDoesntExist_ContactSave(
            DataAdapterProvider provider,
            IContactFactory contactFactory,
            IContact contact,
            LeaseOwner leaseOwner,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                provider.SaveContact(contact, new ContactSaveOptions(true, leaseOwner)).Should().BeTrue();

                var actual = provider.LoadContactReadOnly(contact.Id, contactFactory);

                AssertContactsEqual(actual, contact);
            }
        }
コード例 #5
0
        public Contact CreateContact()
        {
            var identifier = "XGen" + ContactCount;

            var contactRepository = new ContactRepository();

            var contact = contactRepository.LoadContactReadOnly(identifier);

            while (contact != null)
            {
                ContactCount++;
                identifier = "XGen" + ContactCount;
                contact    = contactRepository.LoadContactReadOnly(identifier);
            }

            contact = contactRepository.CreateContact(ID.NewID);
            contact.Identifiers.AuthenticationLevel = AuthenticationLevel.None;
            contact.System.Classification           = 0;
            contact.ContactSaveMode               = ContactSaveMode.AlwaysSave;
            contact.Identifiers.Identifier        = "XGen" + ContactCount;
            contact.System.OverrideClassification = 0;
            contact.System.Value      = 0;
            contact.System.VisitCount = 0;

            var contactPreferences = contact.GetFacet <IContactPreferences>("Preferences");

            contactPreferences.Language = _languages[ContactCount % _languages.Length];

            var contactPersonalInfo = contact.GetFacet <IContactPersonalInfo>("Personal");

            contactPersonalInfo.FirstName = Faker.Name.First();
            contactPersonalInfo.Surname   = Faker.Name.Last();

            var contactEmailAddresses = contact.GetFacet <IContactEmailAddresses>("Emails");

            contactEmailAddresses.Entries.Create("Work").SmtpAddress =
                Faker.Internet.Email($"{contactPersonalInfo.FirstName} {contactPersonalInfo.Surname}");
            contactEmailAddresses.Preferred = "Work";

            var leaseOwner = new LeaseOwner("CONTACT_CREATE", LeaseOwnerType.OutOfRequestWorker);
            var options    = new ContactSaveOptions(true, leaseOwner, null);

            contactRepository.SaveContact(contact, options);

            DoContactCreated(contact);
            return(contact);
        }
コード例 #6
0
        public PipelineExecutionResult Execute(PurchaseOrder purchaseOrder)
        {
            var contactRepositoryBase = Assert.ResultNotNull(Factory.CreateObject("contactRepository", true) as ContactRepositoryBase);

            var    contactFactory = new ContactFactory();
            string userIdentifier = contactFactory.GetContact();
            var    leaseOwner     = new LeaseOwner(AnalyticsSettings.ClusterName, LeaseOwnerType.WebCluster);

            var contact = CommerceAutomationHelper.GetContact(userIdentifier);

            if (contact != null)
            {
                AddEmailAddressToContact(purchaseOrder, contact);
                contactRepositoryBase.SaveContact(contact, new ContactSaveOptions(true, leaseOwner, new TimeSpan?()));
            }

            return(PipelineExecutionResult.Success);
        }
        public override Sitecore.Analytics.DataAccess.LockAttemptResult<IContact> TryLoadContact(
            string identifier,
            IContactFactory factory,
            LeaseOwner leaseOwner,
            TimeSpan leaseDuration)
        {
            var res = this.contactService.TryLoadAndLock(
                identifier,
                this.modelMapper.Map<LeaseOwner, ElasticLeaseOwner>(leaseOwner),
                leaseDuration,
                this.ctx);

            IContact scContact = null;
            if (res.LockedObject != null)
            {
                scContact = factory.Create(new ID(res.LockedObject.Id));
                this.modelMapper.Map(res.LockedObject, scContact);
            }

            return new Sitecore.Analytics.DataAccess.LockAttemptResult<IContact>(
                (LockAttemptStatus)(int)res.Status,
                scContact,
                this.modelMapper.Map<ElasticLeaseOwner, LeaseOwner>(res.LeaseOwner));
        }
        public void TryLoadContactById_ContactExistsAndNotLocked_ContactReturned(
            DataAdapterProvider provider,
            IContactFactory contactFactory,
            IContact contact,
            LeaseOwner leaseOwner,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                provider.SaveContact(contact, new ContactSaveOptions(true, leaseOwner)).Should().BeTrue();

                var actual = provider.TryLoadContact(contact.Id, contactFactory, leaseOwner, TimeSpan.FromMinutes(1));

                actual.Status.Should().Be(LockAttemptStatus.Success);
                AssertContactsEqual(actual.Object, contact, l => l.Owner == leaseOwner && l.ExpirationTime > DateTime.UtcNow);
            }
        }
        public void DeleteContact_ContactExists_ContactRemoved(
            DataAdapterProvider provider,
            IContactFactory contactFactory,
            IContact contact,
            LeaseOwner leaseOwner,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                provider.SaveContact(contact, new ContactSaveOptions(true, leaseOwner)).Should().BeTrue();

                provider.DeleteContact(contact.Id);

                var actual = provider.TryLoadContact(contact.Id, contactFactory, leaseOwner, TimeSpan.FromMinutes(1));
                actual.Status.Should().Be(LockAttemptStatus.NotFound);
                actual.Object.Should().BeNull();
            }
        }
        public void ReleaseContact_ContactExistsAndLocked_ContactReleased(
            DataAdapterProvider provider,
            IContactFactory contactFactory,
            IContact contact,
            LeaseOwner them,
            LeaseOwner us,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                // lock contact for 'them'
                provider.SaveContact(contact, new ContactSaveOptions(false, them)).Should().BeTrue();

                provider.ReleaseContact(contact.Id, them);

                var actual = provider.TryLoadContact(contact.Id, contactFactory, us, TimeSpan.FromMinutes(1));
                actual.Status.Should().Be(LockAttemptStatus.Success);
                AssertContactsEqual(actual.Object, contact, l => l.Owner == us && l.ExpirationTime > DateTime.UtcNow);
            }
        }
        public void ObsoleteContact_ContactExistsAndNotLocked_ContactObsoleted(
            DataAdapterProvider provider,
            IContactFactory contactFactory,
            IContact contact,
            IContact successor,
            LeaseOwner leaseOwner,
            LeaseOwner us,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                // lock contact for 'them'
                provider.SaveContact(contact, new ContactSaveOptions(true, leaseOwner)).Should().BeTrue();

                provider.ObsoleteContact(contact.Id, leaseOwner, successor.Id);
                provider.SaveContact(successor, new ContactSaveOptions(true, leaseOwner)).Should().BeTrue();

                var actual = provider.LoadContactReadOnly(contact.Id, contactFactory);
                AssertContactsEqual(actual, successor);
            }
        }
 public override bool ObsoleteContact(ID contact, LeaseOwner leaseOwner, ID successor)
 {
    return this.contactService.Obsolete(
         contact.Guid,
         successor.Guid,
         this.modelMapper.Map<LeaseOwner, ElasticLeaseOwner>(leaseOwner),
         this.ctx);
 }
 public override bool SaveContact(ContactData contact, LeaseOwner leaseOwner, bool release)
 {
     throw new NotImplementedException();
 }
 public override bool ReleaseContact(ID id, LeaseOwner leaseOwner)
 {
     return this.contactService.Release(
         id.Guid,
         this.modelMapper.Map<LeaseOwner, ElasticLeaseOwner>(leaseOwner),
         this.ctx);
 }
 public override Sitecore.Analytics.DataAccess.LockAttemptResult<IContact> TryLoadContact(Guid contactId, LeaseOwner leaseOwner, TimeSpan leaseDuration)
 {
     throw new NotImplementedException();
 }
 public override bool ReleaseContact(Guid id, LeaseOwner leaseOwner)
 {
     throw new NotImplementedException();
 }
        public void TryLoadContactById_ContactDoesntExist_NullReturned(
            DataAdapterProvider provider,
            IContactFactory contactFactory,
            IContact contact,
            LeaseOwner leaseOwner,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                var actual = provider.TryLoadContact(contact.Id, contactFactory, leaseOwner, TimeSpan.FromMinutes(1));

                actual.Status.Should().Be(LockAttemptStatus.NotFound);
                actual.Object.Should().BeNull();
            }
        }
コード例 #18
0
        private bool ImportXdbContact(Models.Contact contact)
        {
            var leaseOwner = new LeaseOwner(GetType() + Guid.NewGuid().ToString(), LeaseOwnerType.OutOfRequestWorker);

            Contact xdbContact  = null;
            Guid    contactGuid = Guid.Empty;

            // Attempt to obtain an exclusive lock on an existing contact in xDB.
            LockAttemptResult <Contact> lockResult = _contactRepository.TryLoadContact(contact.Email, leaseOwner, TimeSpan.FromSeconds(30));

            // If contact is not found by email try by contactId
            if (lockResult.Status == LockAttemptStatus.NotFound)
            {
                if (Guid.TryParse(contact.ContactId, out contactGuid))
                {
                    lockResult = _contactRepository.TryLoadContact(contactGuid, leaseOwner, TimeSpan.FromSeconds(30));
                }
            }

            // If contact is still not found create a new contact
            if (lockResult.Status == LockAttemptStatus.NotFound)
            {
                // if contactGuid is NOT Guid.Empty use it or just generate new Guid
                xdbContact = XdbContactRepository.CreateContact(contactGuid != Guid.Empty ? contactGuid : Guid.NewGuid());

                //Setting these values allows processing and aggregation to process this contact without erroring.
                xdbContact.System.Value      = 0;
                xdbContact.System.VisitCount = 0;

                Log.Info(string.Format("{0} Creating new contact {1}", _logInfoPrefix, contact.ContactId), this);
            }
            else if (lockResult.Status == LockAttemptStatus.AlreadyLocked)
            {
                Log.Info(string.Format("{0} Contact {1} is locked and cannot be processed.", _logInfoPrefix, contact.ContactId), this);

                return(false);
            }
            else if (lockResult.Status == LockAttemptStatus.DatabaseUnavailable)
            {
                Log.Info(string.Format("{0} Database is unavailable. Contact {1} cannot be processed.", _logInfoPrefix, contact.ContactId), this);

                return(false);
            }
            else
            {
                xdbContact = lockResult.Object;

                Log.Info(string.Format("{0} Updating contact {1}", _logInfoPrefix, contact.ContactId), this);
            }

            // Update contact Identifier if needed
            if (xdbContact.Identifiers.IdentificationLevel != ContactIdentificationLevel.Known)
            {
                xdbContact.Identifiers.IdentificationLevel = Sitecore.Analytics.Model.ContactIdentificationLevel.Known;
            }
            if (xdbContact.Identifiers.Identifier != contact.Email)
            {
                xdbContact.Identifiers.Identifier = contact.Email;
            }

            // Personal facet
            var personalFacet = xdbContact.GetFacet <IContactPersonalInfo>("Personal");

            personalFacet.FirstName = personalFacet.FirstName != contact.FirstName ? contact.FirstName : personalFacet.FirstName;
            personalFacet.Surname   = personalFacet.Surname != contact.LastName ? contact.LastName : personalFacet.Surname;

            // Emails facet
            var emailsFacet = xdbContact.GetFacet <IContactEmailAddresses>("Emails");

            emailsFacet.Preferred = "Home";
            var email = emailsFacet.Entries.Contains("Home") ? emailsFacet.Entries["Home"] : emailsFacet.Entries.Create("Home");

            email.SmtpAddress = email.SmtpAddress != contact.Email ? contact.Email : email.SmtpAddress;

            // Addresses facet
            var addressesFacet = xdbContact.GetFacet <IContactAddresses>("Addresses");

            addressesFacet.Preferred = "Home";
            var address = addressesFacet.Entries.Contains("Billing") ? addressesFacet.Entries["Billing"] : addressesFacet.Entries.Create("Billing");

            address.Country    = address.Country != contact.Country ? contact.Country : address.Country;
            address.PostalCode = address.PostalCode != contact.PostalCode ? contact.PostalCode : address.PostalCode;

            return(XdbContactRepository.SaveContact(xdbContact, new ContactSaveOptions(true, leaseOwner)));
        }
        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 + "|");
        }
        public void TryExtendLock_ContactExistsLockedBySameOwner_LeaseExtended(
            DataAdapterProvider provider,
            IContact contact,
            LeaseOwner leaseOwner,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                provider.SaveContact(contact, new ContactSaveOptions(false, leaseOwner, TimeSpan.FromMinutes(1)))
                    .Should().Be(true);

                contact.Lease.Owner = leaseOwner;
                var res = provider.TryExtendContactLockLease(contact, TimeSpan.FromHours(1));

                res.Should().BeTrue();
                contact.Lease.Owner.Identifier.Should().Be(leaseOwner.Identifier);
            }
        }
        public void TryExtendLock_ContactExistsLockedByDifferentOwner_NotExtended(
            DataAdapterProvider provider,
            IContact contact,
            LeaseOwner them,
            LeaseOwner us,
            TestIndexUtils contactIndex)
        {
            using (contactIndex)
            {
                // lock by a different owner
                provider.SaveContact(contact, new ContactSaveOptions(false, them, TimeSpan.FromMinutes(1)))
                    .Should().Be(true);

                // extend it
                contact.Lease.Owner = us;
                var res = provider.TryExtendContactLockLease(contact, TimeSpan.FromHours(1));

                res.Should().BeFalse();
                // In Andes, owner is not updated to reflect the persistence layer, so we do the same here.
                contact.Lease.Owner.Identifier.Should().Be(us.Identifier);
            }
        }