Redoing the fix for U4-1875

This commit is contained in:
Morten Christensen
2013-03-22 12:13:06 -01:00
parent 9c89a56d8d
commit c5710277a5
5 changed files with 42 additions and 23 deletions

View File

@@ -66,6 +66,15 @@ namespace Umbraco.Core.Models.EntityBase
[DataMember]
public DateTime UpdateDate { get; set; }
/// <summary>
/// Gets or sets the WasCancelled flag, which is used to track
/// whether some action against an entity was cancelled through some event.
/// This only exists so we have a way to check if an event was cancelled through
/// the new api, which also needs to take effect in the legacy api.
/// </summary>
[IgnoreDataMember]
internal bool WasCancelled { get; set; }
/// <summary>
/// Property changed event
/// </summary>

View File

@@ -64,13 +64,15 @@ namespace Umbraco.Core.Services
/// <returns><see cref="IContent"/></returns>
public IContent CreateContent(string name, int parentId, string contentTypeAlias, int userId = 0)
{
IContentType contentType = FindContentTypeByAlias(contentTypeAlias);
IContent content = null;
var contentType = FindContentTypeByAlias(contentTypeAlias);
var content = new Content(name, parentId, contentType); ;
if (Creating.IsRaisedEventCancelled(new NewEventArgs<IContent>(content, contentTypeAlias, parentId), this))
return content;
{
content.WasCancelled = true;
return content;
}
content = new Content(name, parentId, contentType);
content.CreatorId = userId;
content.WriterId = userId;
@@ -92,13 +94,15 @@ namespace Umbraco.Core.Services
/// <returns><see cref="IContent"/></returns>
public IContent CreateContent(string name, IContent parent, string contentTypeAlias, int userId = 0)
{
IContentType contentType = FindContentTypeByAlias(contentTypeAlias);
IContent content = null;
var contentType = FindContentTypeByAlias(contentTypeAlias);
var content = new Content(name, parent, contentType);
if (Creating.IsRaisedEventCancelled(new NewEventArgs<IContent>(content, contentTypeAlias, parent), this))
{
content.WasCancelled = true;
return content;
}
content = new Content(name, parent, contentType);
content.CreatorId = userId;
content.WriterId = userId;

View File

@@ -46,11 +46,8 @@ namespace Umbraco.Core.Services
/// <returns><see cref="IMedia"/></returns>
public IMedia CreateMedia(string name, int parentId, string mediaTypeAlias, int userId = 0)
{
Models.Media media = null;
IMediaType mediaType = null;
if (Creating.IsRaisedEventCancelled(new NewEventArgs<IMedia>(media, mediaTypeAlias, parentId), this))
return media;
IMediaType mediaType;
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow))
{
@@ -68,7 +65,13 @@ namespace Umbraco.Core.Services
mediaTypeAlias));
}
media = new Models.Media(name, parentId, mediaType);
var media = new Models.Media(name, parentId, mediaType);
if (Creating.IsRaisedEventCancelled(new NewEventArgs<IMedia>(media, mediaTypeAlias, parentId), this))
{
media.WasCancelled = true;
return media;
}
media.CreatorId = userId;
@@ -90,11 +93,7 @@ namespace Umbraco.Core.Services
/// <returns><see cref="IMedia"/></returns>
public IMedia CreateMedia(string name, IMedia parent, string mediaTypeAlias, int userId = 0)
{
Models.Media media = null;
IMediaType mediaType = null;
if (Creating.IsRaisedEventCancelled(new NewEventArgs<IMedia>(media, mediaTypeAlias, parent), this))
return media;
IMediaType mediaType;
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow))
@@ -113,7 +112,12 @@ namespace Umbraco.Core.Services
mediaTypeAlias));
}
media = new Models.Media(name, parent, mediaType);
var media = new Models.Media(name, parent, mediaType);
if (Creating.IsRaisedEventCancelled(new NewEventArgs<IMedia>(media, mediaTypeAlias, parent), this))
{
media.WasCancelled = true;
return media;
}
media.CreatorId = userId;

View File

@@ -1,6 +1,7 @@
using System;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.EntityBase;
using umbraco.BusinessLogic;
using umbraco.DataLayer;
using System.Collections;
@@ -85,8 +86,8 @@ namespace umbraco.cms.businesslogic.media
}
var media = ApplicationContext.Current.Services.MediaService.CreateMedia(Name, ParentId, dct.Alias, u.Id);
//The media object will only be null if the 'Creating' event has been cancelled
if (media == null)
//The media object will only have the 'WasCancelled' flag set to 'True' if the 'Creating' event has been cancelled
if (((Entity)media).WasCancelled)
return null;
ApplicationContext.Current.Services.MediaService.Save(media);

View File

@@ -7,6 +7,7 @@ using System.Xml;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Logging;
using Umbraco.Core.Models.EntityBase;
using Umbraco.Core.Persistence.Caching;
using Umbraco.Core.Services;
using umbraco.BusinessLogic;
@@ -288,8 +289,8 @@ namespace umbraco.cms.businesslogic.web
//Create a new IContent object based on the passed in DocumentType's alias, set the name and save it
IContent content = ApplicationContext.Current.Services.ContentService.CreateContent(Name, ParentId, dct.Alias, u.Id);
//The content object will only be null if the 'Creating' event has been cancelled, so we return null.
if (content == null)
//The content object will only have the 'WasCancelled' flag set to 'True' if the 'Creating' event has been cancelled, so we return null.
if (((Entity)content).WasCancelled)
return null;
//don't raise events here (false), they will get raised with the d.Save() call.