Ejemplo n.º 1
0
        /// <summary>
        /// Persists the Session History
        /// </summary>
        /// <param name="context">Context used to save the session history</param>
        /// <param name="sessionHistory">Session History object</param>
        public Guid PersistSessionHistory(OrganizationServiceContext context, SessionHistory sessionHistory)
        {
            if (sessionHistory == null)
            {
                return(Guid.Empty);
            }

            var webFormSession = sessionHistory.Id != Guid.Empty ? context.CreateQuery("adx_webformsession").FirstOrDefault(s => s.GetAttributeValue <OptionSetValue>("statecode") != null && s.GetAttributeValue <OptionSetValue>("statecode").Value == 0 && s.GetAttributeValue <Guid>("adx_webformsessionid") == sessionHistory.Id) : null;

            var addNew = webFormSession == null;

            if (addNew)
            {
                webFormSession = new Entity("adx_webformsession");

                if (sessionHistory.WebFormId != Guid.Empty)
                {
                    webFormSession.Attributes["adx_webform"] = new EntityReference("adx_webform", sessionHistory.WebFormId);
                }
            }

            if (sessionHistory.PrimaryRecord != null && sessionHistory.PrimaryRecord.ID != Guid.Empty)
            {
                webFormSession.Attributes["adx_primaryrecordid"] = sessionHistory.PrimaryRecord.ID.ToString();
            }

            if (sessionHistory.CurrentStepId != Guid.Empty)
            {
                webFormSession.Attributes["adx_currentwebformstep"] = new EntityReference("adx_webformstep", sessionHistory.CurrentStepId);
            }

            if (sessionHistory.PrimaryRecord != null && !string.IsNullOrWhiteSpace(sessionHistory.PrimaryRecord.LogicalName))
            {
                webFormSession.Attributes["adx_primaryrecordentitylogicalname"] = sessionHistory.PrimaryRecord.LogicalName;
            }

            if (sessionHistory.PrimaryRecord != null && !string.IsNullOrWhiteSpace(sessionHistory.PrimaryRecord.PrimaryKeyLogicalName))
            {
                webFormSession.Attributes["adx_primaryrecordentitykeyname"] = sessionHistory.PrimaryRecord.PrimaryKeyLogicalName;
            }

            webFormSession.Attributes["adx_currentstepindex"] = sessionHistory.CurrentStepIndex;

            if (sessionHistory.ContactId != Guid.Empty)
            {
                webFormSession.Attributes["adx_contact"] = new EntityReference("contact", sessionHistory.ContactId);
            }

            if (sessionHistory.QuoteId != Guid.Empty)
            {
                webFormSession.Attributes["adx_quoteid"] = new EntityReference("quote", sessionHistory.QuoteId);
            }

            if (sessionHistory.SystemUserId != Guid.Empty)
            {
                webFormSession.Attributes["adx_systemuser"] = new EntityReference("systemuser", sessionHistory.SystemUserId);
            }

            if (!string.IsNullOrWhiteSpace(sessionHistory.AnonymousIdentification))
            {
                webFormSession.Attributes["adx_anonymousidentification"] = sessionHistory.AnonymousIdentification;
            }

            webFormSession.Attributes["adx_stephistory"] = ConvertListToJsonString(sessionHistory.StepHistory);

            if (!string.IsNullOrWhiteSpace(sessionHistory.UserHostAddress))
            {
                webFormSession.Attributes["adx_userhostaddress"] = sessionHistory.UserHostAddress;
            }

            if (!string.IsNullOrWhiteSpace(sessionHistory.UserIdentityName))
            {
                webFormSession.Attributes["adx_useridentityname"] = sessionHistory.UserIdentityName;
            }

            if (addNew)
            {
                context.AddObject(webFormSession);
            }
            else
            {
                context.UpdateObject(webFormSession);
            }

            context.SaveChanges();

            return(webFormSession.Id);
        }
Ejemplo n.º 2
0
        private static SessionHistory GetSessionHistory(Entity webFormSession)
        {
            if (webFormSession == null)
            {
                return(null);
            }

            var sessionHistory = new SessionHistory
            {
                Id        = webFormSession.Id,
                WebFormId = webFormSession.GetAttributeValue <EntityReference>("adx_webform") == null ? Guid.Empty : webFormSession.GetAttributeValue <EntityReference>("adx_webform").Id
            };

            var currentStep = webFormSession.GetAttributeValue <EntityReference>("adx_currentwebformstep");

            if (currentStep == null)
            {
                throw new ApplicationException("adx_webformsession.adx_currentwebformstep is null.");
            }

            sessionHistory.CurrentStepId = currentStep.Id;

            var currentStepIndex = webFormSession.GetAttributeValue <int?>("adx_currentstepindex");

            var stepIndex = currentStepIndex ?? 0;

            if (currentStepIndex == null)
            {
                throw new ApplicationException("adx_webformsession.adx_currentwebformstep is null.");
            }

            sessionHistory.CurrentStepIndex = stepIndex;

            Guid recordGuid;
            var  recordid = webFormSession.GetAttributeValue <string>("adx_primaryrecordid");

            sessionHistory.PrimaryRecord = new SessionHistory.ReferenceEntity();

            if (!string.IsNullOrWhiteSpace(recordid) && Guid.TryParse(recordid, out recordGuid))
            {
                sessionHistory.PrimaryRecord.ID = recordGuid;
            }

            sessionHistory.PrimaryRecord.LogicalName = webFormSession.GetAttributeValue <string>("adx_primaryrecordentitylogicalname") ?? string.Empty;

            sessionHistory.PrimaryRecord.PrimaryKeyLogicalName = webFormSession.GetAttributeValue <string>("adx_primaryrecordentitykeyname") ?? string.Empty;

            var contact = webFormSession.GetAttributeValue <EntityReference>("adx_contact");

            sessionHistory.ContactId = contact != null ? contact.Id : Guid.Empty;

            var quote = webFormSession.GetAttributeValue <EntityReference>("adx_quoteid");

            sessionHistory.QuoteId = quote != null ? quote.Id : Guid.Empty;

            var systemUser = webFormSession.GetAttributeValue <EntityReference>("adx_systemuser");

            sessionHistory.SystemUserId = systemUser != null ? systemUser.Id : Guid.Empty;

            sessionHistory.AnonymousIdentification = webFormSession.GetAttributeValue <string>("adx_anonymousidentification") ?? string.Empty;

            sessionHistory.StepHistory = ConvertJsonStringToList(webFormSession.GetAttributeValue <string>("adx_stephistory"));

            sessionHistory.UserHostAddress = webFormSession.GetAttributeValue <string>("adx_userhostaddress") ?? string.Empty;

            sessionHistory.UserIdentityName = webFormSession.GetAttributeValue <string>("adx_useridentityname") ?? string.Empty;

            return(sessionHistory);
        }