Ejemplo n.º 1
0
        /// <summary>
        /// Extracts and returns a collection of topics from the query's results
        /// </summary>
        private List <Topic> GetDocsTopics(List <ItemEdge> results)
        {
            var topics = new List <Topic>();

            if (results != null)
            {
                foreach (ItemEdge edge in results)
                {
                    Page page = edge.Node as Page;
                    if (page == null)
                    {
                        Log.Debug("Node not is not a Page, skipping.");
                        continue;
                    }

                    int          docsPublicationId = (int)edge.Node.PublicationId;
                    Localization docsLocalization  = new DocsLocalization(docsPublicationId);
                    docsLocalization.EnsureInitialized();

                    // Deserialize Page Content as R2 Data Model
                    string        pageModelJson = page.RawContent.Content;
                    PageModelData pageModelData = JsonConvert.DeserializeObject <PageModelData>(pageModelJson, DataModelBinder.SerializerSettings);

                    // Extract the R2 Data Model of the Topic and convert it to a Strongly Typed View Model
                    EntityModelData topicModelData = pageModelData.Regions[0].Entities[0];
                    EntityModel     topicModel     = ModelBuilderPipeline.CreateEntityModel(topicModelData, null, docsLocalization);

                    Topic topic = topicModel as Topic;
                    if (topic == null)
                    {
                        Log.Warn($"Unexpected View Model type for {topicModel}: '{topicModel.GetType().FullName}'");
                        continue;
                    }

                    // Post-process the Strongly Typed Topic
                    topic.Id   = topicModelData.XpmMetadata["ComponentID"] as string;
                    topic.Link = GetFullyQualifiedUrlForTopic(topicModelData.LinkUrl);

                    topics.Add(topic);
                }
            }

            return(topics);
        }
        protected virtual PageModel LoadPageModel(int pageId, bool addIncludes, ILocalization localization)
        {
            using (new Tracer(pageId, addIncludes, localization))
            {
                PageModelData pageModelData = SiteConfiguration.ModelServiceProvider.GetPageModelData(pageId, localization, addIncludes);

                if (pageModelData == null)
                {
                    throw new DxaItemNotFoundException($"Page not found for publication id {localization.Id} and page id {pageId}");
                }

                if (pageModelData.MvcData == null)
                {
                    throw new DxaException($"Data Model for Page '{pageModelData.Title}' ({pageModelData.Id}) contains no MVC data. Ensure that the Page is published using the DXA R2 TBBs.");
                }

                return(ModelBuilderPipeline.CreatePageModel(pageModelData, addIncludes, localization));
            }
        }
        protected virtual EntityModel LoadEntityModel(string id, ILocalization localization)
        {
            using (new Tracer(id, localization))
            {
                EntityModelData entityModelData = SiteConfiguration.ModelServiceProvider.GetEntityModelData(id, localization);

                if (entityModelData == null)
                {
                    throw new DxaItemNotFoundException(id);
                }

                EntityModel result = ModelBuilderPipeline.CreateEntityModel(entityModelData, null, localization);

                if (result.XpmMetadata != null)
                {
                    // Entity Models requested through this method are per definition "query based" in XPM terminology.
                    result.XpmMetadata["IsQueryBased"] = true; // TODO TSI-24: Do this in Model Service (or CM-side?)
                }

                return(result);
            }
        }
        public void BuildEntityModel_ThroughModelBuilderPipeline_Success()
        {
            Localization testLocalization = new DocsLocalization();

            testLocalization.EnsureInitialized();

            string       testTopicId  = "1612-1970";
            string       testTitle    = "DITA title";
            string       testBody     = "<div class=\"section \">First section</div><div class=\"section \">Second section</div>";
            GenericTopic genericTopic = new GenericTopic
            {
                TopicTitle = "<Test topic title>",
                TopicBody  = $"<h1 class=\"title \">{testTitle}</h1><div class=\"body \">{testBody}</div>"
            };

            PageModelData testPageModelData = new PageModelData
            {
                Id      = "666",
                Regions = new List <RegionModelData>
                {
                    new RegionModelData
                    {
                        Name     = "Main",
                        Entities = new List <EntityModelData>
                        {
                            new EntityModelData
                            {
                                Id       = testTopicId,
                                SchemaId = "1",  // Tridion Docs uses a hard-coded/fake Schema ID.
                                Content  = new ContentModelData
                                {
                                    { "topicTitle", genericTopic.TopicTitle },
                                    { "topicBody", genericTopic.TopicBody }
                                },
                                MvcData = new DataModel.MvcData
                                {
                                    AreaName = "Ish",
                                    ViewName = "Topic"
                                }
                            }
                        },
                        MvcData = new DataModel.MvcData
                        {
                            AreaName = "Core", // TODO: test with ISH data
                            ViewName = "Main"
                        }
                    }
                },
                MvcData = new DataModel.MvcData
                {
                    AreaName = "Test",
                    ViewName = "SimpleTestPage"
                }
            };

            PageModel pageModel = ModelBuilderPipeline.CreatePageModel(testPageModelData, false, testLocalization);

            Assert.IsNotNull(pageModel, "pageModel");

            OutputJson(pageModel);

            TestStronglyTypedTopic result = pageModel.Regions["Main"].Entities[0] as TestStronglyTypedTopic;

            Assert.IsNotNull(result);

            Assert.AreEqual(testTopicId, result.Id, "result.Id");
            Assert.AreEqual(genericTopic.TopicTitle, result.TopicTitle, "result.TopicTitle");
            Assert.AreEqual(testTitle, result.Title, "result.Title");
            Assert.AreEqual(testBody, result.BodyRichText.ToString(), "result.BodyRichText.ToString()");
            Assert.AreEqual("First section", result.FirstSection, "result.FirstSection");
            Assert.AreEqual(result.GetDefaultView(null), result.MvcData, "result.MvcData");
        }