mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-23 04:43:35 +08:00
ContentManager::Import implementation.
Adding an import session used by the data recipe handler and the content manager. --HG-- branch : dev
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Recipes.Models;
|
||||
@@ -24,8 +25,24 @@ namespace Orchard.ImportExport.RecipeHandlers {
|
||||
return;
|
||||
}
|
||||
|
||||
var importContentSession = new ImportContentSession(_orchardServices.ContentManager);
|
||||
foreach (var element in recipeContext.RecipeStep.Step.Elements()) {
|
||||
_orchardServices.ContentManager.Import(element);
|
||||
var elementId = element.Attribute("Id");
|
||||
if (elementId == null)
|
||||
continue;
|
||||
|
||||
var identity = elementId.Value;
|
||||
|
||||
var item = importContentSession.Get(identity);
|
||||
if (item == null) {
|
||||
item = _orchardServices.ContentManager.New(element.Name.LocalName);
|
||||
_orchardServices.ContentManager.Create(item);
|
||||
importContentSession.Store(identity, item);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var element in recipeContext.RecipeStep.Step.Elements()) {
|
||||
_orchardServices.ContentManager.Import(element, importContentSession);
|
||||
}
|
||||
|
||||
recipeContext.Executed = true;
|
||||
|
@@ -398,6 +398,33 @@ namespace Orchard.ContentManagement {
|
||||
return query.ForPart<ContentItem>();
|
||||
}
|
||||
|
||||
public void Import(XElement element, ImportContentSession importContentSession) {
|
||||
var elementId = element.Attribute("Id");
|
||||
if (elementId == null)
|
||||
return;
|
||||
|
||||
var identity = elementId.Value;
|
||||
var item = importContentSession.Get(identity);
|
||||
if (item == null) {
|
||||
item = New(element.Name.LocalName);
|
||||
Create(item);
|
||||
importContentSession.Store(identity, item);
|
||||
}
|
||||
else {
|
||||
item = Get(item.Id, VersionOptions.DraftRequired);
|
||||
}
|
||||
|
||||
var context = new ImportContentContext(item, element);
|
||||
foreach (var contentHandler in Handlers) {
|
||||
contentHandler.Importing(context);
|
||||
}
|
||||
foreach (var contentHandler in Handlers) {
|
||||
contentHandler.Imported(context);
|
||||
}
|
||||
|
||||
Publish(item);
|
||||
}
|
||||
|
||||
public XElement Export(ContentItem contentItem) {
|
||||
var context = new ExportContentContext(contentItem, new XElement(contentItem.ContentType));
|
||||
|
||||
@@ -409,14 +436,12 @@ namespace Orchard.ContentManagement {
|
||||
contentHandler.Exported(context);
|
||||
}
|
||||
|
||||
// Put version information in the id.
|
||||
context.Data.SetAttributeValue("Id", GetItemMetadata(contentItem).Identity.ToString());
|
||||
|
||||
return context.Data;
|
||||
}
|
||||
|
||||
public void Import(XElement element) {
|
||||
}
|
||||
|
||||
public void Flush() {
|
||||
_contentItemRepository.Flush();
|
||||
}
|
||||
@@ -441,6 +466,39 @@ namespace Orchard.ContentManagement {
|
||||
}
|
||||
}
|
||||
|
||||
public class ImportContentSession {
|
||||
private readonly IContentManager _contentManager;
|
||||
|
||||
// order of x500 elements.
|
||||
private readonly Dictionary<string, ContentItem> _dictionary;
|
||||
|
||||
public ImportContentSession(IContentManager contentManager) {
|
||||
_contentManager = contentManager;
|
||||
_dictionary = new Dictionary<string, ContentItem>();
|
||||
}
|
||||
|
||||
// gets a content item for an identity string.
|
||||
public ContentItem Get(string id) {
|
||||
if (_dictionary.ContainsKey(id))
|
||||
return _dictionary[id];
|
||||
|
||||
foreach (var item in _contentManager.Query(VersionOptions.Published).List()) {
|
||||
var identity = _contentManager.GetItemMetadata(item).Identity.ToString();
|
||||
if (identity == id) {
|
||||
_dictionary.Add(identity, item);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Store(string id, ContentItem item) {
|
||||
_dictionary.Add(id, item);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class CallSiteCollection : ConcurrentDictionary<string, CallSite<Func<CallSite, object, object>>> {
|
||||
readonly Func<string, CallSite<Func<CallSite, object, object>>> _valueFactory;
|
||||
|
||||
|
@@ -22,7 +22,7 @@ namespace Orchard.ContentManagement {
|
||||
void Index(ContentItem contentItem, IDocumentIndex documentIndex);
|
||||
|
||||
XElement Export(ContentItem contentItem);
|
||||
void Import(XElement element);
|
||||
void Import(XElement element, ImportContentSession importContentSession);
|
||||
|
||||
void Flush();
|
||||
IContentQuery<ContentItem> Query();
|
||||
|
Reference in New Issue
Block a user