BuildComponent() public method

public BuildComponent ( Tridion.ContentManager.ContentManagement tcmComponent ) : Tridion.Extensions.DynamicDelivery.ContentModel.Component
tcmComponent Tridion.ContentManager.ContentManagement
return Tridion.Extensions.DynamicDelivery.ContentModel.Component
Ejemplo n.º 1
0
        public static Dynamic.ComponentPresentation BuildComponentPresentation(TCM.ComponentPresentation tcmComponentPresentation, Engine engine, int linkLevels, bool resolveWidthAndHeight, BuildManager manager)
        {
            Dynamic.ComponentPresentation cp = new Dynamic.ComponentPresentation();


            // 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(PageTemplateBase.VariableNameCalledFromDynamicDelivery))
            {
                engine.PublishingContext.RenderContext.ContextVariables.Add(PageTemplateBase.VariableNameCalledFromDynamicDelivery, PageTemplateBase.VariableValueCalledFromDynamicDelivery);
            }

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

            engine.PublishingContext.RenderContext.ContextVariables.Remove(PageTemplateBase.VariableNameCalledFromDynamicDelivery);

            renderedContent = TridionUtils.StripTcdlTags(renderedContent);

            if (tcmComponentPresentation.ComponentTemplate.IsRepositoryPublishable)
            {
                // 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); // linkLevels = 0 means: only summarize the component
            }
            else
            {
                TextReader tr = new StringReader(renderedContent);
                if (serializer == null)
                {
                    serializer = new XmlSerializerFactory().CreateSerializer(typeof(Dynamic.Component));
                }
                try
                {
                    cp.Component = (Dynamic.Component)serializer.Deserialize(tr);
                }
                catch
                {
                    // 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;
                }
            }
            cp.ComponentTemplate = manager.BuildComponentTemplate(tcmComponentPresentation.ComponentTemplate);
            return(cp);
        }
Ejemplo n.º 2
0
        public static Dynamic.Field BuildField(TCM.Fields.ItemField tcmItemField, int linkLevels, bool resolveWidthAndHeight, BuildManager manager)
        {
            Dynamic.Field f = new Dynamic.Field();

            if (tcmItemField == null)
            {
                throw new FieldHasNoValueException();
            }
            f.Name = tcmItemField.Name;
            if (tcmItemField is TCM.Fields.XhtmlField)
            {
                TCM.Fields.XhtmlField sField = (TCM.Fields.XhtmlField)tcmItemField;
                if (sField.Values.Count == 0)
                {
                    throw new FieldHasNoValueException();
                }
                foreach (string v in sField.Values)
                {
                    f.Values.Add(v);
                }
                f.FieldType = FieldType.Xhtml;
                return(f);
            }
            if (tcmItemField is TCM.Fields.MultiLineTextField)
            {
                TCM.Fields.TextField sField = (TCM.Fields.MultiLineTextField)tcmItemField;
                if (sField.Values.Count == 0)
                {
                    throw new FieldHasNoValueException();
                }
                foreach (string v in sField.Values)
                {
                    f.Values.Add(v);
                }
                f.FieldType = FieldType.MultiLineText;
                return(f);
            }
            if (tcmItemField is TCM.Fields.TextField)
            {
                TCM.Fields.TextField sField = (TCM.Fields.TextField)tcmItemField;
                if (sField.Values.Count == 0)
                {
                    throw new FieldHasNoValueException();
                }
                foreach (string v in sField.Values)
                {
                    f.Values.Add(v);
                }
                f.FieldType = FieldType.Text;
                return(f);
            }
            if (tcmItemField is TCM.Fields.KeywordField)
            {
                TCM.Fields.KeywordField sField = (TCM.Fields.KeywordField)tcmItemField;
                if (sField.Values.Count == 0)
                {
                    throw new FieldHasNoValueException();
                }
                // we will wrap each linked component in a ContentModel component
                f.Values = new List <string>();
                foreach (TCM.Keyword kw in sField.Values)
                {
                    // todo: add binary to package, and add BinaryUrl property to the component
                    f.Values.Add(kw.Title);
                }
                f.FieldType = FieldType.Keyword;
                KeywordFieldDefinition fieldDef = (KeywordFieldDefinition)sField.Definition;
                f.CategoryId   = fieldDef.Category.Id;
                f.CategoryName = fieldDef.Category.Title;
                return(f);
            }
            if (tcmItemField is TCM.Fields.NumberField)
            {
                TCM.Fields.NumberField sField = (TCM.Fields.NumberField)tcmItemField;
                if (sField.Values.Count == 0)
                {
                    throw new FieldHasNoValueException();
                }
                f.NumericValues = (List <double>)sField.Values;
                f.Values        = new List <string>();
                foreach (double d in f.NumericValues)
                {
                    f.Values.Add(Convert.ToString(d));
                }
                f.FieldType = FieldType.Number;
                return(f);
            }
            if (tcmItemField is TCM.Fields.DateField)
            {
                TCM.Fields.DateField sField = (TCM.Fields.DateField)tcmItemField;
                if (sField.Values.Count == 0)
                {
                    throw new FieldHasNoValueException();
                }
                f.DateTimeValues = (List <DateTime>)sField.Values;
                f.Values         = new List <string>();
                foreach (DateTime dt in f.DateTimeValues)
                {
                    f.Values.Add(Convert.ToString(dt));
                }
                f.FieldType = FieldType.Date;
                return(f);
            }
            if (tcmItemField is TCM.Fields.MultimediaLinkField)
            {
                TCM.Fields.MultimediaLinkField sField = (TCM.Fields.MultimediaLinkField)tcmItemField;
                if (sField.Values.Count == 0)
                {
                    throw new FieldHasNoValueException();
                }

                // we will wrap each linked component in a ContentModel component
                f.LinkedComponentValues = new List <Dynamic.Component>();
                foreach (TCM.Component comp in sField.Values)
                {
                    // todo: add binary to package, and add BinaryUrl property to the component
                    f.LinkedComponentValues.Add(manager.BuildComponent(comp, linkLevels - 1, resolveWidthAndHeight));
                }
                f.Values = new List <string>();
                foreach (Dynamic.Component c in f.LinkedComponentValues)
                {
                    f.Values.Add(c.Id);
                }
                f.FieldType = FieldType.MultiMediaLink;
                return(f);
            }
            if (tcmItemField is TCM.Fields.ComponentLinkField)
            {
                TCM.Fields.ComponentLinkField sField = (TCM.Fields.ComponentLinkField)tcmItemField;
                if (sField.Values.Count == 0)
                {
                    throw new FieldHasNoValueException();
                }
                // we will wrap each linked component in a ContentModel component
                f.LinkedComponentValues = new List <Dynamic.Component>();
                foreach (TCM.Component comp in sField.Values)
                {
                    f.LinkedComponentValues.Add(manager.BuildComponent(comp, linkLevels - 1, resolveWidthAndHeight));
                }
                f.Values = new List <string>();
                foreach (Dynamic.Component c in f.LinkedComponentValues)
                {
                    f.Values.Add(c.Id);
                }
                f.FieldType = FieldType.ComponentLink;
                return(f);
            }

            if (tcmItemField is TCM.Fields.EmbeddedSchemaField)
            {
                TCM.Fields.EmbeddedSchemaField sField = (TCM.Fields.EmbeddedSchemaField)tcmItemField;
                if (sField.Values.Count == 0)
                {
                    throw new FieldHasNoValueException();
                }
                // we will wrap each linked component in a ContentModel component
                f.EmbeddedValues = new List <Dynamic.Fields>();
                foreach (TCM.Fields.ItemFields embeddedFields in sField.Values)
                {
                    f.EmbeddedValues.Add(manager.BuildFields(embeddedFields, linkLevels, resolveWidthAndHeight));
                }
                f.FieldType = FieldType.Embedded;
                return(f);
            }

            throw new FieldTypeNotDefinedException();
        }
        private Dynamic.Component GetDynamicComponent(BuildManager manager)
        {
            GeneralUtils.TimedLog("start getting component from package");
            Item item = Package.GetByName(Package.ComponentName);
            GeneralUtils.TimedLog("finished getting component from package");
            if (item == null)
            {
                Log.Error("no component found (is this a page template?)");
                return null;
            }
            Component tcmComponent = (Component)Engine.GetObject(item.GetAsSource().GetValue("ID"));
            int linkLevels;
            if (HasPackageValue(Package, "LinkLevels"))
            {
                linkLevels = Convert.ToInt32(Package.GetValue("LinkLevels"));
            }
            else
            {
                GeneralUtils.TimedLog("no link levels configured, using default level " + this.DefaultLinkLevels);
                linkLevels = this.DefaultLinkLevels;
            }
            bool resolveWidthAndHeight;
            if (HasPackageValue(Package, "ResolveWidthAndHeight"))
            {
                resolveWidthAndHeight = Package.GetValue("ResolveWidthAndHeight").ToLower().Equals("yes");
            }
            else
            {
                GeneralUtils.TimedLog("no ResolveWidthAndHeight configured, using default value " + this.DefaultResolveWidthAndHeight);
                resolveWidthAndHeight = this.DefaultResolveWidthAndHeight;
            }

            GeneralUtils.TimedLog("found component with title " + tcmComponent.Title + " and id " + tcmComponent.Id);
            GeneralUtils.TimedLog("constructing dynamic component, links are followed to level " + linkLevels + ", width and height are " + (resolveWidthAndHeight ? "" : "not ") + "resolved");

            GeneralUtils.TimedLog("start building dynamic component");
            Dynamic.Component component = manager.BuildComponent(tcmComponent, linkLevels, resolveWidthAndHeight);
            GeneralUtils.TimedLog("finished building dynamic component");
            return component;
        }
Ejemplo n.º 4
0
        public static Dynamic.Field BuildField(TCM.Fields.ItemField tcmItemField, int linkLevels, bool resolveWidthAndHeight, BuildManager manager)
        {
            Dynamic.Field f = new Dynamic.Field();

             if (tcmItemField == null)
             {
            throw new FieldHasNoValueException();
             }
             f.Name = tcmItemField.Name;
             if (tcmItemField is TCM.Fields.XhtmlField)
             {
            TCM.Fields.XhtmlField sField = (TCM.Fields.XhtmlField)tcmItemField;
            if (sField.Values.Count == 0)
               throw new FieldHasNoValueException();
            foreach (string v in sField.Values)
            {
               f.Values.Add(v);
            }
            f.FieldType = FieldType.Xhtml;
            return f;
             }
             if (tcmItemField is TCM.Fields.MultiLineTextField)
             {
            TCM.Fields.TextField sField = (TCM.Fields.MultiLineTextField)tcmItemField;
            if (sField.Values.Count == 0)
               throw new FieldHasNoValueException();
            foreach (string v in sField.Values)
            {
               f.Values.Add(v);
            }
            f.FieldType = FieldType.MultiLineText;
            return f;
             }
             if (tcmItemField is TCM.Fields.TextField)
             {
            TCM.Fields.TextField sField = (TCM.Fields.TextField)tcmItemField;
            if (sField.Values.Count == 0)
               throw new FieldHasNoValueException();
            foreach (string v in sField.Values)
            {
               f.Values.Add(v);
            }
            f.FieldType = FieldType.Text;
            return f;
             }
             if (tcmItemField is TCM.Fields.KeywordField)
             {
            TCM.Fields.KeywordField sField = (TCM.Fields.KeywordField)tcmItemField;
            if (sField.Values.Count == 0)
               throw new FieldHasNoValueException();
            // we will wrap each linked component in a ContentModel component
            f.Values = new List<string>();
            foreach (TCM.Keyword kw in sField.Values)
            {
               // todo: add binary to package, and add BinaryUrl property to the component
               f.Values.Add(kw.Title);
            }
            f.FieldType = FieldType.Keyword;
            KeywordFieldDefinition fieldDef = (KeywordFieldDefinition) sField.Definition;
            f.CategoryId = fieldDef.Category.Id;
            f.CategoryName = fieldDef.Category.Title;
            return f;
             }
             if (tcmItemField is TCM.Fields.NumberField)
             {
            TCM.Fields.NumberField sField = (TCM.Fields.NumberField)tcmItemField;
            if (sField.Values.Count == 0)
               throw new FieldHasNoValueException();
            f.NumericValues = (List<double>)sField.Values;
            f.Values = new List<string>();
            foreach (double d in f.NumericValues)
            {
               f.Values.Add(Convert.ToString(d));
            }
            f.FieldType = FieldType.Number;
            return f;
             }
             if (tcmItemField is TCM.Fields.DateField)
             {
            TCM.Fields.DateField sField = (TCM.Fields.DateField)tcmItemField;
            if (sField.Values.Count == 0)
               throw new FieldHasNoValueException();
            f.DateTimeValues = (List<DateTime>)sField.Values;
            f.Values = new List<string>();
            foreach (DateTime dt in f.DateTimeValues)
            {
               f.Values.Add(Convert.ToString(dt));
            }
            f.FieldType = FieldType.Date;
            return f;
             }
             if (tcmItemField is TCM.Fields.MultimediaLinkField)
             {
            TCM.Fields.MultimediaLinkField sField = (TCM.Fields.MultimediaLinkField)tcmItemField;
            if (sField.Values.Count == 0)
               throw new FieldHasNoValueException();

            // we will wrap each linked component in a ContentModel component
            f.LinkedComponentValues = new List<Dynamic.Component>();
            foreach (TCM.Component comp in sField.Values)
            {
               // todo: add binary to package, and add BinaryUrl property to the component
                f.LinkedComponentValues.Add(manager.BuildComponent(comp, linkLevels - 1, resolveWidthAndHeight));
            }
            f.Values = new List<string>();
            foreach (Dynamic.Component c in f.LinkedComponentValues)
            {
               f.Values.Add(c.Id);
            }
            f.FieldType = FieldType.MultiMediaLink;
            return f;
             }
             if (tcmItemField is TCM.Fields.ComponentLinkField)
             {
            TCM.Fields.ComponentLinkField sField = (TCM.Fields.ComponentLinkField)tcmItemField;
            if (sField.Values.Count == 0)
               throw new FieldHasNoValueException();
            // we will wrap each linked component in a ContentModel component
            f.LinkedComponentValues = new List<Dynamic.Component>();
            foreach (TCM.Component comp in sField.Values)
            {
                f.LinkedComponentValues.Add(manager.BuildComponent(comp, linkLevels - 1, resolveWidthAndHeight));
            }
            f.Values = new List<string>();
            foreach (Dynamic.Component c in f.LinkedComponentValues)
            {
               f.Values.Add(c.Id);
            }
            f.FieldType = FieldType.ComponentLink;
            return f;
             }

             if (tcmItemField is TCM.Fields.EmbeddedSchemaField)
             {
            TCM.Fields.EmbeddedSchemaField sField = (TCM.Fields.EmbeddedSchemaField)tcmItemField;
            if (sField.Values.Count == 0)
               throw new FieldHasNoValueException();
            // we will wrap each linked component in a ContentModel component
            f.EmbeddedValues = new List<Dynamic.Fields>();
            foreach (TCM.Fields.ItemFields embeddedFields in sField.Values)
            {
                f.EmbeddedValues.Add(manager.BuildFields(embeddedFields, linkLevels, resolveWidthAndHeight));
            }
            f.FieldType = FieldType.Embedded;
            return f;
             }

             throw new FieldTypeNotDefinedException();
        }