mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Implementing Import/Export.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using Orchard.AuditTrail.Models;
|
||||
using Orchard.Data;
|
||||
using Orchard.Events;
|
||||
|
||||
namespace Orchard.AuditTrail.ImportExport {
|
||||
public interface IExportEventHandler : IEventHandler {
|
||||
void Exporting(dynamic context);
|
||||
void Exported(dynamic context);
|
||||
}
|
||||
|
||||
public class AuditTrailExportEventHandler : IExportEventHandler {
|
||||
private readonly IRepository<AuditTrailEventRecord> _auditTrailEventRepository;
|
||||
|
||||
public AuditTrailExportEventHandler(IRepository<AuditTrailEventRecord> auditTrailEventRepository) {
|
||||
_auditTrailEventRepository = auditTrailEventRepository;
|
||||
}
|
||||
|
||||
public void Exporting(dynamic context) {
|
||||
}
|
||||
|
||||
public void Exported(dynamic context) {
|
||||
|
||||
if (!((IEnumerable<string>)context.ExportOptions.CustomSteps).Contains("AuditTrail")) {
|
||||
return;
|
||||
}
|
||||
|
||||
var records = _auditTrailEventRepository.Table.ToList();
|
||||
|
||||
if (!records.Any()) {
|
||||
return;
|
||||
}
|
||||
|
||||
var root = new XElement("AuditTrail");
|
||||
context.Document.Element("Orchard").Add(root);
|
||||
|
||||
foreach (var record in records) {
|
||||
root.Add(new XElement("Event",
|
||||
new XAttribute("Name", record.Event),
|
||||
new XAttribute("Category", record.Category),
|
||||
new XAttribute("User", record.UserName),
|
||||
new XAttribute("CreatedUtc", record.CreatedUtc),
|
||||
new XAttribute("EventFilterKey", record.EventFilterKey),
|
||||
new XAttribute("EventFilterData", record.EventFilterData),
|
||||
CreateElement("Comment", record.Comment),
|
||||
CreateCDataElement("EventData", record.EventData)));
|
||||
}
|
||||
}
|
||||
|
||||
private static XElement CreateElement(string name, string value) {
|
||||
return !String.IsNullOrWhiteSpace(value) ? new XElement(name, value) : null;
|
||||
}
|
||||
|
||||
private static XElement CreateCDataElement(string name, string value) {
|
||||
return !String.IsNullOrWhiteSpace(value) ? new XElement(name, new XCData(value)) : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Events;
|
||||
|
||||
namespace Orchard.AuditTrail.ImportExport {
|
||||
public interface ICustomExportStep : IEventHandler {
|
||||
void Register(IList<string> steps);
|
||||
}
|
||||
|
||||
public class AuditTrailExportStep : ICustomExportStep {
|
||||
public void Register(IList<string> steps) {
|
||||
steps.Add("AuditTrail");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using Orchard.AuditTrail.Models;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Data;
|
||||
using Orchard.Recipes.Models;
|
||||
using Orchard.Recipes.Services;
|
||||
|
||||
namespace Orchard.AuditTrail.ImportExport {
|
||||
public class AuditTrailImportHandler : Component, IRecipeHandler {
|
||||
private readonly IRepository<AuditTrailEventRecord> _auditTrailEventRepository;
|
||||
|
||||
public AuditTrailImportHandler(IRepository<AuditTrailEventRecord> auditTrailEventRepository) {
|
||||
_auditTrailEventRepository = auditTrailEventRepository;
|
||||
}
|
||||
|
||||
public void ExecuteRecipeStep(RecipeContext recipeContext) {
|
||||
if (!String.Equals(recipeContext.RecipeStep.Name, "AuditTrail", StringComparison.OrdinalIgnoreCase)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var eventElement in recipeContext.RecipeStep.Step.Elements()) {
|
||||
var record = new AuditTrailEventRecord {
|
||||
Event = eventElement.Attr<string>("Name"),
|
||||
Category = eventElement.Attr<string>("Category"),
|
||||
UserName = eventElement.Attr<string>("User"),
|
||||
CreatedUtc = eventElement.Attr<DateTime>("CreatedUtc"),
|
||||
EventFilterKey = eventElement.Attr<string>("EventFilterKey"),
|
||||
EventFilterData = eventElement.Attr<string>("EventFilterData"),
|
||||
Comment = eventElement.El("Comment"),
|
||||
EventData = eventElement.El("EventData"),
|
||||
};
|
||||
|
||||
_auditTrailEventRepository.Create(record);
|
||||
}
|
||||
|
||||
recipeContext.Executed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,6 +136,9 @@
|
||||
<Compile Include="AdminMenu.cs" />
|
||||
<Compile Include="Controllers\AdminController.cs" />
|
||||
<Compile Include="Drivers\AuditTrailSiteSettingsPartDriver.cs" />
|
||||
<Compile Include="ImportExport\AuditTrailExportStep.cs" />
|
||||
<Compile Include="ImportExport\AuditTrailExportHandler.cs" />
|
||||
<Compile Include="ImportExport\AuditTrailImportHandler.cs" />
|
||||
<Compile Include="Models\AuditTrailEventRecordResult.cs" />
|
||||
<Compile Include="Providers\Content\ContentAuditTrailEventShapes.cs" />
|
||||
<Compile Include="ViewModels\AuditTrailCategorySettingsViewModel.cs" />
|
||||
|
||||
Reference in New Issue
Block a user