public static Dynamic.ComponentPresentation BuildComponentPresentation(TCM.ComponentPresentation tcmComponentPresentation, Engine engine, BuildManager manager)
        {
            TemplatingLogger logger = TemplatingLogger.GetLogger(typeof(ComponentPresentationBuilder));

            Dynamic.ComponentPresentation cp = new Dynamic.ComponentPresentation();

            logger.Debug(string.Format(">BuildCP {0} ({1})", tcmComponentPresentation.ComponentTemplate.Title, tcmComponentPresentation.ComponentTemplate.IsRepositoryPublishable));
            if (tcmComponentPresentation.ComponentTemplate.IsRepositoryPublishable)
            {
                // call render but ignore the output - render ensures componentlinking will be setup as normal.
                // don't bother with page flags because the end result is dynamically published so it needs to run with DCP settings
                engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);

                // ignore the rendered CP, because it is already available in the broker
                // instead, we will render a very simple version without any links
                cp.Component = manager.BuildComponent(tcmComponentPresentation.Component, 0); // linkLevel = 0 means: only summarize the component
                cp.IsDynamic = true;
            }
            else
            {
                // render the component presentation using its own CT
                // but first, set a parameter in the context so that the CT will know it is beng called
                // from a DynamicDelivery page template
                if (engine.PublishingContext.RenderContext != null && !engine.PublishingContext.RenderContext.ContextVariables.Contains(BasePageTemplate.VariableNameCalledFromDynamicDelivery))
                {
                    engine.PublishingContext.RenderContext.ContextVariables.Add(BasePageTemplate.VariableNameCalledFromDynamicDelivery, BasePageTemplate.VariableValueCalledFromDynamicDelivery);
                }

                string renderedContent = engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);
                renderedContent = TridionUtils.StripTcdlTags(renderedContent);

                // rendered content could contain si4t search data. if that's the case, the value of renderedCotnent is not valid DD4T data.
                // lets remove the si4t search data if that's the case.
                string dd4tData = Si4tUtils.RemoveSearchData(renderedContent);

                try
                {
                    // we cannot be sure the component template uses the same serializer service as the page template
                    // so we will call a factory which can detect the correct service based on the content
                    ISerializerService serializerService = SerializerServiceFactory.FindSerializerServiceForContent(dd4tData);
                    cp = serializerService.Deserialize <Dynamic.ComponentPresentation>(dd4tData);

                    // inital renderedContent could contain si4t search data. we need to preserve the search data.
                    // lets retrieve the si4t search data if that's the case and added to the renderedContent property
                    cp.RenderedContent = Si4tUtils.RetrieveSearchData(renderedContent);
                }
                catch (Exception e)
                {
                    log.Error("exception while deserializing into CP", e);
                    // the component presentation could not be deserialized, this probably not a Dynamic Delivery template
                    // just store the output as 'RenderedContent' on the CP
                    cp.RenderedContent = renderedContent;
                    // because the CT was not a DD4T CT, we will generate the DD4T XML code here
                    cp.Component = manager.BuildComponent(tcmComponentPresentation.Component);
                }
                cp.IsDynamic = false;
            }
            cp.ComponentTemplate = manager.BuildComponentTemplate(tcmComponentPresentation.ComponentTemplate);
            return(cp);
        }
        private Dynamic.ComponentPresentation GetDynamicComponentPresentation()
        {
            Template template = Engine.PublishingContext.ResolvedItem.Template;

            if (!(template is ComponentTemplate))
            {
                Log.Error("no component template found (is this a page template?)");
                return(null);
            }
            ComponentTemplate tcmComponentTemplate = (ComponentTemplate)template;
            Item item = Package.GetByName(Package.ComponentName);

            if (item == null)
            {
                Log.Error("no component found (is this a page template?)");
                return(null);
            }
            Component tcmComponent = (Component)Engine.GetObject(item);

            Dynamic.Component component = Manager.BuildComponent(tcmComponent);
            EnsureExtraProperties(component, tcmComponent);
            Dynamic.ComponentTemplate     componentTemplate     = Manager.BuildComponentTemplate(tcmComponentTemplate);
            Dynamic.ComponentPresentation componentPresentation = new Dynamic.ComponentPresentation()
            {
                Component = component, ComponentTemplate = componentTemplate, IsDynamic = tcmComponentTemplate.IsRepositoryPublishable
            };

            return(componentPresentation);
        }
        public override Dd4tComponentPresentation BuildComponentPresentation(TcmComponentPresentation tcmComponentPresentation, Engine engine,
                                                                             int linkLevels, bool resolveWidthAndHeight)
        {
            Dd4tComponentPresentation result = base.BuildComponentPresentation(tcmComponentPresentation, engine, linkLevels, resolveWidthAndHeight);

            AddCpExtensionData(result, tcmComponentPresentation);
            return(result);
        }
        public static Dynamic.ComponentPresentation BuildComponentPresentation(TCM.ComponentPresentation tcmComponentPresentation, Engine engine, int linkLevels, bool resolveWidthAndHeight, BuildManager manager)
        {
            Dynamic.ComponentPresentation cp = new Dynamic.ComponentPresentation();

            if (tcmComponentPresentation.ComponentTemplate.IsRepositoryPublishable)
            {
                // call render but ignore the output - render ensures componentlinking will be setup as normal.
                // don't bother with page flags because the end result is dynamically published so it needs to run with DCP settings
                engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);

                // ignore the rendered CP, because it is already available in the broker
                // instead, we will render a very simple version without any links
                cp.Component = manager.BuildComponent(tcmComponentPresentation.Component, 0, false,false); // linkLevels = 0 means: only summarize the component
                cp.IsDynamic = true;
            }
            else
            {
                // render the component presentation using its own CT
                // but first, set a parameter in the context so that the CT will know it is beng called
                // from a DynamicDelivery page template
                if (engine.PublishingContext.RenderContext != null && !engine.PublishingContext.RenderContext.ContextVariables.Contains(BasePageTemplate.VariableNameCalledFromDynamicDelivery))
                {
                    engine.PublishingContext.RenderContext.ContextVariables.Add(BasePageTemplate.VariableNameCalledFromDynamicDelivery, BasePageTemplate.VariableValueCalledFromDynamicDelivery);
                }

                string renderedContent = engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);
                engine.PublishingContext.RenderContext.ContextVariables.Remove(BasePageTemplate.VariableNameCalledFromDynamicDelivery);

                renderedContent = TridionUtils.StripTcdlTags(renderedContent);

                cp.IsDynamic = false;
                TextReader tr = new StringReader(renderedContent);
                if (serializer == null)
                {
                    serializer = new ComponentSerializer();
                    //serializer = new XmlSerializerFactory().CreateSerializer(typeof(Dynamic.Component));
                }
                try
                {
                    cp.Component = (Dynamic.Component)serializer.Deserialize(tr);
                }
                catch (Exception e)
                {
                    TemplatingLogger.GetLogger(typeof(ComponentPresentationBuilder)).Error("exception while deserializing into CP: " + e.Message);                    
                    // the component presentation could not be deserialized, this probably not a Dynamic Delivery template
                    // just store the output as 'RenderedContent' on the CP
                    cp.RenderedContent = renderedContent;
                    // because the CT was not a DD4T CT, we will generate the DD4T XML code here
                    cp.Component = manager.BuildComponent(tcmComponentPresentation.Component);
                }
            }
            cp.ComponentTemplate = manager.BuildComponentTemplate(tcmComponentPresentation.ComponentTemplate);
            return cp;
        }
        public static Dynamic.ComponentPresentation BuildComponentPresentation(TCM.ComponentPresentation tcmComponentPresentation, Engine engine, int linkLevels, bool resolveWidthAndHeight, BuildManager manager)
        {
            Dynamic.ComponentPresentation cp = new Dynamic.ComponentPresentation();

            if (tcmComponentPresentation.ComponentTemplate.IsRepositoryPublishable)
            {
                // call render but ignore the output - render ensures componentlinking will be setup as normal.
                // don't bother with page flags because the end result is dynamically published so it needs to run with DCP settings
                engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);

                // ignore the rendered CP, because it is already available in the broker
                // instead, we will render a very simple version without any links
                cp.Component = manager.BuildComponent(tcmComponentPresentation.Component, 0, false, false); // linkLevels = 0 means: only summarize the component
                cp.IsDynamic = true;
            }
            else
            {
                // render the component presentation using its own CT
                // but first, set a parameter in the context so that the CT will know it is beng called
                // from a DynamicDelivery page template
                if (engine.PublishingContext.RenderContext != null && !engine.PublishingContext.RenderContext.ContextVariables.Contains(BasePageTemplate.VariableNameCalledFromDynamicDelivery))
                {
                    engine.PublishingContext.RenderContext.ContextVariables.Add(BasePageTemplate.VariableNameCalledFromDynamicDelivery, BasePageTemplate.VariableValueCalledFromDynamicDelivery);
                }

                string renderedContent = engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);
                engine.PublishingContext.RenderContext.ContextVariables.Remove(BasePageTemplate.VariableNameCalledFromDynamicDelivery);

                renderedContent = TridionUtils.StripTcdlTags(renderedContent);

                cp.IsDynamic = false;
                TextReader tr = new StringReader(renderedContent);
                if (serializer == null)
                {
                    serializer = new ComponentSerializer();
                    //serializer = new XmlSerializerFactory().CreateSerializer(typeof(Dynamic.Component));
                }
                try
                {
                    cp.Component = (Dynamic.Component)serializer.Deserialize(tr);
                }
                catch (Exception e)
                {
                    TemplatingLogger.GetLogger(typeof(ComponentPresentationBuilder)).Error("exception while deserializing into CP: " + e.Message);
                    // the component presentation could not be deserialized, this probably not a Dynamic Delivery template
                    // just store the output as 'RenderedContent' on the CP
                    cp.RenderedContent = renderedContent;
                    // because the CT was not a DD4T CT, we will generate the DD4T XML code here
                    cp.Component = manager.BuildComponent(tcmComponentPresentation.Component);
                }
            }
            cp.ComponentTemplate = manager.BuildComponentTemplate(tcmComponentPresentation.ComponentTemplate);
            return(cp);
        }
Exemplo n.º 6
0
        public static Dynamic.Page BuildPage(TCM.Page tcmPage, Engine engine, BuildManager manager, int linkLevels, bool resolveWidthAndHeight, bool publishEmptyFields)
        {
            Dynamic.Page p = new Dynamic.Page
            {
                Title          = tcmPage.Title,
                Id             = tcmPage.Id.ToString(),
                Filename       = tcmPage.FileName,
                PageTemplate   = manager.BuildPageTemplate(tcmPage.PageTemplate),
                Schema         = manager.BuildSchema(tcmPage.MetadataSchema),
                Version        = tcmPage.Version,
                RevisionDate   = tcmPage.RevisionDate,
                MetadataFields = new Dynamic.FieldSet()
            };
            if (linkLevels > 0)
            {
                try
                {
                    if (tcmPage.Metadata != null && tcmPage.MetadataSchema != null)
                    {
                        var tcmMetadataFields = new Tridion.ContentManager.ContentManagement.Fields.ItemFields(tcmPage.Metadata, tcmPage.MetadataSchema);
                        p.MetadataFields = manager.BuildFields(tcmMetadataFields);
                    }
                }
                catch (Exception)
                {
                    // fail silently if there is no metadata schema
                }
            }

            p.ComponentPresentations = new List <Dynamic.ComponentPresentation>();
            foreach (TCM.ComponentPresentation cp in tcmPage.ComponentPresentations)
            {
                Dynamic.ComponentPresentation dynCp = manager.BuildComponentPresentation(cp, engine, linkLevels - 1, resolveWidthAndHeight);
                p.ComponentPresentations.Add(dynCp);
            }
            p.StructureGroup = manager.BuildOrganizationalItem((TCM.StructureGroup)tcmPage.OrganizationalItem);

            if (!manager.BuildProperties.OmitContextPublications)
            {
                p.Publication = manager.BuildPublication(tcmPage.ContextRepository);
            }
            if (!manager.BuildProperties.OmitOwningPublications)
            {
                p.OwningPublication = manager.BuildPublication(tcmPage.OwningRepository);
            }
            if (!manager.BuildProperties.OmitCategories)
            {
                p.Categories = manager.BuildCategories(tcmPage);
            }
            manager.AddXpathToFields(p.MetadataFields, "Metadata");
            return(p);
        }
        public static Dynamic.ComponentPresentation BuildComponentPresentation(TCM.ComponentPresentation tcmComponentPresentation, Engine engine, BuildManager manager)
        {
            TemplatingLogger logger = TemplatingLogger.GetLogger(typeof(ComponentPresentationBuilder));
            Dynamic.ComponentPresentation cp = new Dynamic.ComponentPresentation();

            logger.Debug(string.Format(">BuildCP {0} ({1})", tcmComponentPresentation.ComponentTemplate.Title, tcmComponentPresentation.ComponentTemplate.IsRepositoryPublishable));
            if (tcmComponentPresentation.ComponentTemplate.IsRepositoryPublishable)
            {
                // call render but ignore the output - render ensures componentlinking will be setup as normal.
                // don't bother with page flags because the end result is dynamically published so it needs to run with DCP settings
                engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);

                // ignore the rendered CP, because it is already available in the broker
                // instead, we will render a very simple version without any links
                cp.Component = manager.BuildComponent(tcmComponentPresentation.Component, 0); // linkLevel = 0 means: only summarize the component
                cp.IsDynamic = true;
            }
            else
            {
                // render the component presentation using its own CT
                // but first, set a parameter in the context so that the CT will know it is beng called
                // from a DynamicDelivery page template
                if (engine.PublishingContext.RenderContext != null && !engine.PublishingContext.RenderContext.ContextVariables.Contains(BasePageTemplate.VariableNameCalledFromDynamicDelivery))
                {
                    engine.PublishingContext.RenderContext.ContextVariables.Add(BasePageTemplate.VariableNameCalledFromDynamicDelivery, BasePageTemplate.VariableValueCalledFromDynamicDelivery);
                }

                string renderedContent = engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);
                renderedContent = TridionUtils.StripTcdlTags(renderedContent);
     
                try
                {
                    // we cannot be sure the component template uses the same serializer service as the page template
                    // so we will call a factory which can detect the correct service based on the content
                    ISerializerService serializerService = SerializerServiceFactory.FindSerializerServiceForContent(renderedContent);
                    cp = serializerService.Deserialize<Dynamic.ComponentPresentation>(renderedContent);
                }
                catch (Exception e)
                {
                    log.Error("exception while deserializing into CP", e);
                    // the component presentation could not be deserialized, this probably not a Dynamic Delivery template
                    // just store the output as 'RenderedContent' on the CP
                    cp.RenderedContent = renderedContent;
                    // because the CT was not a DD4T CT, we will generate the DD4T XML code here
                    cp.Component = manager.BuildComponent(tcmComponentPresentation.Component);
                }
                cp.IsDynamic = false;
            }
            cp.ComponentTemplate = manager.BuildComponentTemplate(tcmComponentPresentation.ComponentTemplate);
            return cp;
        }
        public static Dynamic.Region BuildRegion(TCM.Regions.IRegion tcmRegion, Engine engine, BuildManager manager, int linkLevels, bool resolveWidthAndHeight, bool publishEmptyFields)
        {
            Dynamic.Region r = new Dynamic.Region
            {
                Name           = tcmRegion.RegionName,
                Schema         = manager.BuildSchema(tcmRegion.RegionSchema),
                MetadataFields = new Dynamic.FieldSet()
            };
            if (linkLevels > 0)
            {
                try
                {
                    if (tcmRegion.Metadata != null && tcmRegion.RegionSchema != null)
                    {
                        var tcmMetadataFields = new Tridion.ContentManager.ContentManagement.Fields.ItemFields(tcmRegion.Metadata, tcmRegion.RegionSchema);
                        r.MetadataFields = manager.BuildFields(tcmMetadataFields);
                    }
                }
                catch (Exception)
                {
                    // fail silently if there is no metadata schema
                }
            }

            r.ComponentPresentations = new List <Dynamic.ComponentPresentation>();
            foreach (TCM.ComponentPresentation cp in tcmRegion.ComponentPresentations)
            {
                Dynamic.ComponentPresentation dynCp = manager.BuildComponentPresentation(cp, engine, linkLevels - 1, resolveWidthAndHeight);
                r.ComponentPresentations.Add(dynCp);
            }

            manager.AddXpathToFields(r.MetadataFields, "Metadata");

            // adding nested regions
            r.Regions = new List <Dynamic.Region>();
            foreach (TCM.Regions.IRegion nestedTcmRegion in tcmRegion.Regions)
            {
                Dynamic.Region nestedRegion = manager.BuildRegion(nestedTcmRegion, engine);
                r.Regions.Add(nestedRegion);
            }

            return(r);
        }
        private void AddCpExtensionData(Dd4tComponentPresentation dd4tComponentPresentation, TcmComponentPresentation tcmComponentPresentation)
        {
            if (!tcmComponentPresentation.Conditions.Any())
            {
                return;
            }
            _logger.Debug("ComponentPresentation has Conditions");

            string[] includeContextExpressions = ContextExpressionUtils.GetContextExpressions(tcmComponentPresentation.Conditions.Where(c => !c.Negate).Select(c => c.TargetGroup));
            string[] excludeContextExpressions = ContextExpressionUtils.GetContextExpressions(tcmComponentPresentation.Conditions.Where(c => c.Negate).Select(c => c.TargetGroup));

            if (includeContextExpressions.Any())
            {
                _logger.Debug("Adding Context Expression Conditions (Include): " + string.Join(", ", includeContextExpressions));
                dd4tComponentPresentation.AddExtensionProperty(ContextExpressionSectionName, "Include", includeContextExpressions);
            }

            if (excludeContextExpressions.Any())
            {
                _logger.Debug("Adding Context Expression Conditions (Exclude): " + string.Join(", ", excludeContextExpressions));
                dd4tComponentPresentation.AddExtensionProperty(ContextExpressionSectionName, "Exclude", excludeContextExpressions);
            }
        }
Exemplo n.º 10
0
        private void AddCpExtensionData(Dd4tComponentPresentation dd4tComponentPresentation, TcmComponentPresentation tcmComponentPresentation)
        {
            if (!tcmComponentPresentation.Conditions.Any())
            {
                return;
            }
            _logger.Debug("ComponentPresentation has Conditions");

            string[] includeContextExpressions = ContextExpressionManager.GetContextExpressions(tcmComponentPresentation.Conditions.Where(c => !c.Negate).Select(c => c.TargetGroup));
            string[] excludeContextExpressions = ContextExpressionManager.GetContextExpressions(tcmComponentPresentation.Conditions.Where(c => c.Negate).Select(c => c.TargetGroup));

            if (includeContextExpressions.Any())
            {
                _logger.Debug("Adding Context Expression Conditions (Include): " + string.Join(", ", includeContextExpressions));
                dd4tComponentPresentation.AddExtensionProperty(ContextExpressionSectionName, "Include", includeContextExpressions);
            }

            if (excludeContextExpressions.Any())
            {
                _logger.Debug("Adding Context Expression Conditions (Exclude): " + string.Join(", ", excludeContextExpressions));
                dd4tComponentPresentation.AddExtensionProperty(ContextExpressionSectionName, "Exclude", excludeContextExpressions);
            }
        }
Exemplo n.º 11
0
        private void ProcessRegions(IList <IRegion> regions, Dynamic.Page dd4tPage, int fieldCounter)
        {
            int i = fieldCounter;

            Log.Debug($"ProcessRegions {regions.Count()}, {dd4tPage.ComponentPresentations.Count()}, {fieldCounter}");
            foreach (var region in regions)
            {
                if (region.ComponentPresentations != null && region.ComponentPresentations.Any())
                {
                    foreach (var cp in region.ComponentPresentations)
                    {
                        Dynamic.Component             component             = Manager.BuildComponent(cp.Component);
                        Dynamic.ComponentTemplate     componentTemplate     = Manager.BuildComponentTemplate(cp.ComponentTemplate);
                        Dynamic.ComponentPresentation componentPresentation = new Dynamic.ComponentPresentation()
                        {
                            Component = component, ComponentTemplate = componentTemplate, IsDynamic = cp.ComponentTemplate.IsRepositoryPublishable
                        };
                        dd4tPage.ComponentPresentations.Add(componentPresentation);
                        if (componentTemplate.MetadataFields == null)
                        {
                            componentTemplate.MetadataFields = new Dynamic.FieldSet();
                        }
                        if (componentTemplate.MetadataFields.ContainsKey("region"))
                        {
                            componentTemplate.MetadataFields["region"].Values.Clear();
                            componentTemplate.MetadataFields["region"].Values.Add(region.RegionSchema.Title);
                        }
                        else
                        {
                            componentTemplate.MetadataFields.Add("region", new Dynamic.Field()
                            {
                                Name      = "region",
                                FieldType = Dynamic.FieldType.Text,
                                Values    = new List <string>()
                                {
                                    region.RegionName
                                }
                            });
                        }
                        // remove regionView metadata field, because it is no longer possible to manage this information in Tridion 9 with Regions
                        if (componentTemplate.MetadataFields.ContainsKey("regionView"))
                        {
                            componentTemplate.MetadataFields.Remove("regionView");
                        }
                        // copy all other metadata fields from the region to the entity
                        if (region.Metadata != null)
                        {
                            ItemFields regionMetadataFields = new ItemFields(region.Metadata, region.RegionSchema);
                            try
                            {
                                foreach (var regionMeta in regionMetadataFields)
                                {
                                    componentTemplate.MetadataFields.Add(regionMeta.Name, Manager.BuildField(regionMeta, 1));
                                }
                            }
                            catch (Exception e)
                            {
                                Log.Warning("error while trying to copy metadata from region to component template\r\n" + e.Message + "\r\n" + e.StackTrace);
                            }
                        }

                        if (region.Regions != null && region.RegionName.Any())
                        {
                            ProcessRegions(region.Regions, dd4tPage, i);
                        }
                    }
                }
            }
        }
        private Dynamic.ComponentPresentation GetDynamicComponentPresentation()
        {

            Template template = Engine.PublishingContext.ResolvedItem.Template;
            if (! (template is ComponentTemplate))
            {
                Log.Error("no component template found (is this a page template?)");
                return null;
            }
            ComponentTemplate tcmComponentTemplate = (ComponentTemplate)template;
            Item item = Package.GetByName(Package.ComponentName);
            if (item == null)
            {
                Log.Error("no component found (is this a page template?)");
                return null;
            }
            Component tcmComponent = (Component)Engine.GetObject(item);

            Dynamic.Component component = Manager.BuildComponent(tcmComponent);
            EnsureExtraProperties(component,tcmComponent);
            Dynamic.ComponentTemplate componentTemplate = Manager.BuildComponentTemplate(tcmComponentTemplate);
            Dynamic.ComponentPresentation componentPresentation = new Dynamic.ComponentPresentation() { Component = component, ComponentTemplate = componentTemplate, IsDynamic = true };

            return componentPresentation;
        }