Fixes MediaService to have the correct RecycleBin id = -21 (not -20 like Content). Converted

MoveEventArgs and all services that use it to use the new event structure.
This commit is contained in:
Shannon Deminick
2012-12-16 06:37:27 +05:00
parent dad5326281
commit af5a01caed
3 changed files with 85 additions and 94 deletions

View File

@@ -1,10 +1,20 @@
namespace Umbraco.Core.Events
{
public class MoveEventArgs : System.ComponentModel.CancelEventArgs
public class MoveEventArgs<TEntity> : CancellableObjectEventArgs<TEntity>
{
public MoveEventArgs(TEntity entity, bool canCancel, int parentId) : base(entity, canCancel)
{
ParentId = parentId;
}
public MoveEventArgs(TEntity entity, int parentId) : base(entity)
{
ParentId = parentId;
}
/// <summary>
/// Gets or Sets the Id of the objects new parent.
/// </summary>
public int ParentId { get; set; }
public int ParentId { get; private set; }
}
}

View File

@@ -914,38 +914,33 @@ namespace Umbraco.Core.Services
/// <param name="userId">Optional Id of the User deleting the Content</param>
public void MoveToRecycleBin(IContent content, int userId = -1)
{
if (Trashing.IsRaisedEventCancelled(new MoveEventArgs<IContent>(content, -20), this))
return;
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateContentRepository(uow))
{
var e = new MoveEventArgs { ParentId = -20 };
if (Trashing != null)
Trashing(content, e);
if (!e.Cancel)
//Make sure that published content is unpublished before being moved to the Recycle Bin
if (HasPublishedVersion(content.Id))
{
//Make sure that published content is unpublished before being moved to the Recycle Bin
if (HasPublishedVersion(content.Id))
{
UnPublish(content, userId);
}
//Move children to Recycle Bin before the 'possible parent' is moved there
var children = GetChildren(content.Id);
foreach (var child in children)
{
MoveToRecycleBin(child, userId);
}
SetWriter(content, userId);
content.ChangeTrashedState(true);
repository.AddOrUpdate(content);
uow.Commit();
if (Trashed != null)
Trashed(content, e);
Audit.Add(AuditTypes.Move, "Move Content to Recycle Bin performed by user", userId == -1 ? 0 : userId, content.Id);
UnPublish(content, userId);
}
//Move children to Recycle Bin before the 'possible parent' is moved there
var children = GetChildren(content.Id);
foreach (var child in children)
{
MoveToRecycleBin(child, userId);
}
SetWriter(content, userId);
content.ChangeTrashedState(true);
repository.AddOrUpdate(content);
uow.Commit();
Trashed.RaiseEvent(new MoveEventArgs<IContent>(content, false, -20), this);
Audit.Add(AuditTypes.Move, "Move Content to Recycle Bin performed by user", userId == -1 ? 0 : userId, content.Id);
}
}
@@ -964,39 +959,35 @@ namespace Umbraco.Core.Services
{
//TODO Verify that SortOrder + Path is updated correctly
//TODO Add a check to see if parentId = -20 because then we should change the TrashState
var e = new MoveEventArgs { ParentId = parentId };
if (Moving != null)
Moving(content, e);
if (Moving.IsRaisedEventCancelled(new MoveEventArgs<IContent>(content, parentId), this))
return;
SetWriter(content, userId);
if (!e.Cancel)
//If Content is being moved away from Recycle Bin, its state should be un-trashed
if (content.Trashed && parentId != -20)
{
SetWriter(content, userId);
//If Content is being moved away from Recycle Bin, its state should be un-trashed
if (content.Trashed && parentId != -20)
{
content.ChangeTrashedState(false, parentId);
}
else
{
content.ParentId = parentId;
}
//If Content is published, it should be (re)published from its new location
if (content.Published)
{
SaveAndPublish(content, userId);
}
else
{
Save(content, userId);
}
if (Moved != null)
Moved(content, e);
Audit.Add(AuditTypes.Move, "Move Content performed by user", userId == -1 ? 0 : userId, content.Id);
content.ChangeTrashedState(false, parentId);
}
else
{
content.ParentId = parentId;
}
//If Content is published, it should be (re)published from its new location
if (content.Published)
{
SaveAndPublish(content, userId);
}
else
{
Save(content, userId);
}
Moved.RaiseEvent(new MoveEventArgs<IContent>(content, false, parentId), this);
Audit.Add(AuditTypes.Move, "Move Content performed by user", userId == -1 ? 0 : userId, content.Id);
}
/// <summary>
@@ -1324,22 +1315,22 @@ namespace Umbraco.Core.Services
/// <summary>
/// Occurs before Content is moved to Recycle Bin
/// </summary>
public static event EventHandler<MoveEventArgs> Trashing;
public static event TypedEventHandler<IContentService, MoveEventArgs<IContent>> Trashing;
/// <summary>
/// Occurs after Content is moved to Recycle Bin
/// </summary>
public static event EventHandler<MoveEventArgs> Trashed;
public static event TypedEventHandler<IContentService, MoveEventArgs<IContent>> Trashed;
/// <summary>
/// Occurs before Move
/// </summary>
public static event EventHandler<MoveEventArgs> Moving;
public static event TypedEventHandler<IContentService, MoveEventArgs<IContent>> Moving;
/// <summary>
/// Occurs after Move
/// </summary>
public static event EventHandler<MoveEventArgs> Moved;
public static event TypedEventHandler<IContentService, MoveEventArgs<IContent>> Moved;
/// <summary>
/// Occurs before Rollback

View File

@@ -172,7 +172,7 @@ namespace Umbraco.Core.Services
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
{
var query = Query<IMedia>.Builder.Where(x => x.ParentId == -20);
var query = Query<IMedia>.Builder.Where(x => x.ParentId == -21);
var medias = repository.GetByQuery(query);
return medias;
@@ -187,20 +187,15 @@ namespace Umbraco.Core.Services
/// <param name="userId">Id of the User moving the Media</param>
public void Move(IMedia media, int parentId, int userId = -1)
{
var e = new MoveEventArgs { ParentId = parentId };
if (Moving != null)
Moving(media, e);
if (Moving.IsRaisedEventCancelled(new MoveEventArgs<IMedia>(media, parentId), this))
return;
if (!e.Cancel)
{
media.ParentId = parentId;
Save(media, userId);
media.ParentId = parentId;
Save(media, userId);
if (Moved != null)
Moved(media, e);
Moved.RaiseEvent(new MoveEventArgs<IMedia>(media, false, parentId), this);
Audit.Add(AuditTypes.Move, "Move Media performed by user", userId == -1 ? 0 : userId, media.Id);
}
Audit.Add(AuditTypes.Move, "Move Media performed by user", userId == -1 ? 0 : userId, media.Id);
}
/// <summary>
@@ -212,24 +207,19 @@ namespace Umbraco.Core.Services
{
//TODO If media item has children those should also be moved to the recycle bin as well
if (Trashing.IsRaisedEventCancelled(new MoveEventArgs<IMedia>(media, -21), this))
return;
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
{
var e = new MoveEventArgs { ParentId = -20 };
if (Trashing != null)
Trashing(media, e);
((Core.Models.Media)media).ChangeTrashedState(true);
repository.AddOrUpdate(media);
uow.Commit();
if (!e.Cancel)
{
((Core.Models.Media)media).ChangeTrashedState(true);
repository.AddOrUpdate(media);
uow.Commit();
Trashed.RaiseEvent(new MoveEventArgs<IMedia>(media, false, -21), this);
if (Trashed != null)
Trashed(media, e);
Audit.Add(AuditTypes.Move, "Move Media to Recycle Bin performed by user", userId == -1 ? 0 : userId, media.Id);
}
Audit.Add(AuditTypes.Move, "Move Media to Recycle Bin performed by user", userId == -1 ? 0 : userId, media.Id);
}
}
@@ -241,7 +231,7 @@ namespace Umbraco.Core.Services
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
{
var query = Query<IMedia>.Builder.Where(x => x.ParentId == -20);
var query = Query<IMedia>.Builder.Where(x => x.ParentId == -21);
var contents = repository.GetByQuery(query);
foreach (var content in contents)
@@ -250,7 +240,7 @@ namespace Umbraco.Core.Services
}
uow.Commit();
Audit.Add(AuditTypes.Delete, "Empty Recycle Bin performed by user", 0, -20);
Audit.Add(AuditTypes.Delete, "Empty Recycle Bin performed by user", 0, -21);
}
}
@@ -460,22 +450,22 @@ namespace Umbraco.Core.Services
/// <summary>
/// Occurs before Content is moved to Recycle Bin
/// </summary>
public static event EventHandler<MoveEventArgs> Trashing;
public static event TypedEventHandler<IMediaService, MoveEventArgs<IMedia>> Trashing;
/// <summary>
/// Occurs after Content is moved to Recycle Bin
/// </summary>
public static event EventHandler<MoveEventArgs> Trashed;
public static event TypedEventHandler<IMediaService, MoveEventArgs<IMedia>> Trashed;
/// <summary>
/// Occurs before Move
/// </summary>
public static event EventHandler<MoveEventArgs> Moving;
public static event TypedEventHandler<IMediaService, MoveEventArgs<IMedia>> Moving;
/// <summary>
/// Occurs after Move
/// </summary>
public static event EventHandler<MoveEventArgs> Moved;
public static event TypedEventHandler<IMediaService, MoveEventArgs<IMedia>> Moved;
#endregion
}
}