private static DivvyContentMapping FindContentMapping(int divvyId)
        {
            var divvyContentMapping = DivvyContentMapping.FindFromDivvyContent(divvyId);

            if (divvyContentMapping == null)
            {
                DivvyLogManager.LogRequest("No Existing Content Found");
                return(null);
            }

            return(divvyContentMapping);
        }
        private static DivvyTypeMapping FindTypeMapping(string divvyContentType)
        {
            var mapping = Settings.GetMapping(divvyContentType);

            if (mapping == null)
            {
                DivvyLogManager.LogRequest("No Type Mapping Found");
                return(null);
            }
            else
            {
                DivvyLogManager.LogRequest("Found Type Mapping", new { Type = mapping.EpiserverPageTypeName });
                return(mapping);
            }
        }
        private static IContent CreateContent(ContentRequest contentRequest, DivvyTypeMapping mapping)
        {
            // Run the event. The target node might change in here
            var e = new DivvyEventArgs()
            {
                ContentRequest   = contentRequest,
                IntendedParent   = mapping.ParentNode,
                IntendedTypeName = mapping.EpiserverPageTypeName
            };

            OnBeforeContentCreation(null, e);

            if (e.CancelAction)
            {
                return(null);
            }

            var parent = repo.Get <PageData>(e.IntendedParent);

            DivvyLogManager.LogRequest($"Creating New Content", new { Type = e.IntendedTypeName, ParentId = parent.ContentGuid, ParentName = parent.Name });

            try
            {
                // Get the type ID. For whatever reason, we can't create content with a type name, we have to have the ID...
                var type = typeRepo.Load(e.IntendedTypeName);

                // Create the content
                var content = repo.GetDefault <IContent>(e.IntendedParent, type.ID);
                content.Name = contentRequest.Title;
                repo.Save(content, AccessLevel.NoAccess);

                // There's an edge case where we have a mappng already because the Episerver content got deleted
                if (!DivvyContentMapping.HasDivvyMapping(content.ContentLink.ID))
                {
                    DivvyContentMapping.Create(content.ContentLink.ID, contentRequest.Id);
                }

                DivvyLogManager.LogRequest("Created New Content", new { Id = content.ContentGuid });

                return(content);
            }
            catch (Exception ex)
            {
                DivvyLogManager.LogRequest($"Error Creating New Content", ex);
                return(null);
            }
        }
        // This is the main method of the class.
        // This processes the inbound Divvy information.
        public static ContentResponse ProcessDivvyInput(string input)
        {
            // Filter the raw string
            input = FilterInput(input);
            if (input == null)
            {
                // Explicit abandon
                return(null);
            }

            // Parse the string into an object
            var contentRequest = ParseContentRequest(input);

            if (contentRequest == null)
            {
                // Explicit abandon
                return(null);
            }

            // Attempt to find the content mapping
            var contentMapping = FindContentMapping(contentRequest.Id);

            // Do we have an archived mapping?
            // This ensures the same content isn't created twice
            if (contentMapping != null && contentMapping.Status == DivvyContentMapping.ArchivedStatusLabel)
            {
                DivvyLogManager.LogRequest("Found Archived Content Mapping", new { contentMapping.EpiserverContentId, contentMapping.ArchivedOn });
                return(null);
            }

            IContent content;

            if (contentMapping == null)
            {
                // No existing content; attempt to find a mapping
                var typeMapping = FindTypeMapping(contentRequest.ContentType);
                if (typeMapping == null)
                {
                    // No existing content and no mapping for new content
                    // We're done here...
                    return(null);
                }

                // We found a mapping, create new content
                content = CreateContent(contentRequest, typeMapping);

                if (content == null)
                {
                    // Content was short-circuited in an event handler
                    return(null);
                }
            }
            else
            {
                // We found a mapping. Retrieve the content
                var repo = ServiceLocator.Current.GetInstance <IContentRepository>();
                content = repo.Get <IContent>(new ContentReference(contentMapping.EpiserverContentId));

                if (content == null)
                {
                    // For some reason, this content doesn't exist
                    // Archive the mapping
                    DivvyLogManager.LogRequest("Unable to Find Mapped Content", new { EpiserverID = contentMapping.EpiserverContentId });
                    DivvyContentMapping.Archive(contentMapping);
                    return(null);
                }

                DivvyLogManager.LogRequest($"Found Episerver Content", new { Id = content.ContentGuid });
            }

            // If we get to this point, we should have content, one way or the other -- either found or created

            // Get and filter the HTML we're sending back for a preview
            var previewHtml = Settings.PreviewProvider(content);

            previewHtml = FilterPreviewHtml(previewHtml);

            // Get the URL for the content in Edit Mode
            var editUrl = GetEditUrl(content);

            // Get the calculated last modified date
            var lastModified = GetLastModifiedDate(content);

            return(new ContentResponse()
            {
                Id = content.ContentGuid,
                PreviewHtml = previewHtml,
                EditUrl = editUrl,
                LastModified = lastModified,
                Media = GetMedia(content).Select(m => new Media()
                {
                    Name = m.Name,
                    Url = resolver.GetUrl(m)
                }).ToList()
            });
        }