From 2142c5cde07b13d7eb4ae3539dce6084d82afad6 Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Thu, 1 Jul 2021 11:44:33 +0200 Subject: [PATCH] Update XML format to support import/export of new property group data --- .../Packaging/PackageDataInstallation.cs | 37 ++++-- .../Services/Implement/EntityXmlSerializer.cs | 116 +++++++----------- 2 files changed, 72 insertions(+), 81 deletions(-) diff --git a/src/Umbraco.Core/Packaging/PackageDataInstallation.cs b/src/Umbraco.Core/Packaging/PackageDataInstallation.cs index 016b349282..f1fd48e237 100644 --- a/src/Umbraco.Core/Packaging/PackageDataInstallation.cs +++ b/src/Umbraco.Core/Packaging/PackageDataInstallation.cs @@ -690,7 +690,7 @@ namespace Umbraco.Core.Packaging } UpdateContentTypesAllowedTemplates(contentType, infoElement.Element("AllowedTemplates"), defaultTemplateElement); - UpdateContentTypesTabs(contentType, documentType.Element("Tabs")); + UpdateContentTypesPropertyGroups(contentType, documentType.Element("Tabs")); UpdateContentTypesProperties(contentType, documentType.Element("GenericProperties")); return contentType; @@ -734,28 +734,41 @@ namespace Umbraco.Core.Packaging } } - private void UpdateContentTypesTabs(IContentType contentType, XElement tabElement) + private void UpdateContentTypesPropertyGroups(IContentType contentType, XElement propertyGroupsContainer) { - if (tabElement == null) + if (propertyGroupsContainer == null) return; - var tabs = tabElement.Elements("Tab"); - foreach (var tab in tabs) + var propertyGroupElements = propertyGroupsContainer.Elements("Tab"); + foreach (var propertyGroupElement in propertyGroupElements) { - var id = tab.Element("Id").Value;//Do we need to use this for tracking? - var caption = tab.Element("Caption").Value; + var caption = propertyGroupElement.Element("Caption").Value; - if (contentType.PropertyGroups.Contains(caption) == false) + if (Guid.TryParse(propertyGroupElement.Element("Key")?.Value, out var key)) + { + contentType.AddPropertyGroup(key, caption); + } + else { contentType.AddPropertyGroup(caption); - } - int sortOrder; - if (tab.Element("SortOrder") != null && int.TryParse(tab.Element("SortOrder").Value, out sortOrder)) + var propertyGroup = contentType.PropertyGroups[caption]; + + if (Enum.TryParse(propertyGroupElement.Element("Type")?.Value, out var type)) + { + propertyGroup.Type = type; + } + + if (Guid.TryParse(propertyGroupElement.Element("ParentKey")?.Value, out var parentKey)) + { + propertyGroup.ParentKey = parentKey; + } + + if (int.TryParse(propertyGroupElement.Element("SortOrder")?.Value, out var sortOrder)) { // Override the sort order with the imported value - contentType.PropertyGroups[caption].SortOrder = sortOrder; + propertyGroup.SortOrder = sortOrder; } } } diff --git a/src/Umbraco.Core/Services/Implement/EntityXmlSerializer.cs b/src/Umbraco.Core/Services/Implement/EntityXmlSerializer.cs index 8cfcaa1182..db955ab4fe 100644 --- a/src/Umbraco.Core/Services/Implement/EntityXmlSerializer.cs +++ b/src/Umbraco.Core/Services/Implement/EntityXmlSerializer.cs @@ -344,40 +344,9 @@ namespace Umbraco.Core.Services.Implement structure.Add(new XElement("MediaType", allowedType.Alias)); } - var genericProperties = new XElement("GenericProperties"); // actually, all of them - foreach (var propertyType in mediaType.PropertyTypes) - { - var definition = _dataTypeService.GetDataType(propertyType.DataTypeId); + var genericProperties = new XElement("GenericProperties", SerializePropertyTypes(mediaType.PropertyTypes, mediaType.PropertyGroups)); // actually, all of them - var propertyGroup = propertyType.PropertyGroupId == null // true generic property - ? null - : mediaType.PropertyGroups.FirstOrDefault(x => x.Id == propertyType.PropertyGroupId.Value); - - var genericProperty = new XElement("GenericProperty", - new XElement("Name", propertyType.Name), - new XElement("Alias", propertyType.Alias), - new XElement("Type", propertyType.PropertyEditorAlias), - new XElement("Definition", definition.Key), - new XElement("Tab", propertyGroup == null ? "" : propertyGroup.Name), - new XElement("Mandatory", propertyType.Mandatory.ToString()), - new XElement("MandatoryMessage", propertyType.MandatoryMessage), - new XElement("Validation", propertyType.ValidationRegExp), - new XElement("ValidationRegExpMessage", propertyType.ValidationRegExpMessage), - new XElement("LabelOnTop", propertyType.LabelOnTop), - new XElement("Description", new XCData(propertyType.Description))); - genericProperties.Add(genericProperty); - } - - var tabs = new XElement("Tabs"); - foreach (var propertyGroup in mediaType.PropertyGroups) - { - var tab = new XElement("Tab", - new XElement("Id", propertyGroup.Id.ToString(CultureInfo.InvariantCulture)), - new XElement("Caption", propertyGroup.Name), - new XElement("SortOrder", propertyGroup.SortOrder)); - - tabs.Add(tab); - } + var tabs = new XElement("Tabs", SerializePropertyGroups(mediaType.PropertyGroups)); // TODO Rename to PropertyGroups var xml = new XElement("MediaType", info, @@ -474,43 +443,9 @@ namespace Umbraco.Core.Services.Implement structure.Add(new XElement("DocumentType", allowedType.Alias)); } - var genericProperties = new XElement("GenericProperties"); // actually, all of them - foreach (var propertyType in contentType.PropertyTypes) - { - var definition = _dataTypeService.GetDataType(propertyType.DataTypeId); + var genericProperties = new XElement("GenericProperties", SerializePropertyTypes(contentType.PropertyTypes, contentType.PropertyGroups)); // actually, all of them - var propertyGroup = propertyType.PropertyGroupId == null // true generic property - ? null - : contentType.PropertyGroups.FirstOrDefault(x => x.Id == propertyType.PropertyGroupId.Value); - - var genericProperty = new XElement("GenericProperty", - new XElement("Name", propertyType.Name), - new XElement("Alias", propertyType.Alias), - new XElement("Key", propertyType.Key), - new XElement("Type", propertyType.PropertyEditorAlias), - new XElement("Definition", definition.Key), - new XElement("Tab", propertyGroup == null ? "" : propertyGroup.Name), - new XElement("SortOrder", propertyType.SortOrder), - new XElement("Mandatory", propertyType.Mandatory.ToString()), - new XElement("LabelOnTop", propertyType.LabelOnTop.ToString()), - propertyType.MandatoryMessage != null ? new XElement("MandatoryMessage", propertyType.MandatoryMessage) : null, - propertyType.ValidationRegExp != null ? new XElement("Validation", propertyType.ValidationRegExp) : null, - propertyType.ValidationRegExpMessage != null ? new XElement("ValidationRegExpMessage", propertyType.ValidationRegExpMessage) : null, - propertyType.Description != null ? new XElement("Description", new XCData(propertyType.Description)) : null, - new XElement("Variations", propertyType.Variations.ToString())); - - genericProperties.Add(genericProperty); - } - - var tabs = new XElement("Tabs"); - foreach (var propertyGroup in contentType.PropertyGroups) - { - var tab = new XElement("Tab", - new XElement("Id", propertyGroup.Id.ToString(CultureInfo.InvariantCulture)), - new XElement("Caption", propertyGroup.Name), - new XElement("SortOrder", propertyGroup.SortOrder)); - tabs.Add(tab); - } + var tabs = new XElement("Tabs", SerializePropertyGroups(contentType.PropertyGroups)); // TODO Rename to PropertyGroups var xml = new XElement("DocumentType", info, @@ -536,6 +471,49 @@ namespace Umbraco.Core.Services.Implement return xml; } + private IEnumerable SerializePropertyTypes(IEnumerable propertyTypes, IEnumerable propertyGroups) + { + foreach (var propertyType in propertyTypes) + { + var definition = _dataTypeService.GetDataType(propertyType.DataTypeId); + + var propertyGroup = propertyType.PropertyGroupId == null // true generic property + ? null + : propertyGroups.FirstOrDefault(x => x.Id == propertyType.PropertyGroupId.Value); + + var genericProperty = new XElement("GenericProperty", + new XElement("Name", propertyType.Name), + new XElement("Alias", propertyType.Alias), + new XElement("Key", propertyType.Key), + new XElement("Type", propertyType.PropertyEditorAlias), + new XElement("Definition", definition.Key), + new XElement("Tab", propertyGroup == null ? "" : propertyGroup.Name), // TODO Rename to PropertyGroupKey + new XElement("SortOrder", propertyType.SortOrder), + new XElement("Mandatory", propertyType.Mandatory.ToString()), + new XElement("LabelOnTop", propertyType.LabelOnTop.ToString()), + propertyType.MandatoryMessage != null ? new XElement("MandatoryMessage", propertyType.MandatoryMessage) : null, + propertyType.ValidationRegExp != null ? new XElement("Validation", propertyType.ValidationRegExp) : null, + propertyType.ValidationRegExpMessage != null ? new XElement("ValidationRegExpMessage", propertyType.ValidationRegExpMessage) : null, + propertyType.Description != null ? new XElement("Description", new XCData(propertyType.Description)) : null, + new XElement("Variations", propertyType.Variations.ToString())); + + yield return genericProperty; + } + } + + private IEnumerable SerializePropertyGroups(IEnumerable propertyGroups) + { + foreach (var propertyGroup in propertyGroups) + { + yield return new XElement("Tab", // TODO Rename to PropertyGroup + new XElement("Caption", propertyGroup.Name), // TODO Rename to Name + new XElement("Key", propertyGroup.Key), + new XElement("ParentKey", propertyGroup.ParentKey), + new XElement("Type", propertyGroup.Type.ToString()), + new XElement("SortOrder", propertyGroup.SortOrder)); + } + } + // exports an IContentBase (IContent, IMedia or IMember) as an XElement. private XElement SerializeContentBase(IContentBase contentBase, string urlValue, string nodeName, bool published) {