Exemple #1
0
        /// <summary>
        /// Performs the Transform.
        /// </summary>
        public override void Transform(Engine engine, Package package)
        {
            Logger.Debug("Transform");

            Initialize(engine, package);

            int expandLinkDepth;

            package.TryGetParameter("expandLinkDepth", out expandLinkDepth, Logger);

            string[] modelBuilderTypeNames = GetModelBuilderTypeNames();

            RenderedItem renderedItem = Engine.PublishingContext.RenderedItem;
            Page         page         = GetPage();

            try
            {
                DataModelBuilderSettings settings = new DataModelBuilderSettings
                {
                    ExpandLinkDepth     = expandLinkDepth,
                    GenerateXpmMetadata = IsXpmEnabled || IsPreview,
                    Locale = GetLocale()
                };

                DataModelBuilderPipeline modelBuilderPipeline = new DataModelBuilderPipeline(renderedItem, settings, modelBuilderTypeNames);
                PageModelData            pageModel            = modelBuilderPipeline.CreatePageModel(page);
                OutputJson = JsonSerialize(pageModel, IsPreview, DataModelBinder.SerializerSettings);
            }
            catch (Exception ex)
            {
                throw new DxaException($"An error occurred while rendering {page.FormatIdentifier()}", ex);
            }
        }
        public void DataPresentationTemplate_Success()
        {
            Page dummyPage = (Page)TestSession.GetObject(TestFixture.AutoTestParentHomePageWebDavUrl);

            RenderedItem renderedItem = CreateTestRenderedItem(dummyPage, dummyPage.PageTemplate);

            DataModelBuilderPipeline testModelBuilderPipeline = new DataModelBuilderPipeline(
                renderedItem,
                _defaultModelBuilderSettings,
                _defaultModelBuilderTypeNames,
                new ConsoleLogger()
                );

            ComponentTemplate dataPresentationTemplate = testModelBuilderPipeline.DataPresentationTemplate;

            Assert.IsNotNull(dataPresentationTemplate, "dataPresentationTemplate");
        }
        public void DataPresentationTemplate_FoundAndCached_Success()
        {
            using (Session testSessionWithCache = new Session())
            {
                SimpleCache testCache = new SimpleCache();
                testSessionWithCache.Cache = testCache;

                Page dummyPage = (Page)testSessionWithCache.GetObject(TestFixture.AutoTestParentHomePageWebDavUrl);

                RenderedItem renderedItem = CreateTestRenderedItem(dummyPage, dummyPage.PageTemplate);
                TestLogger   testLogger   = new TestLogger();

                DataModelBuilderPipeline testModelBuilderPipeline = new DataModelBuilderPipeline(
                    renderedItem,
                    _defaultModelBuilderSettings,
                    _defaultModelBuilderTypeNames,
                    testLogger
                    );

                ComponentTemplate dataPresentationTemplate = testModelBuilderPipeline.DataPresentationTemplate;
                Assert.IsNotNull(dataPresentationTemplate, "dataPresentationTemplate");

                SimpleCacheRegion dxaCacheRegion;
                Assert.IsTrue(testCache.Regions.TryGetValue("DXA", out dxaCacheRegion), "DXA Cache Region not found.");
                ComponentTemplate cachedComponentTemplate = dxaCacheRegion.Get("DataPresentationTemplate") as ComponentTemplate;
                Assert.AreEqual(dataPresentationTemplate, cachedComponentTemplate, "cachedComponentTemplate");

                // Create new DataModelBuilderPipeline for same Session; DataPresentationTemplate should be obtained from cache.
                testModelBuilderPipeline = new DataModelBuilderPipeline(
                    renderedItem,
                    _defaultModelBuilderSettings,
                    _defaultModelBuilderTypeNames,
                    testLogger
                    );

                ComponentTemplate dataPresentationTemplate2 = testModelBuilderPipeline.DataPresentationTemplate;
                Assert.AreEqual(dataPresentationTemplate, dataPresentationTemplate2, "dataPresentationTemplate2");

                Assert.IsTrue(
                    testLogger.LoggedMessages.Contains(new LogMessage(LogLevel.Debug, "Obtained Data Presentation Template from cache.")),
                    "Expected Log message not found."
                    );
            }
        }
Exemple #4
0
        /// <summary>
        /// Performs the Transform.
        /// </summary>
        public override void Transform(Engine engine, Package package)
        {
            Logger.Debug("Transform");

            Initialize(engine, package);

            bool includeComponentTemplateData;

            if (!package.TryGetParameter("includeComponentTemplateData", out includeComponentTemplateData, Logger))
            {
                includeComponentTemplateData = true; // Default
            }

            int expandLinkDepth;

            package.TryGetParameter("expandLinkDepth", out expandLinkDepth, Logger);

            string[] modelBuilderTypeNames = GetModelBuilderTypeNames();

            RenderedItem      renderedItem = Engine.PublishingContext.RenderedItem;
            Component         component    = GetComponent();
            ComponentTemplate ct           = GetComponentTemplate();

            try
            {
                DataModelBuilderSettings settings = new DataModelBuilderSettings
                {
                    ExpandLinkDepth     = expandLinkDepth,
                    GenerateXpmMetadata = IsXpmEnabled || IsPreview
                };

                DataModelBuilderPipeline modelBuilderPipeline = new DataModelBuilderPipeline(renderedItem, settings, modelBuilderTypeNames);
                EntityModelData          entityModel          = modelBuilderPipeline.CreateEntityModel(component, includeComponentTemplateData ? ct : null);

                string entityModelJson = JsonSerialize(entityModel, IsPreview, DataModelBinder.SerializerSettings);
                Item   outputItem      = Package.CreateStringItem(ContentType.Text, entityModelJson);
                Package.PushItem(Package.OutputName, outputItem);
            }
            catch (Exception ex)
            {
                throw new DxaException($"An error occurred while rendering {component.FormatIdentifier()} with {ct.FormatIdentifier()}", ex);
            }
        }
        public void DataPresentationTemplate_NotFound_Success()
        {
            Page dummyPage = (Page)TestSession.GetObject(TestFixture.AutoTestChildHomePageWebDavUrl);

            RenderedItem renderedItem = CreateTestRenderedItem(dummyPage, dummyPage.PageTemplate);
            TestLogger   testLogger   = new TestLogger();

            DataModelBuilderPipeline testModelBuilderPipeline = new DataModelBuilderPipeline(
                renderedItem,
                _defaultModelBuilderSettings,
                _defaultModelBuilderTypeNames,
                testLogger
                );

            ComponentTemplate dataPresentationTemplate = testModelBuilderPipeline.DataPresentationTemplate;

            Assert.IsNull(dataPresentationTemplate, "dataPresentationTemplate");
            Assert.IsTrue(
                testLogger.LoggedMessages.Contains(new LogMessage(LogLevel.Warning, "Component Template 'Generate Data Presentation' not found.")),
                "Expected Log message not found."
                );
        }
        private EntityModelData CreateEntityModel(Component component, ComponentTemplate ct, out RenderedItem renderedItem, IEnumerable <string> modelBuilderTypeNames = null)
        {
            renderedItem = CreateTestRenderedItem(component, ct);

            if (modelBuilderTypeNames == null)
            {
                modelBuilderTypeNames = _defaultModelBuilderTypeNames;
            }

            DataModelBuilderPipeline testModelBuilderPipeline = new DataModelBuilderPipeline(
                renderedItem,
                _defaultModelBuilderSettings,
                modelBuilderTypeNames,
                new TestLogger()
                );

            EntityModelData result = testModelBuilderPipeline.CreateEntityModel(component, ct);

            Assert.IsNotNull(result);
            OutputJson(result, DataModelBinder.SerializerSettings);

            return(result);
        }
        private PageModelData CreatePageModel(Page page, out RenderedItem renderedItem, IEnumerable <string> modelBuilderTypeNames = null)
        {
            renderedItem = CreateTestRenderedItem(page, page.PageTemplate);

            if (modelBuilderTypeNames == null)
            {
                modelBuilderTypeNames = _defaultModelBuilderTypeNames;
            }

            DataModelBuilderPipeline testModelBuilderPipeline = new DataModelBuilderPipeline(
                renderedItem,
                _defaultModelBuilderSettings,
                modelBuilderTypeNames,
                new TestLogger()
                );

            PageModelData result = testModelBuilderPipeline.CreatePageModel(page);

            Assert.IsNotNull(result);
            OutputJson(result, DataModelBinder.SerializerSettings);

            return(result);
        }