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.
This commit is contained in:
Morten Christensen
2012-12-28 14:47:09 -01:00
parent 194d241b02
commit 5dbcfa57a8
9 changed files with 133 additions and 225 deletions

View File

@@ -5,41 +5,45 @@ namespace Umbraco.Core.Events
public class PublishEventArgs<TEntity> : CancellableObjectEventArgs<IEnumerable<TEntity>>
{
/// <summary>
/// Constructor accepting multiple entities that are used in the publish operation
/// </summary>
/// <param name="eventObject"></param>
/// <param name="canCancel"></param>
public PublishEventArgs(IEnumerable<TEntity> eventObject, bool canCancel)
/// Constructor accepting multiple entities that are used in the publish operation
/// </summary>
/// <param name="eventObject"></param>
/// <param name="canCancel"></param>
/// <param name="isAllPublished"></param>
public PublishEventArgs(IEnumerable<TEntity> eventObject, bool canCancel, bool isAllPublished)
: base(eventObject, canCancel)
{
IsAllRepublished = isAllPublished;
}
/// <summary>
/// Constructor accepting multiple entities that are used in the publish operation
/// </summary>
/// <param name="eventObject"></param>
public PublishEventArgs(IEnumerable<TEntity> eventObject)
/// <summary>
/// Constructor accepting multiple entities that are used in the publish operation
/// </summary>
/// <param name="eventObject"></param>
public PublishEventArgs(IEnumerable<TEntity> eventObject)
: base(eventObject)
{
}
/// <summary>
/// Constructor accepting a single entity instance
/// </summary>
/// <param name="eventObject"></param>
public PublishEventArgs(TEntity eventObject)
/// <summary>
/// Constructor accepting a single entity instance
/// </summary>
/// <param name="eventObject"></param>
public PublishEventArgs(TEntity eventObject)
: base(new List<TEntity> { eventObject })
{
}
/// <summary>
/// Constructor accepting a single entity instance
/// </summary>
/// <param name="eventObject"></param>
/// <param name="canCancel"></param>
public PublishEventArgs(TEntity eventObject, bool canCancel)
/// <summary>
/// Constructor accepting a single entity instance
/// </summary>
/// <param name="eventObject"></param>
/// <param name="canCancel"></param>
/// <param name="isAllPublished"></param>
public PublishEventArgs(TEntity eventObject, bool canCancel, bool isAllPublished)
: base(new List<TEntity> { eventObject }, canCancel)
{
IsAllRepublished = isAllPublished;
}
/// <summary>
@@ -49,5 +53,7 @@ namespace Umbraco.Core.Events
{
get { return EventObject; }
}
public bool IsAllRepublished { get; private set; }
}
}

View File

@@ -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; }
}
}

View File

@@ -31,7 +31,6 @@ namespace Umbraco.Core.Models
Mandate.ParameterNotNull(contentType, "contentType");
Mandate.ParameterNotNull(properties, "properties");
//_parentId = parentId;
_parentId = new Lazy<int>(() => parentId);
_contentTypeId = int.Parse(contentType.Id.ToString(CultureInfo.InvariantCulture));
@@ -74,7 +73,6 @@ namespace Umbraco.Core.Models
/// <summary>
/// Gets or sets the Id of the Parent entity
/// </summary>
/// <remarks>Might not be necessary if handled as a relation?</remarks>
[DataMember]
public virtual int ParentId
{

View File

@@ -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<IContent> content, int userId);
/// <summary>
/// Occurs before publish
/// </summary>
public static event EventHandler<PublishingEventArgs> Publishing;
/// <summary>
/// Raises the <see cref="Publishing"/> event
/// </summary>
/// <param name="content"> </param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected virtual void OnPublish(IContent content, PublishingEventArgs e)
{
if (Publishing != null)
Publishing(content, e);
}
/// <summary>
/// Occurs after publish
/// </summary>
public static event EventHandler<PublishingEventArgs> Published;
/// <summary>
/// Raises the <see cref="Published"/> event
/// </summary>
/// <param name="content"> </param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected virtual void OnPublished(IContent content, PublishingEventArgs e)
{
if (Published != null)
Published(content, e);
}
/// <summary>
/// Raises the <see cref="Published"/> event
/// </summary>
/// <param name="content"> </param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected virtual void OnPublished(IEnumerable<IContent> content, PublishingEventArgs e)
{
if (Published != null)
Published(content, e);
}
/// <summary>
/// Occurs before unpublish
/// </summary>
public static event EventHandler<PublishingEventArgs> UnPublishing;
/// <summary>
/// Raises the <see cref="UnPublishing"/> event
/// </summary>
/// <param name="content"> </param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected virtual void OnUnPublish(IContent content, PublishingEventArgs e)
{
if (UnPublishing != null)
UnPublishing(content, e);
}
/// <summary>
/// Occurs after unpublish
/// </summary>
public static event EventHandler<PublishingEventArgs> UnPublished;
/// <summary>
/// Raises the <see cref="UnPublished"/> event
/// </summary>
/// <param name="content"> </param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected virtual void OnUnPublished(IContent content, PublishingEventArgs e)
{
if (UnPublished != null)
UnPublished(content, e);
}
/// <summary>
/// Raises the <see cref="UnPublished"/> event
/// </summary>
/// <param name="content"> </param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected virtual void OnUnPublished(IEnumerable<IContent> content, PublishingEventArgs e)
{
if (UnPublished != null)
UnPublished(content, e);
}
/// <summary>
/// Call to fire event that updating the published content has finalized.
/// </summary>

View File

@@ -10,9 +10,9 @@ namespace Umbraco.Core.Publishing
/// <summary>
/// Currently acts as an interconnection between the new public api and the legacy api for publishing
/// </summary>
internal class PublishingStrategy : BasePublishingStrategy
public class PublishingStrategy : BasePublishingStrategy
{
internal PublishingStrategy()
public PublishingStrategy()
{
}
@@ -24,49 +24,43 @@ namespace Umbraco.Core.Publishing
/// <returns>True if the publish operation was successfull and not cancelled, otherwise false</returns>
public override bool Publish(IContent content, int userId)
{
var e = new PublishingEventArgs();
//Fire Publishing event
OnPublish(content, e);
if (Publishing.IsRaisedEventCancelled(new PublishEventArgs<IContent>(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<PublishingStrategy>(
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<PublishingStrategy>(
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<PublishingStrategy>(
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<PublishingStrategy>(
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<PublishingStrategy>(
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<PublishingStrategy>(
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<PublishingStrategy>(
string.Format("Content '{0}' with Id '{1}' has been published.",
content.Name, content.Id));
return true;
}
/// <summary>
@@ -77,17 +71,13 @@ namespace Umbraco.Core.Publishing
/// <returns>True if the publish operation was successfull and not cancelled, otherwise false</returns>
public override bool PublishWithChildren(IEnumerable<IContent> 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<IContent>(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
/// <returns>True if the unpublish operation was successfull and not cancelled, otherwise false</returns>
public override bool UnPublish(IContent content, int userId)
{
var e = new PublishingEventArgs();
//Fire BeforeUnPublish event
OnUnPublish(content, e);
if (UnPublishing.IsRaisedEventCancelled(new UnPublishEventArgs<IContent>(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<PublishingStrategy>(
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<PublishingStrategy>(
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<PublishingStrategy>(
string.Format("Content '{0}' with Id '{1}' has been unpublished.",
content.Name, content.Id));
return true;
}
/// <summary>
@@ -171,14 +155,11 @@ namespace Umbraco.Core.Publishing
/// <returns>True if the unpublish operation was successfull and not cancelled, otherwise false</returns>
public override bool UnPublish(IEnumerable<IContent> 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<IContent>(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
/// <param name="content"><see cref="IContent"/> thats being published</param>
public override void PublishingFinalized(IContent content)
{
OnPublished(content, new PublishingEventArgs());
Published.RaiseEvent(new PublishEventArgs<IContent>(content, false, false), this);
}
/// <summary>
@@ -222,7 +203,8 @@ namespace Umbraco.Core.Publishing
/// <param name="isAllRepublished">Boolean indicating whether its all content that is republished</param>
public override void PublishingFinalized(IEnumerable<IContent> content, bool isAllRepublished)
{
OnPublished(content, new PublishingEventArgs(isAllRepublished));
Published.RaiseEvent(new PublishEventArgs<IContent>(content, false, isAllRepublished), this);
}
/// <summary>
@@ -231,7 +213,7 @@ namespace Umbraco.Core.Publishing
/// <param name="content"><see cref="IContent"/> thats being unpublished</param>
public override void UnPublishingFinalized(IContent content)
{
OnUnPublished(content, new PublishingEventArgs());
UnPublished.RaiseEvent(new UnPublishEventArgs<IContent>(content, false), this);
}
/// <summary>
@@ -240,7 +222,29 @@ namespace Umbraco.Core.Publishing
/// <param name="content">An enumerable list of <see cref="IContent"/> thats being unpublished</param>
public override void UnPublishingFinalized(IEnumerable<IContent> content)
{
OnUnPublished(content, new PublishingEventArgs());
UnPublished.RaiseEvent(new UnPublishEventArgs<IContent>(content, false), this);
}
/// <summary>
/// Occurs before publish
/// </summary>
public static event TypedEventHandler<IPublishingStrategy, PublishEventArgs<IContent>> Publishing;
/// <summary>
/// Occurs after publish
/// </summary>
public static event TypedEventHandler<IPublishingStrategy, PublishEventArgs<IContent>> Published;
/// <summary>
/// Occurs before unpublish
/// </summary>
public static event TypedEventHandler<IPublishingStrategy, UnPublishEventArgs<IContent>> UnPublishing;
/// <summary>
/// Occurs after unpublish
/// </summary>
public static event TypedEventHandler<IPublishingStrategy, UnPublishEventArgs<IContent>> UnPublished;
}
}

View File

@@ -1268,12 +1268,12 @@ namespace Umbraco.Core.Services
public static event TypedEventHandler<IContentService, DeleteEventArgs<IContent>> Deleted;
/// <summary>
/// Occurs before Delete
/// Occurs before Delete Versions
/// </summary>
public static event TypedEventHandler<IContentService, DeleteRevisionsEventArgs> DeletingVersions;
/// <summary>
/// Occurs after Delete
/// Occurs after Delete Versions
/// </summary>
public static event TypedEventHandler<IContentService, DeleteRevisionsEventArgs> DeletedVersions;

View File

@@ -127,7 +127,6 @@
<Compile Include="Events\EventExtensions.cs" />
<Compile Include="Events\PublishEventArgs.cs" />
<Compile Include="Events\TypedEventHandler.cs" />
<Compile Include="Events\PublishingEventArgs.cs" />
<Compile Include="Events\MoveEventArgs.cs" />
<Compile Include="Events\NewEventArgs.cs" />
<Compile Include="Events\RefreshContentEventArgs.cs" />

View File

@@ -19,6 +19,7 @@ namespace Umbraco.Web.Strategies
/// <remarks>
/// 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.
/// </remarks>
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<IContent> e)
{
if (sender is IContent)
{
var content = sender as IContent;
UpdateSingleContentCache(content);
}
else if (sender is IEnumerable<IContent>)
if (e.PublishedEntities.Any())
{
if (e.IsAllRepublished)
{
var content = sender as IEnumerable<IContent>;
UpdateMultipleContentCache(content);
UpdateEntireCache();
return;
}
if (e.PublishedEntities.Count() > 1)
{
UpdateMultipleContentCache(e.PublishedEntities);
}
else
{
UpdateEntireCache();
var content = e.PublishedEntities.FirstOrDefault();
UpdateSingleContentCache(content);
}
}
}

View File

@@ -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
/// <remarks>
/// 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.
/// </remarks>
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<IContent> e)
{
if (sender is IContent)
if (e.UnPublishedEntities.Any())
{
var content = sender as IContent;
UnPublishSingle(content);
}
else if (sender is IEnumerable<IContent>)
{
var content = sender as IEnumerable<IContent>;
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);
}
}
}