From 5dbcfa57a8f2d6f2e2c7b6a33d6feb3cab3edf1a Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Fri, 28 Dec 2012 14:47:09 -0100 Subject: [PATCH] Implements the new publish and unpublish events in the PublishingStrategy. Making the PublishStrategy public, so that its possible to subscribe to the events. Removing the old PublishingEventArgs, which is no longer used. Correcting a few comments. --- src/Umbraco.Core/Events/PublishEventArgs.cs | 48 +++--- .../Events/PublishingEventArgs.cs | 17 -- src/Umbraco.Core/Models/ContentBase.cs | 2 - .../Publishing/BasePublishingStrategy.cs | 88 ---------- .../Publishing/PublishingStrategy.cs | 152 +++++++++--------- src/Umbraco.Core/Services/ContentService.cs | 4 +- src/Umbraco.Core/Umbraco.Core.csproj | 1 - .../Strategies/UpdateCacheAfterPublish.cs | 22 +-- .../Strategies/UpdateCacheAfterUnPublish.cs | 24 +-- 9 files changed, 133 insertions(+), 225 deletions(-) delete mode 100644 src/Umbraco.Core/Events/PublishingEventArgs.cs diff --git a/src/Umbraco.Core/Events/PublishEventArgs.cs b/src/Umbraco.Core/Events/PublishEventArgs.cs index 98afede375..2355e250ef 100644 --- a/src/Umbraco.Core/Events/PublishEventArgs.cs +++ b/src/Umbraco.Core/Events/PublishEventArgs.cs @@ -5,41 +5,45 @@ namespace Umbraco.Core.Events public class PublishEventArgs : CancellableObjectEventArgs> { /// - /// Constructor accepting multiple entities that are used in the publish operation - /// - /// - /// - public PublishEventArgs(IEnumerable eventObject, bool canCancel) + /// Constructor accepting multiple entities that are used in the publish operation + /// + /// + /// + /// + public PublishEventArgs(IEnumerable eventObject, bool canCancel, bool isAllPublished) : base(eventObject, canCancel) { + IsAllRepublished = isAllPublished; } - /// - /// Constructor accepting multiple entities that are used in the publish operation - /// - /// - public PublishEventArgs(IEnumerable eventObject) + /// + /// Constructor accepting multiple entities that are used in the publish operation + /// + /// + public PublishEventArgs(IEnumerable eventObject) : base(eventObject) { } - /// - /// Constructor accepting a single entity instance - /// - /// - public PublishEventArgs(TEntity eventObject) + /// + /// Constructor accepting a single entity instance + /// + /// + public PublishEventArgs(TEntity eventObject) : base(new List { eventObject }) { } - /// - /// Constructor accepting a single entity instance - /// - /// - /// - public PublishEventArgs(TEntity eventObject, bool canCancel) + /// + /// Constructor accepting a single entity instance + /// + /// + /// + /// + public PublishEventArgs(TEntity eventObject, bool canCancel, bool isAllPublished) : base(new List { eventObject }, canCancel) { + IsAllRepublished = isAllPublished; } /// @@ -49,5 +53,7 @@ namespace Umbraco.Core.Events { get { return EventObject; } } + + public bool IsAllRepublished { get; private set; } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Events/PublishingEventArgs.cs b/src/Umbraco.Core/Events/PublishingEventArgs.cs deleted file mode 100644 index c58e3bac15..0000000000 --- a/src/Umbraco.Core/Events/PublishingEventArgs.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Umbraco.Core.Events -{ - public class PublishingEventArgs : System.ComponentModel.CancelEventArgs - { - public PublishingEventArgs() - { - IsAllRepublished = false; - } - - public PublishingEventArgs(bool isAllPublished) - { - IsAllRepublished = isAllPublished; - } - - public bool IsAllRepublished { get; private set; } - } -} \ No newline at end of file diff --git a/src/Umbraco.Core/Models/ContentBase.cs b/src/Umbraco.Core/Models/ContentBase.cs index fd4692cb33..84fd5f1495 100644 --- a/src/Umbraco.Core/Models/ContentBase.cs +++ b/src/Umbraco.Core/Models/ContentBase.cs @@ -31,7 +31,6 @@ namespace Umbraco.Core.Models Mandate.ParameterNotNull(contentType, "contentType"); Mandate.ParameterNotNull(properties, "properties"); - //_parentId = parentId; _parentId = new Lazy(() => parentId); _contentTypeId = int.Parse(contentType.Id.ToString(CultureInfo.InvariantCulture)); @@ -74,7 +73,6 @@ namespace Umbraco.Core.Models /// /// Gets or sets the Id of the Parent entity /// - /// Might not be necessary if handled as a relation? [DataMember] public virtual int ParentId { diff --git a/src/Umbraco.Core/Publishing/BasePublishingStrategy.cs b/src/Umbraco.Core/Publishing/BasePublishingStrategy.cs index 927a172996..d126140c04 100644 --- a/src/Umbraco.Core/Publishing/BasePublishingStrategy.cs +++ b/src/Umbraco.Core/Publishing/BasePublishingStrategy.cs @@ -1,6 +1,4 @@ -using System; using System.Collections.Generic; -using Umbraco.Core.Events; using Umbraco.Core.Models; namespace Umbraco.Core.Publishing @@ -15,92 +13,6 @@ namespace Umbraco.Core.Publishing public abstract bool UnPublish(IContent content, int userId); public abstract bool UnPublish(IEnumerable content, int userId); - /// - /// Occurs before publish - /// - public static event EventHandler Publishing; - - /// - /// Raises the event - /// - /// - /// The instance containing the event data. - protected virtual void OnPublish(IContent content, PublishingEventArgs e) - { - if (Publishing != null) - Publishing(content, e); - } - - /// - /// Occurs after publish - /// - public static event EventHandler Published; - - /// - /// Raises the event - /// - /// - /// The instance containing the event data. - protected virtual void OnPublished(IContent content, PublishingEventArgs e) - { - if (Published != null) - Published(content, e); - } - - /// - /// Raises the event - /// - /// - /// The instance containing the event data. - protected virtual void OnPublished(IEnumerable content, PublishingEventArgs e) - { - if (Published != null) - Published(content, e); - } - - /// - /// Occurs before unpublish - /// - public static event EventHandler UnPublishing; - - /// - /// Raises the event - /// - /// - /// The instance containing the event data. - protected virtual void OnUnPublish(IContent content, PublishingEventArgs e) - { - if (UnPublishing != null) - UnPublishing(content, e); - } - - /// - /// Occurs after unpublish - /// - public static event EventHandler UnPublished; - - /// - /// Raises the event - /// - /// - /// The instance containing the event data. - protected virtual void OnUnPublished(IContent content, PublishingEventArgs e) - { - if (UnPublished != null) - UnPublished(content, e); - } - - /// - /// Raises the event - /// - /// - /// The instance containing the event data. - protected virtual void OnUnPublished(IEnumerable content, PublishingEventArgs e) - { - if (UnPublished != null) - UnPublished(content, e); - } - /// /// Call to fire event that updating the published content has finalized. /// diff --git a/src/Umbraco.Core/Publishing/PublishingStrategy.cs b/src/Umbraco.Core/Publishing/PublishingStrategy.cs index 44add90e41..b570f59421 100644 --- a/src/Umbraco.Core/Publishing/PublishingStrategy.cs +++ b/src/Umbraco.Core/Publishing/PublishingStrategy.cs @@ -10,9 +10,9 @@ namespace Umbraco.Core.Publishing /// /// Currently acts as an interconnection between the new public api and the legacy api for publishing /// - internal class PublishingStrategy : BasePublishingStrategy + public class PublishingStrategy : BasePublishingStrategy { - internal PublishingStrategy() + public PublishingStrategy() { } @@ -24,49 +24,43 @@ namespace Umbraco.Core.Publishing /// True if the publish operation was successfull and not cancelled, otherwise false public override bool Publish(IContent content, int userId) { - var e = new PublishingEventArgs(); - //Fire Publishing event - OnPublish(content, e); + if (Publishing.IsRaisedEventCancelled(new PublishEventArgs(content), this)) + return false; - if (!e.Cancel) + //Check if the Content is Expired to verify that it can in fact be published + if (content.Status == ContentStatus.Expired) { - //Check if the Content is Expired to verify that it can in fact be published - if (content.Status == ContentStatus.Expired) - { - LogHelper.Info( - string.Format("Content '{0}' with Id '{1}' has expired and could not be published.", - content.Name, content.Id)); - return false; - } - - //Check if the Content is Awaiting Release to verify that it can in fact be published - if (content.Status == ContentStatus.AwaitingRelease) - { - LogHelper.Info( - string.Format("Content '{0}' with Id '{1}' is awaiting release and could not be published.", - content.Name, content.Id)); - return false; - } - - //Check if the Content is Trashed to verify that it can in fact be published - if (content.Status == ContentStatus.Trashed) - { - LogHelper.Info( - string.Format("Content '{0}' with Id '{1}' is trashed and could not be published.", - content.Name, content.Id)); - return false; - } - - content.ChangePublishedState(true); - LogHelper.Info( - string.Format("Content '{0}' with Id '{1}' has been published.", + string.Format("Content '{0}' with Id '{1}' has expired and could not be published.", content.Name, content.Id)); - - return true; + return false; } - return false; + //Check if the Content is Awaiting Release to verify that it can in fact be published + if (content.Status == ContentStatus.AwaitingRelease) + { + LogHelper.Info( + string.Format("Content '{0}' with Id '{1}' is awaiting release and could not be published.", + content.Name, content.Id)); + return false; + } + + //Check if the Content is Trashed to verify that it can in fact be published + if (content.Status == ContentStatus.Trashed) + { + LogHelper.Info( + string.Format("Content '{0}' with Id '{1}' is trashed and could not be published.", + content.Name, content.Id)); + return false; + } + + content.ChangePublishedState(true); + + LogHelper.Info( + string.Format("Content '{0}' with Id '{1}' has been published.", + content.Name, content.Id)); + + return true; } /// @@ -77,17 +71,13 @@ namespace Umbraco.Core.Publishing /// True if the publish operation was successfull and not cancelled, otherwise false public override bool PublishWithChildren(IEnumerable content, int userId) { - var e = new PublishingEventArgs(); - /* Only update content thats not already been published - we want to loop through * all unpublished content to write skipped content (expired and awaiting release) to log. */ foreach (var item in content.Where(x => x.Published == false)) { //Fire Publishing event - OnPublish(item, e); - if (e.Cancel) - return false; + if (Publishing.IsRaisedEventCancelled(new PublishEventArgs(item), this)) //Check if the Content is Expired to verify that it can in fact be published if (item.Status == ContentStatus.Expired) @@ -134,33 +124,27 @@ namespace Umbraco.Core.Publishing /// True if the unpublish operation was successfull and not cancelled, otherwise false public override bool UnPublish(IContent content, int userId) { - var e = new PublishingEventArgs(); - //Fire BeforeUnPublish event - OnUnPublish(content, e); + if (UnPublishing.IsRaisedEventCancelled(new UnPublishEventArgs(content), this)) + return false; - if (!e.Cancel) + //If Content has a release date set to before now, it should be removed so it doesn't interrupt an unpublish + //Otherwise it would remain released == published + if (content.ReleaseDate.HasValue && content.ReleaseDate.Value <= DateTime.Now) { - //If Content has a release date set to before now, it should be removed so it doesn't interrupt an unpublish - //Otherwise it would remain released == published - if (content.ReleaseDate.HasValue && content.ReleaseDate.Value <= DateTime.Now) - { - content.ReleaseDate = null; - - LogHelper.Info( - string.Format( - "Content '{0}' with Id '{1}' had its release date removed, because it was unpublished.", - content.Name, content.Id)); - } - - content.ChangePublishedState(false); + content.ReleaseDate = null; LogHelper.Info( - string.Format("Content '{0}' with Id '{1}' has been unpublished.", - content.Name, content.Id)); - return true; + string.Format( + "Content '{0}' with Id '{1}' had its release date removed, because it was unpublished.", + content.Name, content.Id)); } - return false; + content.ChangePublishedState(false); + + LogHelper.Info( + string.Format("Content '{0}' with Id '{1}' has been unpublished.", + content.Name, content.Id)); + return true; } /// @@ -171,14 +155,11 @@ namespace Umbraco.Core.Publishing /// True if the unpublish operation was successfull and not cancelled, otherwise false public override bool UnPublish(IEnumerable content, int userId) { - var e = new PublishingEventArgs(); - //Only update content thats already been published foreach (var item in content.Where(x => x.Published == true)) { - //Fire UnPublished event - OnUnPublish(item, e); - if (e.Cancel) + //Fire UnPublishing event + if (UnPublishing.IsRaisedEventCancelled(new UnPublishEventArgs(item), this)) return false; //If Content has a release date set to before now, it should be removed so it doesn't interrupt an unpublish @@ -212,7 +193,7 @@ namespace Umbraco.Core.Publishing /// thats being published public override void PublishingFinalized(IContent content) { - OnPublished(content, new PublishingEventArgs()); + Published.RaiseEvent(new PublishEventArgs(content, false, false), this); } /// @@ -222,7 +203,8 @@ namespace Umbraco.Core.Publishing /// Boolean indicating whether its all content that is republished public override void PublishingFinalized(IEnumerable content, bool isAllRepublished) { - OnPublished(content, new PublishingEventArgs(isAllRepublished)); + Published.RaiseEvent(new PublishEventArgs(content, false, isAllRepublished), this); + } /// @@ -231,7 +213,7 @@ namespace Umbraco.Core.Publishing /// thats being unpublished public override void UnPublishingFinalized(IContent content) { - OnUnPublished(content, new PublishingEventArgs()); + UnPublished.RaiseEvent(new UnPublishEventArgs(content, false), this); } /// @@ -240,7 +222,29 @@ namespace Umbraco.Core.Publishing /// An enumerable list of thats being unpublished public override void UnPublishingFinalized(IEnumerable content) { - OnUnPublished(content, new PublishingEventArgs()); + UnPublished.RaiseEvent(new UnPublishEventArgs(content, false), this); } + + /// + /// Occurs before publish + /// + public static event TypedEventHandler> Publishing; + + /// + /// Occurs after publish + /// + public static event TypedEventHandler> Published; + + /// + /// Occurs before unpublish + /// + public static event TypedEventHandler> UnPublishing; + + /// + /// Occurs after unpublish + /// + public static event TypedEventHandler> UnPublished; + + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index 0600b0af54..958e56d40b 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -1268,12 +1268,12 @@ namespace Umbraco.Core.Services public static event TypedEventHandler> Deleted; /// - /// Occurs before Delete + /// Occurs before Delete Versions /// public static event TypedEventHandler DeletingVersions; /// - /// Occurs after Delete + /// Occurs after Delete Versions /// public static event TypedEventHandler DeletedVersions; diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index c09fb93d35..3b2eebb79c 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -127,7 +127,6 @@ - diff --git a/src/Umbraco.Web/Strategies/UpdateCacheAfterPublish.cs b/src/Umbraco.Web/Strategies/UpdateCacheAfterPublish.cs index c7c91f87eb..faf0795837 100644 --- a/src/Umbraco.Web/Strategies/UpdateCacheAfterPublish.cs +++ b/src/Umbraco.Web/Strategies/UpdateCacheAfterPublish.cs @@ -19,6 +19,7 @@ namespace Umbraco.Web.Strategies /// /// This implementation is meant as a seperation of the cache refresh from the ContentService /// and PublishingStrategy. + /// This event subscriber will only be relevant as long as there is an xml cache. /// public class UpdateCacheAfterPublish : IApplicationStartupHandler { @@ -27,23 +28,24 @@ namespace Umbraco.Web.Strategies PublishingStrategy.Published += PublishingStrategy_Published; } - void PublishingStrategy_Published(object sender, PublishingEventArgs e) + void PublishingStrategy_Published(IPublishingStrategy sender, PublishEventArgs e) { - if (sender is IContent) - { - var content = sender as IContent; - UpdateSingleContentCache(content); - } - else if (sender is IEnumerable) + if (e.PublishedEntities.Any()) { if (e.IsAllRepublished) { - var content = sender as IEnumerable; - UpdateMultipleContentCache(content); + UpdateEntireCache(); + return; + } + + if (e.PublishedEntities.Count() > 1) + { + UpdateMultipleContentCache(e.PublishedEntities); } else { - UpdateEntireCache(); + var content = e.PublishedEntities.FirstOrDefault(); + UpdateSingleContentCache(content); } } } diff --git a/src/Umbraco.Web/Strategies/UpdateCacheAfterUnPublish.cs b/src/Umbraco.Web/Strategies/UpdateCacheAfterUnPublish.cs index be9bd22839..801b9bdf93 100644 --- a/src/Umbraco.Web/Strategies/UpdateCacheAfterUnPublish.cs +++ b/src/Umbraco.Web/Strategies/UpdateCacheAfterUnPublish.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using Umbraco.Core.Events; using Umbraco.Core.Models; using Umbraco.Core.Publishing; @@ -17,6 +18,7 @@ namespace Umbraco.Web.Strategies /// /// This implementation is meant as a seperation of the cache refresh from the ContentService /// and PublishingStrategy. + /// This event subscriber will only be relevant as long as there is an xml cache. /// public class UpdateCacheAfterUnPublish : IApplicationStartupHandler { @@ -25,19 +27,21 @@ namespace Umbraco.Web.Strategies PublishingStrategy.UnPublished += PublishingStrategy_UnPublished; } - void PublishingStrategy_UnPublished(object sender, PublishingEventArgs e) + void PublishingStrategy_UnPublished(IPublishingStrategy sender, UnPublishEventArgs e) { - if (sender is IContent) + if (e.UnPublishedEntities.Any()) { - var content = sender as IContent; - UnPublishSingle(content); - } - else if (sender is IEnumerable) - { - var content = sender as IEnumerable; - foreach (var c in content) + if (e.UnPublishedEntities.Count() > 1) { - UnPublishSingle(c); + foreach (var c in e.UnPublishedEntities) + { + UnPublishSingle(c); + } + } + else + { + var content = e.UnPublishedEntities.FirstOrDefault(); + UnPublishSingle(content); } } }