private IEnumerable <SemanticSchemaFieldData> GetSemanticSchemaFields( IEnumerable <ItemFieldDefinition> schemaFields, SemanticTypeData[] semanticTypes, Schema schema, string contextPath ) { List <SemanticSchemaFieldData> semanticSchemaFields = new List <SemanticSchemaFieldData>(); if (schemaFields == null) { return(semanticSchemaFields); } foreach (ItemFieldDefinition schemaField in schemaFields) { EmbeddedSchemaFieldDefinition embeddedSchemaField = schemaField as EmbeddedSchemaFieldDefinition; SemanticSchemaFieldData semanticSchemaField = (embeddedSchemaField == null) ? new SemanticSchemaFieldData() : new EmbeddedSemanticSchemaFieldData(); semanticSchemaField.Name = schemaField.Name; semanticSchemaField.Path = $"{contextPath}/{schemaField.Name}"; semanticSchemaField.IsMultiValue = schemaField.MaxOccurs != 1; semanticSchemaField.Semantics = GetSemanticProperties(schemaField, semanticTypes, schema).ToArray(); semanticSchemaField.FieldType = GetFieldType(schemaField); if (embeddedSchemaField == null) { semanticSchemaField.Fields = new SemanticSchemaFieldData[0]; } else { Schema embeddedSchema = embeddedSchemaField.EmbeddedSchema; EmbeddedSemanticSchemaFieldData fieldData = (EmbeddedSemanticSchemaFieldData)semanticSchemaField; fieldData.Id = embeddedSchema.Id.ItemId; fieldData.Title = embeddedSchema.Title; fieldData.RootElementName = embeddedSchema.RootElementName; semanticSchemaField.Fields = GetSemanticSchemaFields( embeddedSchemaField.EmbeddedFields, semanticTypes.Concat(GetSemanticTypes(embeddedSchema)).ToArray(), embeddedSchema, semanticSchemaField.Path ).ToArray(); } semanticSchemaFields.Add(semanticSchemaField); } return(semanticSchemaFields); }
private IEnumerable <SemanticSchemaFieldData> GetSemanticSchemaFields( IEnumerable <ItemFieldDefinition> schemaFields, SemanticTypeData[] semanticTypes, Schema schema, string contextPath ) { List <SemanticSchemaFieldData> semanticSchemaFields = new List <SemanticSchemaFieldData>(); if (schemaFields == null) { return(semanticSchemaFields); } foreach (ItemFieldDefinition schemaField in schemaFields) { SemanticSchemaFieldData semanticSchemaField = new SemanticSchemaFieldData { Name = schemaField.Name, Path = string.Format("{0}/{1}", contextPath, schemaField.Name), IsMultiValue = schemaField.MaxOccurs != 1, Semantics = GetSemanticProperties(schemaField, semanticTypes, schema).ToArray() }; EmbeddedSchemaFieldDefinition embeddedSchemaField = schemaField as EmbeddedSchemaFieldDefinition; if (embeddedSchemaField == null) { semanticSchemaField.Fields = new SemanticSchemaFieldData[0]; } else { Schema embeddedSchema = embeddedSchemaField.EmbeddedSchema; semanticSchemaField.Fields = GetSemanticSchemaFields( embeddedSchemaField.EmbeddedFields, semanticTypes.Concat(GetSemanticTypes(embeddedSchema)).ToArray(), embeddedSchema, semanticSchemaField.Path ).ToArray(); } semanticSchemaFields.Add(semanticSchemaField); } return(semanticSchemaFields); }
/// <summary> /// Process images /// </summary> /// <param name="htmlDoc"></param> /// <param name="taggedImageList"></param> /// <param name="parentFolder"></param> /// <param name="componentTitle"></param> /// <param name="archive"></param> private static void ProcessImages(HtmlDocument htmlDoc, EmbeddedSchemaField taggedImageList, OrganizationalItem parentFolder, String componentTitle, ZipArchive archive) { EmbeddedSchemaFieldDefinition taggedImageField = (EmbeddedSchemaFieldDefinition)taggedImageList.Definition; Schema taggedImageSchema = taggedImageField.EmbeddedSchema; SchemaFields taggedImageSchemaFields = new SchemaFields(taggedImageSchema); MultimediaLinkFieldDefinition mmLinkFieldDef = (MultimediaLinkFieldDefinition)taggedImageSchemaFields.Fields.Where(field => field.Name.Equals("image")).First(); Schema imageSchema = mmLinkFieldDef.AllowedTargetSchemas[0]; Folder imageFolder = null; var taggedImageNames = new List <string>(); var foundImages = new Dictionary <string, Component>(); var imageFolderWebDavUrl = parentFolder.WebDavUrl + "/" + componentTitle + " Images"; foreach (var node in htmlDoc.DocumentNode.QuerySelectorAll("[data-image-name]")) { var imageUrl = node.Attributes["src"]; //Logger.Write("Processing image tag...", "CampaignZipImporter", LogCategory.Custom, System.Diagnostics.TraceEventType.Information); var taggedImageName = node.Attributes["data-image-name"]; if (imageUrl != null && taggedImageName != null && !taggedImageNames.Contains(taggedImageName.Value) && !IsEntryAlreadyDefined(taggedImageName.Value, taggedImageList)) { //Logger.Write("Adding image with name: " + taggedImageName.Value, "CampaignZipImporter", LogCategory.Custom, System.Diagnostics.TraceEventType.Information); if (imageUrl != null && !imageUrl.Value.StartsWith("http")) { if (imageFolder == null) { if (parentFolder.Session.IsExistingObject(imageFolderWebDavUrl)) { imageFolder = (Folder)parentFolder.Session.GetObject(imageFolderWebDavUrl); } else { // Create folder // imageFolder = new Folder(parentFolder.Session, parentFolder.Id); imageFolder.Title = componentTitle + " Images"; imageFolder.Save(); } } } // If an absolute image URL // else if (imageUrl != null && imageUrl.Value.StartsWith("http")) { var url = imageUrl.Value; string parameters = null; if (url.Contains("?")) { var parts = url.Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries); url = parts[0]; parameters = parts[1]; } var taggedImageXml = new StringBuilder(); taggedImageXml.Append("<TaggedImage xmlns:xlink=\"http://www.w3.org/1999/xlink\"><name>"); taggedImageXml.Append(taggedImageName.Value); taggedImageXml.Append("</name><imageUrl>"); taggedImageXml.Append(SecurityElement.Escape(url)); taggedImageXml.Append("</imageUrl>"); if (parameters != null) { taggedImageXml.Append("<parameters>"); taggedImageXml.Append(SecurityElement.Escape(parameters)); taggedImageXml.Append("</parameters>"); } taggedImageXml.Append("</TaggedImage>"); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(taggedImageXml.ToString()); ItemFields taggedImage = new ItemFields(xmlDoc.DocumentElement, taggedImageSchema); taggedImageList.Values.Add(taggedImage); taggedImageNames.Add(taggedImageName.Value); continue; } ZipArchiveEntry imageEntry = archive.GetEntry(imageUrl.Value); if (imageEntry != null) { Component imageComponent; if (foundImages.TryGetValue(imageUrl.Value, out imageComponent) == false) { var imageName = Path.GetFileName(imageUrl.Value); var imageWebDavUri = imageFolderWebDavUrl + "/" + imageName; if (parentFolder.Session.IsExistingObject(imageWebDavUri)) { imageComponent = (Component)parentFolder.Session.GetObject(imageWebDavUri); } else { imageComponent = new Component(parentFolder.Session, imageFolder.Id); var metadataXml = new XmlDocument(); metadataXml.LoadXml("<Metadata xmlns=\"" + imageSchema.NamespaceUri + "\"/>"); imageComponent.Schema = imageSchema; imageComponent.Metadata = metadataXml.DocumentElement; var extension = Path.GetExtension(imageUrl.Value); imageComponent.Title = imageName.Replace(extension, ""); // Set title without extension extension = extension.ToLower().Replace(".", ""); bool foundMMType = false; foreach (var mmType in imageSchema.AllowedMultimediaTypes) { if (mmType.FileExtensions.Contains(extension)) { imageComponent.BinaryContent.MultimediaType = mmType; foundMMType = true; break; } } if (!foundMMType) { Logger.Write("Could not find multimedia type for image extension: " + extension, "CampaignZipImporter", LogCategory.Custom, System.Diagnostics.TraceEventType.Error); } imageComponent.BinaryContent.UploadFromStream = imageEntry.Open(); imageComponent.BinaryContent.Filename = imageName; imageComponent.Save(true); } foundImages.Add(imageUrl.Value, imageComponent); } var taggedImageXml = new StringBuilder(); taggedImageXml.Append("<TaggedImage xmlns:xlink=\"http://www.w3.org/1999/xlink\"><name>"); taggedImageXml.Append(taggedImageName.Value); taggedImageXml.Append("</name><image xlink:type=\"simple\" xlink:href=\""); taggedImageXml.Append(imageComponent.Id); taggedImageXml.Append("\" xlink:title=\""); taggedImageXml.Append(imageComponent.Title); taggedImageXml.Append("\" /></TaggedImage>"); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(taggedImageXml.ToString()); ItemFields taggedImage = new ItemFields(xmlDoc.DocumentElement, taggedImageSchema); taggedImageList.Values.Add(taggedImage); taggedImageNames.Add(taggedImageName.Value); } } } }
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 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(); }
private List <Component> GatherLinkedComponents(Component component) { _log.Debug($"Gathering linked components for component {component.Title}"); List <ItemFields> fieldList = new List <ItemFields>(); List <Component> components = new List <Component>(); if (component.Content != null) { fieldList.Add(new ItemFields(component.Content, component.Schema)); } if (component.Metadata != null) { fieldList.Add(new ItemFields(component.Metadata, component.MetadataSchema)); } var componentLinkFields = new List <ComponentLinkField>(); var embeddedSchemaFields = new List <EmbeddedSchemaField>(); foreach (var fields in fieldList) { foreach (var field in fields) { if (field is ComponentLinkField) { componentLinkFields.Add((ComponentLinkField)field); } if (field is EmbeddedSchemaField) { embeddedSchemaFields.Add((EmbeddedSchemaField)field); } } } for (int i = 0; i < embeddedSchemaFields.Count; i++) { EmbeddedSchemaField linkField = embeddedSchemaFields[i]; EmbeddedSchemaFieldDefinition linksFieldDefinition = linkField.Definition as EmbeddedSchemaFieldDefinition; if (linksFieldDefinition?.EmbeddedSchema == null) { continue; } TcmUri id = linksFieldDefinition.EmbeddedSchema.Id; // force schema load foreach (var embeddedFields in linkField.Values) { foreach (var embeddedField in embeddedFields) { if (embeddedField is ComponentLinkField) { componentLinkFields.Add((ComponentLinkField)embeddedField); } if (embeddedField is EmbeddedSchemaField) { embeddedSchemaFields.Add((EmbeddedSchemaField)embeddedField); } } } } foreach (var linkField in componentLinkFields) { if (linkField.Values != null) { components.AddRange(linkField.Values); } } return(components); }
/// <summary> /// Process images /// </summary> /// <param name="htmlDoc"></param> /// <param name="taggedImageList"></param> /// <param name="parentFolder"></param> /// <param name="componentTitle"></param> /// <param name="archive"></param> private static void ProcessImages(HtmlDocument htmlDoc, EmbeddedSchemaField taggedImageList, OrganizationalItem parentFolder, String componentTitle, ZipArchive archive) { EmbeddedSchemaFieldDefinition taggedImageField = (EmbeddedSchemaFieldDefinition)taggedImageList.Definition; Schema taggedImageSchema = taggedImageField.EmbeddedSchema; SchemaFields taggedImageSchemaFields = new SchemaFields(taggedImageSchema); MultimediaLinkFieldDefinition mmLinkFieldDef = (MultimediaLinkFieldDefinition)taggedImageSchemaFields.Fields[1]; Schema imageSchema = mmLinkFieldDef.AllowedTargetSchemas[0]; Folder imageFolder = null; var taggedImageNames = new List <string>(); var foundImages = new Dictionary <string, Component>(); foreach (var node in htmlDoc.DocumentNode.QuerySelectorAll("[data-image-name]")) { if (imageFolder == null) { imageFolder = new Folder(parentFolder.Session, parentFolder.Id); imageFolder.Title = componentTitle + " " + "Images"; imageFolder.Save(); } //Logger.Write("Processing image tag...", "CampaignZipImporter", LogCategory.Custom, System.Diagnostics.TraceEventType.Information); var imageUrl = node.Attributes["src"]; var taggedImageName = node.Attributes["data-image-name"]; if (imageUrl != null && taggedImageName != null && !taggedImageNames.Contains(taggedImageName.Value)) { ZipArchiveEntry imageEntry = archive.GetEntry(imageUrl.Value); if (imageEntry != null) { Component imageComponent; if (foundImages.TryGetValue(imageUrl.Value, out imageComponent) == false) { imageComponent = new Component(parentFolder.Session, imageFolder.Id); var imageName = Path.GetFileName(imageUrl.Value); var metadataXml = new XmlDocument(); metadataXml.LoadXml("<Metadata xmlns=\"" + imageSchema.NamespaceUri + "\"/>"); imageComponent.Schema = imageSchema; imageComponent.Metadata = metadataXml.DocumentElement; imageComponent.Title = imageName; var extension = Path.GetExtension(imageUrl.Value).ToLower(); bool foundMMType = false; foreach (var mmType in imageSchema.AllowedMultimediaTypes) { if (mmType.FileExtensions.Contains(extension)) { imageComponent.BinaryContent.MultimediaType = mmType; foundMMType = true; break; } } if (!foundMMType) { Logger.Write("Could not find multimedia type for image extension: " + extension, "CampaignZipImporter", LogCategory.Custom, System.Diagnostics.TraceEventType.Error); } imageComponent.BinaryContent.UploadFromStream = imageEntry.Open(); imageComponent.BinaryContent.Filename = imageName; imageComponent.Save(true); foundImages.Add(imageUrl.Value, imageComponent); } var taggedImageXml = new StringBuilder(); taggedImageXml.Append("<TaggedImage xmlns:xlink=\"http://www.w3.org/1999/xlink\"><name>"); taggedImageXml.Append(taggedImageName.Value); taggedImageXml.Append("</name><image xlink:type=\"simple\" xlink:href=\""); taggedImageXml.Append(imageComponent.Id); taggedImageXml.Append("\" xlink:title=\""); taggedImageXml.Append(imageComponent.Title); taggedImageXml.Append("\" /></TaggedImage>"); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(taggedImageXml.ToString()); ItemFields taggedImage = new ItemFields(xmlDoc.DocumentElement, taggedImageSchema); taggedImageList.Values.Add(taggedImage); taggedImageNames.Add(taggedImageName.Value); } } } }