#21015: Adding methods to automatically export and import infoset data.

Work Item: 21015
This commit is contained in:
Lombiq
2015-01-16 23:08:17 +01:00
committed by Zoltán Lehóczky
parent 7df7e1086a
commit f35c4df120

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml.Linq;
using Orchard.ContentManagement.FieldStorage.InfosetStorage;
using Orchard.ContentManagement.Handlers;
using Orchard.ContentManagement.MetaData;
using Orchard.DisplayManagement;
@@ -129,6 +131,62 @@ namespace Orchard.ContentManagement.Drivers {
protected virtual void Exporting(TContent part, ExportContentContext context) { }
protected virtual void Exported(TContent part, ExportContentContext context) { }
/// <summary>
/// Import a content part's previously exported (through the <see cref="ExportInfoset"/> method) infoset data. Note that you can
/// only import data this way that isn't also stored in records (since only the infoset will be populated but not the part record).
/// </summary>
/// <param name="part">The content part used for import.</param>
/// <param name="context">The context object of the import operation.</param>
protected static void ImportInfoset(TContent part, ImportContentContext context) {
if (!part.Has<InfosetPart>()) {
return;
}
Action<XElement, bool> importInfoset = (element, versioned) => {
if (element == null) {
return;
}
foreach (var attribute in element.Attributes()) {
part.Store(attribute.Name.ToString(), attribute.Value, versioned);
}
};
importInfoset(context.Data.Element(GetInfosetXmlElementName(part, true)), true);
importInfoset(context.Data.Element(GetInfosetXmlElementName(part, false)), false);
}
/// <summary>
/// Export a content part's data that is stored in the infoset.
/// </summary>
/// <param name="part">The content part used for export.</param>
/// <param name="context">The context object of the export operation.</param>
protected static void ExportInfoset(TContent part, ExportContentContext context) {
var infosetPart = part.As<InfosetPart>();
if (infosetPart == null) {
return;
}
Action<XElement, bool> exportInfoset = (element, versioned) => {
if (element == null) {
return;
}
var elementName = GetInfosetXmlElementName(part, versioned);
foreach (var attribute in element.Attributes()) {
context.Element(elementName).SetAttributeValue(attribute.Name, attribute.Value);
}
};
exportInfoset(infosetPart.VersionInfoset.Element.Element(part.PartDefinition.Name), true);
exportInfoset(infosetPart.Infoset.Element.Element(part.PartDefinition.Name), false);
}
private static string GetInfosetXmlElementName(TContent part, bool versioned) {
return part.PartDefinition.Name + "-" + (versioned ? "VersionInfoset" : "Infoset");
}
[Obsolete("Provided while transitioning to factory variations")]
public ContentShapeResult ContentShape(IShape shape) {
return ContentShapeImplementation(shape.Metadata.Type, ctx => shape).Location("Content");