/// <summary> /// Gets the raw string (xml) from the broker db by URL /// </summary> /// <param name="Url">URL of the page</param> /// <returns>String with page xml or empty string if no page was found</returns> public string GetContentByUrl(string Url) { Page page = new Page(); page.Title = Randomizer.AnyString(15); page.Id = Randomizer.AnyUri(64); page.Filename = Randomizer.AnySafeString(8) + ".html"; PageTemplate pt = new PageTemplate(); pt.Title = Randomizer.AnyString(20); Field ptfieldView = new Field(); ptfieldView.Name = "view"; ptfieldView.Values.Add("Standard"); pt.MetadataFields = new FieldSet(); pt.MetadataFields.Add(ptfieldView.Name, ptfieldView); page.PageTemplate = pt; Schema schema = new Schema(); schema.Title = Randomizer.AnyString(10); Component component = new Component(); component.Title = Randomizer.AnyString(30); component.Id = Randomizer.AnyUri(16); component.Schema = schema; Field field1 = Randomizer.AnyTextField(6, 120, true); Field field2 = Randomizer.AnyTextField(8, 40, false); FieldSet fieldSet = new FieldSet(); fieldSet.Add(field1.Name, field1); fieldSet.Add(field2.Name, field2); component.Fields = fieldSet; ComponentTemplate ct = new ComponentTemplate(); ct.Title = Randomizer.AnyString(20); Field fieldView = new Field(); fieldView.Name = "view"; fieldView.Values.Add("DefaultComponentView"); ct.MetadataFields = new FieldSet(); ct.MetadataFields.Add(fieldView.Name, fieldView); ComponentPresentation cp = new ComponentPresentation(); cp.Component = component; cp.ComponentTemplate = ct; page.ComponentPresentations = new List<ComponentPresentation>(); page.ComponentPresentations.Add(cp); FieldSet metadataFields = new FieldSet(); page.MetadataFields = metadataFields; var serializer = new XmlSerializer(typeof(Page)); StringBuilder builder = new StringBuilder(); StringWriter writer = new StringWriter(builder); //XmlTextWriter writer = new XmlTextWriter(page.Filename, Encoding.UTF8); //serializer.Serialize(writer, page); serializer.Serialize(writer, page); string pageAsString = builder.ToString(); return pageAsString; }
/// <summary> /// Creates a lightweight DD4T Component that contains enough information such that the semantic model builder can cope and build a strongly typed model from it. /// </summary> /// <param name="componentMeta">A <see cref="DD4T.ContentModel.IComponentMeta"/> instance obtained from CD API.</param> /// <returns>A DD4T Component.</returns> private static IComponent CreateComponent(IComponentMeta componentMeta) { Component component = new Component { Id = $"tcm:{componentMeta.PublicationId}-{componentMeta.Id}", LastPublishedDate = componentMeta.LastPublicationDate, RevisionDate = componentMeta.ModificationDate, Schema = new Schema { PublicationId = componentMeta.PublicationId.ToString(), Id = $"tcm:{componentMeta.PublicationId}-{componentMeta.SchemaId}" }, MetadataFields = new FieldSet() }; FieldSet metadataFields = new FieldSet(); component.MetadataFields.Add("standardMeta", new Field { EmbeddedValues = new List <FieldSet> { metadataFields } }); foreach (DictionaryEntry de in componentMeta.CustomMeta.NameValues) { object v = ((NameValuePair)de.Value).Value; if (v == null) { continue; } string k = de.Key.ToString(); metadataFields.Add(k, new Field { Name = k, Values = new List <string> { v.ToString() } }); } // The semantic mapping requires that some metadata fields exist. This may not be the case so we map some component meta properties onto them // if they don't exist. if (!metadataFields.ContainsKey("dateCreated")) { metadataFields.Add("dateCreated", new Field { Name = "dateCreated", DateTimeValues = new List <DateTime> { componentMeta.LastPublicationDate } }); } if (!metadataFields.ContainsKey("name")) { metadataFields.Add("name", new Field { Name = "name", Values = new List <string> { componentMeta.Title } }); } return(component); }
public string GetContent(string uri, string templateUri = "") { Schema schema = new Schema(); schema.Title = Randomizer.AnyString(10); Component component = new Component(); component.Title = Randomizer.AnyString(30); component.Id = Randomizer.AnyUri(16); component.Schema = schema; Field field1 = Randomizer.AnyTextField(6, 120, true); Field field2 = Randomizer.AnyTextField(8, 40, false); FieldSet fieldSet = new FieldSet(); fieldSet.Add(field1.Name, field1); fieldSet.Add(field2.Name, field2); component.Fields = fieldSet; if (templateUri == "componentlink") { CustomizeCompomentForComponentLink(component); } if (templateUri == "embedded") { CustomizeCompomentForEmbeddedField(component); } if (templateUri == "keyword") { CustomizeCompomentForKeywordField(component); } if(templateUri == "componentIgnoreCase") { CustomizeCompomentForComponentLinkIgnoreCase(component); } if (uri == "component") { return SerializerService.Serialize<Component>(component); } ComponentTemplate ct = new ComponentTemplate(); ct.Title = Randomizer.AnyString(20); Field fieldView = new Field(); fieldView.Name = "view"; fieldView.Values.Add("DefaultComponentView"); ct.MetadataFields = new FieldSet(); ct.MetadataFields.Add(fieldView.Name, fieldView); ComponentPresentation cp = new ComponentPresentation(); cp.Component = component; cp.ComponentTemplate = ct; Condition condition = new KeywordCondition() { Keyword = new Keyword() { Id = "tcm:2-123-1024", Key = "test", Title = "Test" }, Operator = NumericalConditionOperator.Equals, Value = 1 }; cp.Conditions = new List<Condition>() { condition }; return SerializerService.Serialize<ComponentPresentation>(cp); }
public string GetContent(string uri, string templateUri = "") { Schema schema = new Schema(); schema.Title = Randomizer.AnyString(10); Component component = new Component(); component.Title = Randomizer.AnyString(30); component.Id = Randomizer.AnyUri(16); component.Schema = schema; Field field1 = Randomizer.AnyTextField(6, 120, true); Field field2 = Randomizer.AnyTextField(8, 40, false); FieldSet fieldSet = new FieldSet(); fieldSet.Add(field1.Name, field1); fieldSet.Add(field2.Name, field2); component.Fields = fieldSet; if (uri == "component") { return(SerializerService.Serialize <Component>(component)); } ComponentTemplate ct = new ComponentTemplate(); ct.Title = Randomizer.AnyString(20); Field fieldView = new Field(); fieldView.Name = "view"; fieldView.Values.Add("DefaultComponentView"); ct.MetadataFields = new FieldSet(); ct.MetadataFields.Add(fieldView.Name, fieldView); ComponentPresentation cp = new ComponentPresentation(); cp.Component = component; cp.ComponentTemplate = ct; Condition condition = new KeywordCondition() { Keyword = new Keyword() { Id = "tcm:2-123-1024", Key = "test", Title = "Test" }, Operator = NumericalConditionOperator.Equals, Value = 1 }; cp.Conditions = new List <Condition>() { condition }; return(SerializerService.Serialize <ComponentPresentation>(cp)); }
private void CustomizeCompomentForKeywordField(Component component) { component.Schema.RootElementName = "hasKeyword"; Field headingField = new Field() { Name = "heading", Values = new List <string> { "some heading" } }; FieldSet metadataFields = new FieldSet(); metadataFields.Add(headingField.Name, headingField); Field keywordField = new Field() { Name = "keyword", KeywordValues = new List <Keyword>() { new Keyword() { MetadataFields = metadataFields, Id = Randomizer.AnyUri(1024), Title = Randomizer.AnyString(33), Description = Randomizer.AnyString(33) } } }; component.Fields.Add(keywordField.Name, keywordField); }
private void CustomizeCompomentForKeywordField(Component component) { component.Schema.RootElementName = "hasKeyword"; Field headingField = new Field() { Name = "heading", Values = new List<string> { "some heading" } }; FieldSet metadataFields = new FieldSet(); metadataFields.Add(headingField.Name, headingField); Field keywordField = new Field() { Name = "keyword", KeywordValues = new List<Keyword>() { new Keyword() { MetadataFields = metadataFields, Id = Randomizer.AnyUri(1024), Title = Randomizer.AnyString(33), Description = Randomizer.AnyString(33) } } }; component.Fields.Add(keywordField.Name, keywordField); }
private void CustomizeCompomentForEmbeddedField(Component component) { component.Schema.RootElementName = "rootEmbedding"; Field headingField = new Field() { Name = "heading", Values = new List <string> { "some heading" } }; FieldSet embeddedFields = new FieldSet(); embeddedFields.Add(headingField.Name, headingField); Field embeddedField = new Field() { Name = "embedded", EmbeddedSchema = new Schema() { Title = "EmbeddedSchema", RootElementName = "embeddedRoot", Id = Randomizer.AnyUri(8) }, EmbeddedValues = new List <FieldSet>() { embeddedFields } }; component.Fields.Add(embeddedField.Name, embeddedField); }
private void CustomizeCompomentForComponentLinkIgnoreCase(Component component) { component.Schema.RootElementName = "RootA"; Field headingField = new Field() { Name = "Heading", Values = new List <string> { "some heading" } }; FieldSet fieldsForLinkedComponent = new FieldSet(); fieldsForLinkedComponent.Add(headingField.Name, headingField); Field linkField = new Field() { Name = "Link", LinkedComponentValues = new List <Component> { new Component() { Title = Randomizer.AnyString(16), Id = Randomizer.AnyUri(16), Schema = new Schema() { Title = Randomizer.AnyString(10), RootElementName = "RootB" }, Fields = fieldsForLinkedComponent } } }; component.Fields.Add(linkField.Name, linkField); }
public static IFieldSet ToFieldSet(this IEnumerable<KeyValuePair<string, IField>> ie) { IFieldSet set = new FieldSet(); foreach (KeyValuePair<string, IField> pair in ie) { set.Add(pair); } return set; }
public Maybe<object> For(object instance) { var fieldSet = new FieldSet(); var members = instance.GetType().FieldsAndProperties(Flags.InstancePublic); foreach (var member in members) { var view = memberViewProviders.For(instance, member); fieldSet.Add(view.IsJust() ? view.FromJust() : member.Get(instance)); } return fieldSet; }
private static FieldSet CreateExternalMetadataFieldSet(IEnumerable <IFieldDefinition> eclFieldDefinitions, XmlElement parentElement) { FieldSet fieldSet = new FieldSet(); foreach (IFieldDefinition eclFieldDefinition in eclFieldDefinitions) { XmlNodeList fieldElements = parentElement.SelectNodes(string.Format("*[local-name()='{0}']", eclFieldDefinition.XmlElementName)); if (fieldElements.Count == 0) { // Don't generate a DD4T Field for ECL field without values. continue; } Field field = new Field { Name = eclFieldDefinition.XmlElementName }; foreach (XmlElement fieldElement in fieldElements) { if (eclFieldDefinition is INumberFieldDefinition) { if (fieldElement.HasChildNodes) { field.NumericValues.Add(XmlConvert.ToDouble(fieldElement.InnerText)); } field.FieldType = FieldType.Number; } else if (eclFieldDefinition is IDateFieldDefinition) { if (fieldElement.HasChildNodes) { field.DateTimeValues.Add(XmlConvert.ToDateTime(fieldElement.InnerText, XmlDateTimeSerializationMode.Unspecified)); } field.FieldType = FieldType.Date; } else if (eclFieldDefinition is IFieldGroupDefinition) { if (field.EmbeddedValues == null) { field.EmbeddedValues = new List <FieldSet>(); } IEnumerable <IFieldDefinition> embeddedFieldDefinitions = ((IFieldGroupDefinition)eclFieldDefinition).Fields; field.EmbeddedValues.Add(CreateExternalMetadataFieldSet(embeddedFieldDefinitions, fieldElement)); field.FieldType = FieldType.Embedded; } else { field.Values.Add(fieldElement.InnerText); if (eclFieldDefinition is IMultiLineTextFieldDefinition) { field.FieldType = FieldType.MultiLineText; } else if (eclFieldDefinition is IXhtmlFieldDefinition) { field.FieldType = FieldType.Xhtml; } else { field.FieldType = FieldType.Text; } } } fieldSet.Add(eclFieldDefinition.XmlElementName, field); } return(fieldSet); }
public static Dynamic.Field BuildField(TCM.Fields.ItemField tcmItemField, int linkLevels, bool resolveWidthAndHeight, bool publishEmptyFields, BuildManager manager) { Dynamic.Field f = new Dynamic.Field(); if (tcmItemField == null&&publishEmptyFields) { GeneralUtils.TimedLog("item field is null"); //throw new FieldHasNoValueException(); f.Values.Add(""); return f; } else if (tcmItemField == null && !publishEmptyFields) { throw new FieldHasNoValueException(); } f.Name = tcmItemField.Name; if (tcmItemField is TCM.Fields.XhtmlField) { TCM.Fields.XhtmlField sField = (TCM.Fields.XhtmlField)tcmItemField; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); if (sField.Values.Count == 0 && publishEmptyFields) { //throw new FieldHasNoValueException(); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) // throw new FieldHasNoValueException(); if (sField.Values.Count == 0 && publishEmptyFields) { //throw new FieldHasNoValueException(); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) // throw new FieldHasNoValueException(); if (sField.Values.Count == 0 && publishEmptyFields) { //throw new FieldHasNoValueException(); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) // throw new FieldHasNoValueException(); if (sField.Values.Count == 0 && publishEmptyFields) { //throw new FieldHasNoValueException(); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { // add keyword values f.Keywords = new List<Keyword>(); // 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.Keywords.Add(manager.BuildKeyword(kw)); } KeywordFieldDefinition fieldDef = (KeywordFieldDefinition)sField.Definition; f.CategoryId = fieldDef.Category.Id; f.CategoryName = fieldDef.Category.Title; } f.FieldType = FieldType.Keyword; return f; } if (tcmItemField is TCM.Fields.NumberField) { TCM.Fields.NumberField sField = (TCM.Fields.NumberField)tcmItemField; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) // throw new FieldHasNoValueException(); if (sField.Values.Count == 0 && publishEmptyFields) { //throw new FieldHasNoValueException(); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) // throw new FieldHasNoValueException(); if (sField.Values.Count == 0 && publishEmptyFields) { //throw new FieldHasNoValueException(); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); if (sField.Values.Count == 0 && publishEmptyFields) { f.Values.Add(tcmItemField.Name); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { // 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, publishEmptyFields)); } 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) // throw new FieldHasNoValueException(); if (sField.Values.Count == 0 && publishEmptyFields) { f.Values.Add(tcmItemField.Name); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { // 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, publishEmptyFields)); } 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) //throw new FieldHasNoValueException(); f.FieldType = FieldType.Embedded; f.EmbeddedValues = new List<Dynamic.FieldSet>(); if (sField.Values.Count == 0 && publishEmptyFields) { Dynamic.FieldSet fields = new FieldSet(); Dynamic.Field fe = new Dynamic.Field(); f.EmbeddedSchema = manager.BuildSchema(((EmbeddedSchemaFieldDefinition)sField.Definition).EmbeddedSchema); EmbeddedSchemaFieldDefinition linksFieldDefinition = sField.Definition as EmbeddedSchemaFieldDefinition; ItemFields newItemField = new ItemFields(linksFieldDefinition.EmbeddedSchema); for (int i = 0; i < newItemField.Count; i++) { if (newItemField[i] is TCM.Fields.EmbeddedSchemaField) { TCM.Fields.EmbeddedSchemaField innerField = (TCM.Fields.EmbeddedSchemaField)newItemField[i]; if (innerField.Values.Count == 0) { Dynamic.FieldSet fieldsinner = new FieldSet(); Dynamic.Field fin = new Dynamic.Field(); fe.EmbeddedSchema = manager.BuildSchema(((EmbeddedSchemaFieldDefinition)innerField.Definition).EmbeddedSchema); EmbeddedSchemaFieldDefinition inlinksFieldDefinition = innerField.Definition as EmbeddedSchemaFieldDefinition; ItemFields newinItemField = new ItemFields(inlinksFieldDefinition.EmbeddedSchema); for (int n = 0; n < newinItemField.Count; n++) { fin.Name = newItemField[n].Name; fieldsinner.Add(newinItemField[n].Name, fin); } fe.EmbeddedValues.Add(fieldsinner); fe.Values.Add(""); } fe.Name = newItemField[i].Name; fields.Add(newItemField[i].Name, fe); } else { Dynamic.Field fein = manager.BuildField(newItemField[i], linkLevels, resolveWidthAndHeight, publishEmptyFields); fein.Values.Clear(); fields.Add(newItemField[i].Name, fein); } } f.EmbeddedValues.Add(fields); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { // we will wrap each linked component in a ContentModel component f.EmbeddedValues = new List<Dynamic.FieldSet>(); f.EmbeddedSchema = manager.BuildSchema(((EmbeddedSchemaFieldDefinition)sField.Definition).EmbeddedSchema); foreach (TCM.Fields.ItemFields embeddedFields in sField.Values) { f.EmbeddedValues.Add(manager.BuildFields(embeddedFields, linkLevels, resolveWidthAndHeight, publishEmptyFields)); } } return f; } throw new FieldTypeNotDefinedException(); }
/// <summary> /// Gets the raw string (xml) from the broker db by URL /// </summary> /// <param name="Url">URL of the page</param> /// <returns>String with page xml or empty string if no page was found</returns> public string GetContentByUrl(string Url) { Page page = new Page(); page.Title = Randomizer.AnyString(15); page.Id = Randomizer.AnyUri(64); page.Filename = Randomizer.AnySafeString(8) + ".html"; PageTemplate pt = new PageTemplate(); pt.Title = Randomizer.AnyString(20); Field ptfieldView = new Field(); ptfieldView.Name = "view"; ptfieldView.Values.Add("Standard"); pt.MetadataFields = new FieldSet(); pt.MetadataFields.Add(ptfieldView.Name, ptfieldView); page.PageTemplate = pt; Schema schema = new Schema(); schema.Title = Randomizer.AnyString(10); Component component = new Component(); component.Title = Randomizer.AnyString(30); component.Id = Randomizer.AnyUri(16); component.Schema = schema; Field field1 = Randomizer.AnyTextField(6, 120, true); Field field2 = Randomizer.AnyTextField(8, 40, false); FieldSet fieldSet = new FieldSet(); fieldSet.Add(field1.Name, field1); fieldSet.Add(field2.Name, field2); component.Fields = fieldSet; ComponentTemplate ct = new ComponentTemplate(); ct.Title = Randomizer.AnyString(20); Field fieldView = new Field(); fieldView.Name = "view"; fieldView.Values.Add("DefaultComponentView"); ct.MetadataFields = new FieldSet(); ct.MetadataFields.Add(fieldView.Name, fieldView); ComponentPresentation cp = new ComponentPresentation(); cp.Component = component; cp.ComponentTemplate = ct; page.ComponentPresentations = new List <ComponentPresentation>(); page.ComponentPresentations.Add(cp); FieldSet metadataFields = new FieldSet(); page.MetadataFields = metadataFields; var serializer = new XmlSerializer(typeof(Page)); StringBuilder builder = new StringBuilder(); StringWriter writer = new StringWriter(builder); //XmlTextWriter writer = new XmlTextWriter(page.Filename, Encoding.UTF8); //serializer.Serialize(writer, page); serializer.Serialize(writer, page); string pageAsString = builder.ToString(); return(pageAsString); }
private void CustomizeCompomentForEmbeddedField(Component component) { component.Schema.RootElementName = "rootEmbedding"; Field headingField = new Field() { Name = "heading", Values = new List<string> { "some heading" } }; FieldSet embeddedFields = new FieldSet(); embeddedFields.Add(headingField.Name, headingField); Field embeddedField = new Field() { Name = "embedded", EmbeddedSchema = new Schema() { Title = "EmbeddedSchema", RootElementName = "embeddedRoot", Id = Randomizer.AnyUri(8) }, EmbeddedValues = new List<FieldSet>() { embeddedFields } }; component.Fields.Add(embeddedField.Name, embeddedField); }
private void CustomizeCompomentForComponentLinkIgnoreCase(Component component) { component.Schema.RootElementName = "RootA"; Field headingField = new Field() { Name = "Heading", Values = new List<string> { "some heading" } }; FieldSet fieldsForLinkedComponent = new FieldSet(); fieldsForLinkedComponent.Add(headingField.Name, headingField); Field linkField = new Field() { Name = "Link", LinkedComponentValues = new List<Component> { new Component() { Title = Randomizer.AnyString(16), Id = Randomizer.AnyUri(16), Schema = new Schema() { Title = Randomizer.AnyString(10), RootElementName = "RootB" }, Fields = fieldsForLinkedComponent } } }; component.Fields.Add(linkField.Name, linkField); }
public void Builder_Can_Build_A_Carousel_Using_The_Embedded_Component_Attribute() { var slide1 = new FieldSet(); slide1.Add("heading", new Field() { Values = { "Slide 1" } }); slide1.Add("summary", new Field() { Values = { "Slide 1 Summary" } }); slide1.Add("image", new Field() { LinkedComponentValues = { new Component() { Multimedia = new Multimedia() { Url = "_images/asd.png" } } } }); var slide2 = new FieldSet(); slide2.Add("heading", new Field() { Values = { "Slide 2" } }); slide2.Add("summary", new Field() { Values = { "Slide 2 Summary" } }); slide2.Add("image", new Field() { LinkedComponentValues = { new Component() { Multimedia = new Multimedia() { Url = "_images/asdc.png" } } } }); var embeddedList = new List <FieldSet>(); embeddedList.Add(slide1); embeddedList.Add(slide2); var carousel = new Component(); carousel.Fields.Add("title", new Field() { Values = { "My Carousel" } }); carousel.Fields.Add("embedded_list", new Field() { EmbeddedValues = embeddedList }); var carouselModel = ComponentViewModelBuilder.Build <CarouselViewModel>(carousel); Assert.That(carouselModel.EmbeddedCarouselItemViewModels.Count(), Is.EqualTo(2)); Assert.That(carouselModel.EmbeddedCarouselItemViewModels.First().Heading, Is.EqualTo("Slide 1")); Assert.That(carouselModel.EmbeddedCarouselItemViewModels.Skip(1).First().Heading, Is.EqualTo("Slide 2")); }
public static Dynamic.Field BuildField(TCM.Fields.ItemField tcmItemField, int currentLinkLevel, BuildManager manager) { Dynamic.Field f = new Dynamic.Field(); if (tcmItemField == null && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); return f; } else if (tcmItemField == null && !manager.BuildProperties.PublishEmptyFields) { 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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { foreach (string v in sField.Values) { f.Values.Add(manager.PublishBinariesInRichTextField(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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { 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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { 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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { // add keyword values f.Keywords = new List<Keyword>(); // we will wrap each linked component in a ContentModel component foreach (TCM.Keyword kw in sField.Values) { f.Keywords.Add(manager.BuildKeyword(kw)); } if (!manager.BuildProperties.OmitValueLists) { f.Values = new List<string>(); foreach (TCM.Keyword kw in sField.Values) { f.Values.Add(kw.Title); } } KeywordFieldDefinition fieldDef = (KeywordFieldDefinition)sField.Definition; f.CategoryId = fieldDef.Category.Id; f.CategoryName = fieldDef.Category.Title; } f.FieldType = FieldType.Keyword; return f; } if (tcmItemField is TCM.Fields.NumberField) { TCM.Fields.NumberField sField = (TCM.Fields.NumberField)tcmItemField; if (sField.Values.Count == 0 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { f.NumericValues = (List<double>)sField.Values; if (!manager.BuildProperties.OmitValueLists) { 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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { f.DateTimeValues = (List<DateTime>)sField.Values; if (!manager.BuildProperties.OmitValueLists) { 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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(tcmItemField.Name); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { // we will wrap each linked component in a ContentModel component f.LinkedComponentValues = new List<Dynamic.Component>(); int nextLinkLevel = currentLinkLevel - 1; if (MustFollowField(sField, manager)) { log.Debug(string.Format("found component link field named {0} with global followLinksPerField property set to false OR followLink set to true for this field", sField.Name)); } else { log.Debug(string.Format("found component link field named {0} with followLink set to false for this field", sField.Name)); nextLinkLevel = 0; } foreach (TCM.Component comp in sField.Values) { f.LinkedComponentValues.Add(manager.BuildComponent(comp, nextLinkLevel)); } if (!manager.BuildProperties.OmitValueLists) { 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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(tcmItemField.Name); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { // we will wrap each linked component in a ContentModel component f.LinkedComponentValues = new List<Dynamic.Component>(); int nextLinkLevel = currentLinkLevel - 1; if (MustFollowField(sField, manager)) { log.Debug(string.Format("found component link field named {0} with global followLinksPerField property set to false OR followLink set to true for this field", sField.Name)); } else { log.Debug(string.Format("found component link field named {0} with followLink set to false for this field", sField.Name)); nextLinkLevel = 0; } foreach (TCM.Component comp in sField.Values) { f.LinkedComponentValues.Add(manager.BuildComponent(comp, nextLinkLevel)); } if (!manager.BuildProperties.OmitValueLists) { 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; f.FieldType = FieldType.Embedded; f.EmbeddedValues = new List<Dynamic.FieldSet>(); bool hasValues = CheckIfEmbeddedFieldHasValues(sField); if (!hasValues && manager.BuildProperties.PublishEmptyFields) { Dynamic.FieldSet fields = new FieldSet(); Dynamic.Field fe = new Dynamic.Field(); f.EmbeddedSchema = manager.BuildSchema(((EmbeddedSchemaFieldDefinition)sField.Definition).EmbeddedSchema); EmbeddedSchemaFieldDefinition linksFieldDefinition = sField.Definition as EmbeddedSchemaFieldDefinition; ItemFields newItemField = new ItemFields(linksFieldDefinition.EmbeddedSchema); for (int i = 0; i < newItemField.Count; i++) { if (newItemField[i] is TCM.Fields.EmbeddedSchemaField) { TCM.Fields.EmbeddedSchemaField innerField = (TCM.Fields.EmbeddedSchemaField)newItemField[i]; if (innerField.Values.Count == 0) { Dynamic.FieldSet fieldsinner = new FieldSet(); Dynamic.Field fin = new Dynamic.Field(); fe.EmbeddedSchema = manager.BuildSchema(((EmbeddedSchemaFieldDefinition)innerField.Definition).EmbeddedSchema); EmbeddedSchemaFieldDefinition inlinksFieldDefinition = innerField.Definition as EmbeddedSchemaFieldDefinition; ItemFields newinItemField = new ItemFields(inlinksFieldDefinition.EmbeddedSchema); for (int n = 0; n < newinItemField.Count; n++) { fin.Name = newItemField[n].Name; fieldsinner.Add(newinItemField[n].Name, fin); } fe.EmbeddedValues.Add(fieldsinner); fe.Values.Add(""); } fe.Name = newItemField[i].Name; fields.Add(newItemField[i].Name, fe); } else { Dynamic.Field fein = manager.BuildField(newItemField[i], currentLinkLevel); fein.Values.Clear(); fields.Add(newItemField[i].Name, fein); } } f.EmbeddedValues.Add(fields); f.Values.Add(""); } else if (!hasValues && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { // we will wrap each linked component in a ContentModel component f.EmbeddedValues = new List<Dynamic.FieldSet>(); f.EmbeddedSchema = manager.BuildSchema(((EmbeddedSchemaFieldDefinition)sField.Definition).EmbeddedSchema); foreach (TCM.Fields.ItemFields embeddedFields in sField.Values) { f.EmbeddedValues.Add(manager.BuildFields(embeddedFields, currentLinkLevel)); } } return f; } throw new FieldTypeNotDefinedException(); }
public static Dynamic.Field BuildField(TCM.Fields.ItemField tcmItemField, int linkLevels, bool resolveWidthAndHeight, bool publishEmptyFields, BuildManager manager) { Dynamic.Field f = new Dynamic.Field(); if (tcmItemField == null && publishEmptyFields) { GeneralUtils.TimedLog("item field is null"); //throw new FieldHasNoValueException(); f.Values.Add(""); return(f); } else if (tcmItemField == null && !publishEmptyFields) { throw new FieldHasNoValueException(); } f.Name = tcmItemField.Name; if (tcmItemField is TCM.Fields.XhtmlField) { TCM.Fields.XhtmlField sField = (TCM.Fields.XhtmlField)tcmItemField; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); if (sField.Values.Count == 0 && publishEmptyFields) { //throw new FieldHasNoValueException(); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) // throw new FieldHasNoValueException(); if (sField.Values.Count == 0 && publishEmptyFields) { //throw new FieldHasNoValueException(); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) // throw new FieldHasNoValueException(); if (sField.Values.Count == 0 && publishEmptyFields) { //throw new FieldHasNoValueException(); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) // throw new FieldHasNoValueException(); if (sField.Values.Count == 0 && publishEmptyFields) { //throw new FieldHasNoValueException(); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { // add keyword values f.Keywords = new List <Keyword>(); // 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.Keywords.Add(manager.BuildKeyword(kw)); } KeywordFieldDefinition fieldDef = (KeywordFieldDefinition)sField.Definition; f.CategoryId = fieldDef.Category.Id; f.CategoryName = fieldDef.Category.Title; } f.FieldType = FieldType.Keyword; return(f); } if (tcmItemField is TCM.Fields.NumberField) { TCM.Fields.NumberField sField = (TCM.Fields.NumberField)tcmItemField; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) // throw new FieldHasNoValueException(); if (sField.Values.Count == 0 && publishEmptyFields) { //throw new FieldHasNoValueException(); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) // throw new FieldHasNoValueException(); if (sField.Values.Count == 0 && publishEmptyFields) { //throw new FieldHasNoValueException(); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); if (sField.Values.Count == 0 && publishEmptyFields) { f.Values.Add(tcmItemField.Name); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { // 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, publishEmptyFields)); } 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) // throw new FieldHasNoValueException(); if (sField.Values.Count == 0 && publishEmptyFields) { f.Values.Add(tcmItemField.Name); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { // 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, publishEmptyFields)); } 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; GeneralUtils.TimedLog(string.Format("item field {0} has {1} values", tcmItemField.Name, sField.Values.Count)); //if (sField.Values.Count == 0) //throw new FieldHasNoValueException(); f.FieldType = FieldType.Embedded; f.EmbeddedValues = new List <Dynamic.FieldSet>(); if (sField.Values.Count == 0 && publishEmptyFields) { Dynamic.FieldSet fields = new FieldSet(); Dynamic.Field fe = new Dynamic.Field(); f.EmbeddedSchema = manager.BuildSchema(((EmbeddedSchemaFieldDefinition)sField.Definition).EmbeddedSchema); EmbeddedSchemaFieldDefinition linksFieldDefinition = sField.Definition as EmbeddedSchemaFieldDefinition; ItemFields newItemField = new ItemFields(linksFieldDefinition.EmbeddedSchema); for (int i = 0; i < newItemField.Count; i++) { if (newItemField[i] is TCM.Fields.EmbeddedSchemaField) { TCM.Fields.EmbeddedSchemaField innerField = (TCM.Fields.EmbeddedSchemaField)newItemField[i]; if (innerField.Values.Count == 0) { Dynamic.FieldSet fieldsinner = new FieldSet(); Dynamic.Field fin = new Dynamic.Field(); fe.EmbeddedSchema = manager.BuildSchema(((EmbeddedSchemaFieldDefinition)innerField.Definition).EmbeddedSchema); EmbeddedSchemaFieldDefinition inlinksFieldDefinition = innerField.Definition as EmbeddedSchemaFieldDefinition; ItemFields newinItemField = new ItemFields(inlinksFieldDefinition.EmbeddedSchema); for (int n = 0; n < newinItemField.Count; n++) { fin.Name = newItemField[n].Name; fieldsinner.Add(newinItemField[n].Name, fin); } fe.EmbeddedValues.Add(fieldsinner); fe.Values.Add(""); } fe.Name = newItemField[i].Name; fields.Add(newItemField[i].Name, fe); } else { Dynamic.Field fein = manager.BuildField(newItemField[i], linkLevels, resolveWidthAndHeight, publishEmptyFields); fein.Values.Clear(); fields.Add(newItemField[i].Name, fein); } } f.EmbeddedValues.Add(fields); f.Values.Add(""); } else if (sField.Values.Count == 0 && !publishEmptyFields) { throw new FieldHasNoValueException(); } else { // we will wrap each linked component in a ContentModel component f.EmbeddedValues = new List <Dynamic.FieldSet>(); f.EmbeddedSchema = manager.BuildSchema(((EmbeddedSchemaFieldDefinition)sField.Definition).EmbeddedSchema); foreach (TCM.Fields.ItemFields embeddedFields in sField.Values) { f.EmbeddedValues.Add(manager.BuildFields(embeddedFields, linkLevels, resolveWidthAndHeight, publishEmptyFields)); } } return(f); } throw new FieldTypeNotDefinedException(); }
public void Builder_Can_Build_A_Carousel_Using_The_Embedded_Component_Attribute() { var slide1 = new FieldSet(); slide1.Add("heading", new Field() { Values = { "Slide 1" } }); slide1.Add("summary", new Field() { Values = { "Slide 1 Summary" } }); slide1.Add("image", new Field() { LinkedComponentValues = { new Component() { Multimedia = new Multimedia() { Url = "_images/asd.png" } } } }); var slide2 = new FieldSet(); slide2.Add("heading", new Field() { Values = { "Slide 2" } }); slide2.Add("summary", new Field() { Values = { "Slide 2 Summary" } }); slide2.Add("image", new Field() { LinkedComponentValues = { new Component() { Multimedia = new Multimedia() { Url = "_images/asdc.png" } } } }); var embeddedList = new List<FieldSet>(); embeddedList.Add(slide1); embeddedList.Add(slide2); var carousel = new Component(); carousel.Fields.Add("title", new Field() { Values = { "My Carousel" } }); carousel.Fields.Add("embedded_list", new Field() { EmbeddedValues = embeddedList }); var carouselModel = ComponentViewModelBuilder.Build<CarouselViewModel>(carousel); Assert.That(carouselModel.EmbeddedCarouselItemViewModels.Count(), Is.EqualTo(2)); Assert.That(carouselModel.EmbeddedCarouselItemViewModels.First().Heading, Is.EqualTo("Slide 1")); Assert.That(carouselModel.EmbeddedCarouselItemViewModels.Skip(1).First().Heading, Is.EqualTo("Slide 2")); }
public static Dynamic.Field BuildField(TCM.Fields.ItemField tcmItemField, int currentLinkLevel, BuildManager manager) { Dynamic.Field f = new Dynamic.Field(); if (tcmItemField == null && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); return(f); } else if (tcmItemField == null && !manager.BuildProperties.PublishEmptyFields) { 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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { foreach (string v in sField.Values) { f.Values.Add(manager.PublishBinariesInRichTextField(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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { 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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { 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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { // add keyword values f.Keywords = new List <Keyword>(); // we will wrap each linked component in a ContentModel component foreach (TCM.Keyword kw in sField.Values) { f.Keywords.Add(manager.BuildKeyword(kw)); } if (!manager.BuildProperties.OmitValueLists) { f.Values = new List <string>(); foreach (TCM.Keyword kw in sField.Values) { f.Values.Add(kw.Title); } } KeywordFieldDefinition fieldDef = (KeywordFieldDefinition)sField.Definition; f.CategoryId = fieldDef.Category.Id; f.CategoryName = fieldDef.Category.Title; } f.FieldType = FieldType.Keyword; return(f); } if (tcmItemField is TCM.Fields.NumberField) { TCM.Fields.NumberField sField = (TCM.Fields.NumberField)tcmItemField; if (sField.Values.Count == 0 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { f.NumericValues = (List <double>)sField.Values; if (!manager.BuildProperties.OmitValueLists) { 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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(""); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { f.DateTimeValues = (List <DateTime>)sField.Values; if (!manager.BuildProperties.OmitValueLists) { 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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(tcmItemField.Name); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { // we will wrap each linked component in a ContentModel component f.LinkedComponentValues = new List <Dynamic.Component>(); int nextLinkLevel = currentLinkLevel - 1; if (MustFollowField(sField, manager)) { log.Debug(string.Format("found component link field named {0} with global followLinksPerField property set to false OR followLink set to true for this field", sField.Name)); } else { log.Debug(string.Format("found component link field named {0} with followLink set to false for this field", sField.Name)); nextLinkLevel = 0; } foreach (TCM.Component comp in sField.Values) { f.LinkedComponentValues.Add(manager.BuildComponent(comp, nextLinkLevel)); } if (!manager.BuildProperties.OmitValueLists) { 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 && manager.BuildProperties.PublishEmptyFields) { f.Values.Add(tcmItemField.Name); } else if (sField.Values.Count == 0 && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { // we will wrap each linked component in a ContentModel component f.LinkedComponentValues = new List <Dynamic.Component>(); int nextLinkLevel = currentLinkLevel - 1; if (MustFollowField(sField, manager)) { log.Debug(string.Format("found component link field named {0} with global followLinksPerField property set to false OR followLink set to true for this field", sField.Name)); } else { log.Debug(string.Format("found component link field named {0} with followLink set to false for this field", sField.Name)); nextLinkLevel = 0; } foreach (TCM.Component comp in sField.Values) { f.LinkedComponentValues.Add(manager.BuildComponent(comp, nextLinkLevel)); } if (!manager.BuildProperties.OmitValueLists) { 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; f.FieldType = FieldType.Embedded; f.EmbeddedValues = new List <Dynamic.FieldSet>(); bool hasValues = CheckIfEmbeddedFieldHasValues(sField); if (!hasValues && manager.BuildProperties.PublishEmptyFields) { Dynamic.FieldSet fields = new FieldSet(); Dynamic.Field fe = new Dynamic.Field(); f.EmbeddedSchema = manager.BuildSchema(((EmbeddedSchemaFieldDefinition)sField.Definition).EmbeddedSchema); EmbeddedSchemaFieldDefinition linksFieldDefinition = sField.Definition as EmbeddedSchemaFieldDefinition; ItemFields newItemField = new ItemFields(linksFieldDefinition.EmbeddedSchema); for (int i = 0; i < newItemField.Count; i++) { if (newItemField[i] is TCM.Fields.EmbeddedSchemaField) { TCM.Fields.EmbeddedSchemaField innerField = (TCM.Fields.EmbeddedSchemaField)newItemField[i]; if (innerField.Values.Count == 0) { Dynamic.FieldSet fieldsinner = new FieldSet(); Dynamic.Field fin = new Dynamic.Field(); fe.EmbeddedSchema = manager.BuildSchema(((EmbeddedSchemaFieldDefinition)innerField.Definition).EmbeddedSchema); EmbeddedSchemaFieldDefinition inlinksFieldDefinition = innerField.Definition as EmbeddedSchemaFieldDefinition; ItemFields newinItemField = new ItemFields(inlinksFieldDefinition.EmbeddedSchema); for (int n = 0; n < newinItemField.Count; n++) { fin.Name = newItemField[n].Name; fieldsinner.Add(newinItemField[n].Name, fin); } fe.EmbeddedValues.Add(fieldsinner); fe.Values.Add(""); } fe.Name = newItemField[i].Name; fields.Add(newItemField[i].Name, fe); } else { Dynamic.Field fein = manager.BuildField(newItemField[i], currentLinkLevel); fein.Values.Clear(); fields.Add(newItemField[i].Name, fein); } } f.EmbeddedValues.Add(fields); f.Values.Add(""); } else if (!hasValues && !manager.BuildProperties.PublishEmptyFields) { throw new FieldHasNoValueException(); } else { // we will wrap each linked component in a ContentModel component f.EmbeddedValues = new List <Dynamic.FieldSet>(); f.EmbeddedSchema = manager.BuildSchema(((EmbeddedSchemaFieldDefinition)sField.Definition).EmbeddedSchema); foreach (TCM.Fields.ItemFields embeddedFields in sField.Values) { f.EmbeddedValues.Add(manager.BuildFields(embeddedFields, currentLinkLevel)); } } return(f); } throw new FieldTypeNotDefinedException(); }