From 639cfad7ec58e4b2780ff317866e2a409b409ccf Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Thu, 8 Aug 2013 16:41:52 -0700 Subject: [PATCH] Modifying the behavior of the Content Created workflow event Before this change, the content item data would not be available when ContentCreated is called, as the Update drivers haven't been called yet. This change will only trigger the event when the item is first created and its data has been updated. It also introduces a new ContentUpdated event. --- .../Activities/ContentActivity.cs | 10 ++++ .../Handlers/ContentHandler.cs | 37 ------------- .../Handlers/WorkflowContentHandler.cs | 53 +++++++++++++++++++ .../Orchard.Workflows.csproj | 2 +- 4 files changed, 64 insertions(+), 38 deletions(-) delete mode 100644 src/Orchard.Web/Modules/Orchard.Workflows/Handlers/ContentHandler.cs create mode 100644 src/Orchard.Web/Modules/Orchard.Workflows/Handlers/WorkflowContentHandler.cs diff --git a/src/Orchard.Web/Modules/Orchard.Workflows/Activities/ContentActivity.cs b/src/Orchard.Web/Modules/Orchard.Workflows/Activities/ContentActivity.cs index 711d51d7b..537b035e5 100644 --- a/src/Orchard.Web/Modules/Orchard.Workflows/Activities/ContentActivity.cs +++ b/src/Orchard.Web/Modules/Orchard.Workflows/Activities/ContentActivity.cs @@ -68,6 +68,16 @@ namespace Orchard.Workflows.Activities { } } + public class ContentUpdatedActivity : ContentActivity { + public override string Name { + get { return "ContentUpdated"; } + } + + public override LocalizedString Description { + get { return T("Content is updated."); } + } + } + public class ContentPublishedActivity : ContentActivity { public override string Name { get { return "ContentPublished"; } diff --git a/src/Orchard.Web/Modules/Orchard.Workflows/Handlers/ContentHandler.cs b/src/Orchard.Web/Modules/Orchard.Workflows/Handlers/ContentHandler.cs deleted file mode 100644 index a319de4e5..000000000 --- a/src/Orchard.Web/Modules/Orchard.Workflows/Handlers/ContentHandler.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System.Collections.Generic; -using Orchard.ContentManagement; -using Orchard.ContentManagement.Handlers; -using Orchard.Workflows.Services; - -namespace Orchard.Workflows.Handlers { - - public class RulePartHandler : ContentHandler { - public RulePartHandler(IWorkflowManager workflowManager) { - - OnPublished( - (context, part) => - workflowManager.TriggerEvent("ContentPublished", - context.ContentItem, - () => new Dictionary { { "Content", context.ContentItem } })); - - OnRemoving( - (context, part) => - workflowManager.TriggerEvent("ContentRemoved", - context.ContentItem, - () => new Dictionary { { "Content", context.ContentItem } })); - - OnVersioned( - (context, part1, part2) => - workflowManager.TriggerEvent("ContentVersioned", - context.BuildingContentItem, - () => new Dictionary { { "Content", context.BuildingContentItem } })); - - OnCreated( - (context, part) => - workflowManager.TriggerEvent("ContentCreated", - context.ContentItem, - () => new Dictionary { { "Content", context.ContentItem } })); - - } - } -} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Workflows/Handlers/WorkflowContentHandler.cs b/src/Orchard.Web/Modules/Orchard.Workflows/Handlers/WorkflowContentHandler.cs new file mode 100644 index 000000000..08fddd949 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Workflows/Handlers/WorkflowContentHandler.cs @@ -0,0 +1,53 @@ +using System.Collections.Generic; +using Orchard.ContentManagement; +using Orchard.ContentManagement.Handlers; +using Orchard.Workflows.Services; + +namespace Orchard.Workflows.Handlers { + + public class WorkflowContentHandler : ContentHandler { + private readonly HashSet _contentCreated = new HashSet(); + + public WorkflowContentHandler(IWorkflowManager workflowManager) { + + OnPublished( + (context, part) => + workflowManager.TriggerEvent("ContentPublished", + context.ContentItem, + () => new Dictionary { { "Content", context.ContentItem } })); + + OnRemoving( + (context, part) => + workflowManager.TriggerEvent("ContentRemoved", + context.ContentItem, + () => new Dictionary { { "Content", context.ContentItem } })); + + OnVersioned( + (context, part1, part2) => + workflowManager.TriggerEvent("ContentVersioned", + context.BuildingContentItem, + () => new Dictionary { { "Content", context.BuildingContentItem } })); + + OnUpdated( + (context, part) => { + workflowManager.TriggerEvent("ContentUpdated", + context.ContentItem, + () => new Dictionary { { "Content", context.ContentItem } }); + + // Trigger the ContentCreated event only when its values have been updated + if(_contentCreated.Contains(context.ContentItem.Id)) { + workflowManager.TriggerEvent("ContentCreated", + context.ContentItem, + () => new Dictionary {{"Content", context.ContentItem}}); + } + }); + + OnCreated( + // Flag the content item as "just created" but actually trigger the event + // when its content has been updated as it is what users would expect. + (context, part) => _contentCreated.Add(context.ContentItem.Id) + ); + + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Workflows/Orchard.Workflows.csproj b/src/Orchard.Web/Modules/Orchard.Workflows/Orchard.Workflows.csproj index 3ff019c24..3ed8c1e2c 100644 --- a/src/Orchard.Web/Modules/Orchard.Workflows/Orchard.Workflows.csproj +++ b/src/Orchard.Web/Modules/Orchard.Workflows/Orchard.Workflows.csproj @@ -162,7 +162,7 @@ - +