From afff2f986ef718ff4863fb120df0f1d16b6fa2fe Mon Sep 17 00:00:00 2001 From: chanond-w Date: Fri, 22 Dec 2017 03:25:41 +0700 Subject: [PATCH] Fix import export of identically named fields (#7912) --- CREDITS.txt | 4 +-- .../Drivers/ContentFieldDriver.cs | 25 +++++++++++++++---- .../Drivers/ContentPartDriver.cs | 20 +++++++++++---- .../Handlers/ExportContentContext.cs | 6 +++++ .../Handlers/ImportContentContext.cs | 24 ++++++++++++++++-- 5 files changed, 65 insertions(+), 14 deletions(-) diff --git a/CREDITS.txt b/CREDITS.txt index 51194d0d6..eca307584 100644 --- a/CREDITS.txt +++ b/CREDITS.txt @@ -187,7 +187,7 @@ License: Apache Software Foundation License 2.0 Lucene.net ----- -Website: http://incubator.apache.org/projects/lucene.net.html +Website: https://lucenenet.apache.org/ Copyright: Copyright (c) 2009 Apache Software Foundation License: Apache Software Foundation License 2.0 @@ -293,4 +293,4 @@ YUI ----- Website: http://developer.yahoo.com/yui/ Copyright: Copyright (c) 2010, Yahoo! Inc. -License: New BSD \ No newline at end of file +License: New BSD diff --git a/src/Orchard/ContentManagement/Drivers/ContentFieldDriver.cs b/src/Orchard/ContentManagement/Drivers/ContentFieldDriver.cs index 72d883568..f86e15dc5 100644 --- a/src/Orchard/ContentManagement/Drivers/ContentFieldDriver.cs +++ b/src/Orchard/ContentManagement/Drivers/ContentFieldDriver.cs @@ -65,23 +65,38 @@ namespace Orchard.ContentManagement.Drivers { } void IContentFieldDriver.Importing(ImportContentContext context) { - Process(context.ContentItem, (part, field) => Importing(part, field, context), context.Logger); + Process(context.ContentItem, (part, field) => { + context.Prefix = part.PartDefinition.Name; + Importing(part, field, context); + }, context.Logger); } void IContentFieldDriver.Imported(ImportContentContext context) { - Process(context.ContentItem, (part, field) => Imported(part, field, context), context.Logger); + Process(context.ContentItem, (part, field) => { + context.Prefix = part.PartDefinition.Name; + Imported(part, field, context); + }, context.Logger); } void IContentFieldDriver.ImportCompleted(ImportContentContext context) { - Process(context.ContentItem, (part, field) => ImportCompleted(part, field, context), context.Logger); + Process(context.ContentItem, (part, field) => { + context.Prefix = part.PartDefinition.Name; + ImportCompleted(part, field, context); + }, context.Logger); } void IContentFieldDriver.Exporting(ExportContentContext context) { - Process(context.ContentItem, (part, field) => Exporting(part, field, context), context.Logger); + Process(context.ContentItem, (part, field) => { + context.Prefix = part.PartDefinition.Name; + Exporting(part, field, context); + }, context.Logger); } void IContentFieldDriver.Exported(ExportContentContext context) { - Process(context.ContentItem, (part, field) => Exported(part, field, context), context.Logger); + Process(context.ContentItem, (part, field) => { + context.Prefix = part.PartDefinition.Name; + Exported(part, field, context); + }, context.Logger); } void IContentFieldDriver.Cloning(CloneContentContext context) { diff --git a/src/Orchard/ContentManagement/Drivers/ContentPartDriver.cs b/src/Orchard/ContentManagement/Drivers/ContentPartDriver.cs index d05328607..b6f5c679c 100644 --- a/src/Orchard/ContentManagement/Drivers/ContentPartDriver.cs +++ b/src/Orchard/ContentManagement/Drivers/ContentPartDriver.cs @@ -78,32 +78,42 @@ namespace Orchard.ContentManagement.Drivers { void IContentPartDriver.Importing(ImportContentContext context) { var part = context.ContentItem.As(); - if (part != null) + if (part != null) { + context.Prefix = string.Empty; Importing(part, context); + } } void IContentPartDriver.Imported(ImportContentContext context) { var part = context.ContentItem.As(); - if (part != null) + if (part != null) { + context.Prefix = string.Empty; Imported(part, context); + } } void IContentPartDriver.ImportCompleted(ImportContentContext context) { var part = context.ContentItem.As(); - if (part != null) + if (part != null) { + context.Prefix = string.Empty; ImportCompleted(part, context); + } } void IContentPartDriver.Exporting(ExportContentContext context) { var part = context.ContentItem.As(); - if (part != null) + if (part != null) { + context.Prefix = string.Empty; Exporting(part, context); + } } void IContentPartDriver.Exported(ExportContentContext context) { var part = context.ContentItem.As(); - if (part != null) + if (part != null) { + context.Prefix = string.Empty; Exported(part, context); + } } void IContentPartDriver.Cloning(CloneContentContext context) { diff --git a/src/Orchard/ContentManagement/Handlers/ExportContentContext.cs b/src/Orchard/ContentManagement/Handlers/ExportContentContext.cs index 142eaa111..a1bdffe44 100644 --- a/src/Orchard/ContentManagement/Handlers/ExportContentContext.cs +++ b/src/Orchard/ContentManagement/Handlers/ExportContentContext.cs @@ -2,6 +2,7 @@ using System.Xml.Linq; namespace Orchard.ContentManagement.Handlers { public class ExportContentContext : ContentContextBase { + public string Prefix { get; set; } public XElement Data { get; set; } /// @@ -9,12 +10,17 @@ namespace Orchard.ContentManagement.Handlers { /// public bool Exclude { get; set; } + private readonly string Separator = @"."; + public ExportContentContext(ContentItem contentItem, XElement data) : base(contentItem) { Data = data; } public XElement Element(string elementName) { + if (!string.IsNullOrEmpty(Prefix)) + elementName = string.Join(Separator, Prefix, elementName); + var element = Data.Element(elementName); if (element == null) { element = new XElement(elementName); diff --git a/src/Orchard/ContentManagement/Handlers/ImportContentContext.cs b/src/Orchard/ContentManagement/Handlers/ImportContentContext.cs index 0722471de..32af565e2 100644 --- a/src/Orchard/ContentManagement/Handlers/ImportContentContext.cs +++ b/src/Orchard/ContentManagement/Handlers/ImportContentContext.cs @@ -1,10 +1,13 @@ using System; using System.Xml.Linq; +using System.Linq; namespace Orchard.ContentManagement.Handlers { public class ImportContentContext : ContentContextBase { public XElement Data { get; set; } private ImportContentSession Session { get; set; } + private readonly string Separator = @"."; + public string Prefix { get; set; } public ImportContentContext(ContentItem contentItem, XElement data, ImportContentSession importContentSession) : base(contentItem) { @@ -13,7 +16,18 @@ namespace Orchard.ContentManagement.Handlers { } public string Attribute(string elementName, string attributeName) { - var element = Data.Element(elementName); + // Step one : fetch element with prefix + var element = Data.Element(AdjustElementName(elementName)); + // Step two : fetch elements without prefix + if (element == null) { + var elements = Data.Elements(elementName); + if (elements != null && elements.Count() > 1) { + element = elements.Last(); + } else if (elements != null && elements.Count() == 1) { + element = elements.First(); + } + } + if (element != null) { var attribute = element.Attribute(attributeName); if (attribute != null) @@ -23,7 +37,7 @@ namespace Orchard.ContentManagement.Handlers { } public string ChildEl(string elementName, string childElementName) { - var element = Data.Element(elementName); + var element = Data.Element(AdjustElementName(elementName)); return element == null ? null : element.El(childElementName); } @@ -68,5 +82,11 @@ namespace Orchard.ContentManagement.Handlers { public ContentItem GetItemFromSession(string id) { return Session.Get(id); } + + private string AdjustElementName(string elementName) { + if (!string.IsNullOrEmpty(Prefix) && !elementName.StartsWith(Prefix + Separator)) + return string.Join(Separator, Prefix, elementName); + return elementName; + } } } \ No newline at end of file