Rearranging using statements in MediaService to ensure events are fired before after using, and that the AuditTrail writes after the uow has been disposed.

Adding GetById overload in MediaService to allow lookup by UniqueId guid.
This commit is contained in:
Morten Christensen
2012-12-15 11:17:32 -01:00
parent fcda8cc7ca
commit fabe5f83a7
3 changed files with 204 additions and 185 deletions

View File

@@ -93,7 +93,6 @@ namespace Umbraco.Core.Persistence.UnitOfWork
/// </remarks>
public void Commit()
{
using (var transaction = Database.GetTransaction())
{
foreach (var operation in _operations.OrderBy(o => o.ProcessDate))
@@ -113,7 +112,6 @@ namespace Umbraco.Core.Persistence.UnitOfWork
}
transaction.Complete();
}
// Clear everything
_operations.Clear();

View File

@@ -88,14 +88,14 @@ namespace Umbraco.Core.Services
throw new Exception(string.Format("ContentType matching the passed in Alias: '{0}' was null",
contentTypeAlias));
}
content = new Content(parentId, contentType);
var e = new NewEventArgs { Alias = contentTypeAlias, ParentId = parentId };
if (Creating != null)
Creating(content, e);
if (!e.Cancel)
{
content = new Content(parentId, contentType);
SetUser(content, userId);
SetWriter(content, userId);
@@ -137,7 +137,6 @@ namespace Umbraco.Core.Services
}
}
/// <summary>
/// Gets a collection of <see cref="IContent"/> objects by the Id of the <see cref="IContentType"/>
/// </summary>
@@ -1369,7 +1368,11 @@ namespace Umbraco.Core.Services
/// <summary>
/// Occurs after Create
/// </summary>
public static event EventHandler<NewEventArgs> Created;
/// <remarks>
/// Please note that the Content object has been created, but not saved
/// so it does not have an identity yet (meaning no Id has been set).
/// </remarks>
public static event EventHandler<NewEventArgs> Created;
/// <summary>
/// Occurs before Copy

View File

@@ -39,54 +39,54 @@ namespace Umbraco.Core.Services
_userService = userService;
}
/// <summary>
/// Creates an <see cref="IMedia"/> object using the alias of the <see cref="IMediaType"/>
/// that this Media is based on.
/// </summary>
/// <param name="parentId">Id of Parent for the new Media item</param>
/// <param name="mediaTypeAlias">Alias of the <see cref="IMediaType"/></param>
/// <param name="userId">Optional id of the user creating the media item</param>
/// <returns><see cref="IMedia"/></returns>
public IMedia CreateMedia(int parentId, string mediaTypeAlias, int userId = -1)
{
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow))
{
var query = Query<IMediaType>.Builder.Where(x => x.Alias == mediaTypeAlias);
var mediaTypes = repository.GetByQuery(query);
/// <summary>
/// Creates an <see cref="IMedia"/> object using the alias of the <see cref="IMediaType"/>
/// that this Media is based on.
/// </summary>
/// <param name="parentId">Id of Parent for the new Media item</param>
/// <param name="mediaTypeAlias">Alias of the <see cref="IMediaType"/></param>
/// <param name="userId">Optional id of the user creating the media item</param>
/// <returns><see cref="IMedia"/></returns>
public IMedia CreateMedia(int parentId, string mediaTypeAlias, int userId = -1)
{
IMediaType mediaType = null;
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow))
{
var query = Query<IMediaType>.Builder.Where(x => x.Alias == mediaTypeAlias);
var mediaTypes = repository.GetByQuery(query);
if (!mediaTypes.Any())
throw new Exception(string.Format("No ContentType matching the passed in Alias: '{0}' was found", mediaTypeAlias));
if (!mediaTypes.Any())
throw new Exception(string.Format("No ContentType matching the passed in Alias: '{0}' was found",
mediaTypeAlias));
var mediaType = mediaTypes.First();
mediaType = mediaTypes.First();
if (mediaType == null)
throw new Exception(string.Format("ContentType matching the passed in Alias: '{0}' was null", mediaTypeAlias));
if (mediaType == null)
throw new Exception(string.Format("ContentType matching the passed in Alias: '{0}' was null",
mediaTypeAlias));
}
var media = new Models.Media(parentId, mediaType);
var media = new Models.Media(parentId, mediaType);
var e = new NewEventArgs {Alias = mediaTypeAlias, ParentId = parentId};
var e = new NewEventArgs { Alias = mediaTypeAlias, ParentId = parentId };
if (Creating != null)
Creating(media, e);
if (Creating != null)
Creating(media, e);
if (!e.Cancel)
{
SetUser(media, userId);
if (!e.Cancel)
{
SetUser(media, userId);
if (Created != null)
Created(media, e);
if (Created != null)
Created(media, e);
Audit.Add(AuditTypes.New, "", media.CreatorId, media.Id);
}
Audit.Add(AuditTypes.New, "", media.CreatorId, media.Id);
}
return media;
}
}
return media;
}
/// <summary>
/// <summary>
/// Gets an <see cref="IMedia"/> object by Id
/// </summary>
/// <param name="id">Id of the Content to retrieve</param>
@@ -100,6 +100,21 @@ namespace Umbraco.Core.Services
}
}
/// <summary>
/// Gets an <see cref="IMedia"/> object by its 'UniqueId'
/// </summary>
/// <param name="key">Guid key of the Media to retrieve</param>
/// <returns><see cref="IMedia"/></returns>
public IMedia GetById(Guid key)
{
using (var repository = _repositoryFactory.CreateMediaRepository(_uowProvider.GetUnitOfWork()))
{
var query = Query<IMedia>.Builder.Where(x => x.Key == key);
var contents = repository.GetByQuery(query);
return contents.SingleOrDefault();
}
}
/// <summary>
/// Gets a collection of <see cref="IMedia"/> objects by Parent Id
/// </summary>
@@ -209,37 +224,37 @@ namespace Umbraco.Core.Services
}
}
/// <summary>
/// Deletes an <see cref="IMedia"/> object by moving it to the Recycle Bin
/// </summary>
/// <param name="media">The <see cref="IMedia"/> to delete</param>
/// <param name="userId">Id of the User deleting the Media</param>
public void MoveToRecycleBin(IMedia media, int userId = -1)
{
//TODO If media item has children those should also be moved to the recycle bin as well
/// <summary>
/// Deletes an <see cref="IMedia"/> object by moving it to the Recycle Bin
/// </summary>
/// <param name="media">The <see cref="IMedia"/> to delete</param>
/// <param name="userId">Id of the User deleting the Media</param>
public void MoveToRecycleBin(IMedia media, int userId = -1)
{
//TODO If media item has children those should also be moved to the recycle bin as well
var e = new MoveEventArgs {ParentId = -20};
if (Trashing != null)
Trashing(media, e);
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
{
var e = new MoveEventArgs { ParentId = -20 };
if (Trashing != null)
Trashing(media, e);
if (!e.Cancel)
{
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
{
((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();
if (Trashed != null)
Trashed(media, e);
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);
}
}
}
/// <summary>
/// <summary>
/// Empties the Recycle Bin by deleting all <see cref="IMedia"/> that resides in the bin
/// </summary>
public void EmptyRecycleBin()
@@ -255,140 +270,139 @@ namespace Umbraco.Core.Services
repository.Delete(content);
}
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, -20);
}
/// <summary>
/// Deletes all media of specified type. All children of deleted media is moved to Recycle Bin.
/// </summary>
/// <remarks>This needs extra care and attention as its potentially a dangerous and extensive operation</remarks>
/// <param name="mediaTypeId">Id of the <see cref="IMediaType"/></param>
/// <param name="userId">Optional id of the user deleting the media</param>
public void DeleteMediaOfType(int mediaTypeId, int userId = -1)
{
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
{
//NOTE What about media that has the contenttype as part of its composition?
//The ContentType has to be removed from the composition somehow as it would otherwise break
//Dbl.check+test that the ContentType's Id is removed from the ContentType2ContentType table
var query = Query<IMedia>.Builder.Where(x => x.ContentTypeId == mediaTypeId);
var contents = repository.GetByQuery(query);
/// <summary>
/// Deletes all media of specified type. All children of deleted media is moved to Recycle Bin.
/// </summary>
/// <remarks>This needs extra care and attention as its potentially a dangerous and extensive operation</remarks>
/// <param name="mediaTypeId">Id of the <see cref="IMediaType"/></param>
/// <param name="userId">Optional id of the user deleting the media</param>
public void DeleteMediaOfType(int mediaTypeId, int userId = -1)
{
var uow = _uowProvider.GetUnitOfWork();
var repository = _repositoryFactory.CreateMediaRepository(uow);
//NOTE What about media that has the contenttype as part of its composition?
//The ContentType has to be removed from the composition somehow as it would otherwise break
//Dbl.check+test that the ContentType's Id is removed from the ContentType2ContentType table
var query = Query<IMedia>.Builder.Where(x => x.ContentTypeId == mediaTypeId);
var contents = repository.GetByQuery(query);
var e = new DeleteEventArgs { Id = mediaTypeId };
if (Deleting != null)
Deleting(contents, e);
var e = new DeleteEventArgs {Id = mediaTypeId};
if (Deleting != null)
Deleting(contents, e);
if (!e.Cancel)
{
foreach (var content in contents)
{
((Core.Models.Media)content).ChangeTrashedState(true);
repository.AddOrUpdate(content);
}
if (!e.Cancel)
{
foreach (var content in contents)
{
((Core.Models.Media) content).ChangeTrashedState(true);
repository.AddOrUpdate(content);
}
uow.Commit();
uow.Commit();
uow.Dispose();
if (Deleted != null)
Deleted(contents, e);
if (Deleted != null)
Deleted(contents, e);
Audit.Add(AuditTypes.Delete, "Delete Media items by Type performed by user", userId == -1 ? 0 : userId, -1);
}
}
}
Audit.Add(AuditTypes.Delete, "Delete Media items by Type performed by user", userId == -1 ? 0 : userId, -1);
}
}
/// <summary>
/// Permanently deletes an <see cref="IMedia"/> object
/// </summary>
/// <remarks>
/// Please note that this method will completely remove the Media from the database,
/// but current not from the file system.
/// </remarks>
/// <param name="media">The <see cref="IMedia"/> to delete</param>
/// <param name="userId">Id of the User deleting the Media</param>
public void Delete(IMedia media, int userId = -1)
{
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
{
var e = new DeleteEventArgs { Id = media.Id };
if (Deleting != null)
Deleting(media, e);
/// <summary>
/// Permanently deletes an <see cref="IMedia"/> object
/// </summary>
/// <remarks>
/// Please note that this method will completely remove the Media from the database,
/// but current not from the file system.
/// </remarks>
/// <param name="media">The <see cref="IMedia"/> to delete</param>
/// <param name="userId">Id of the User deleting the Media</param>
public void Delete(IMedia media, int userId = -1)
{
var e = new DeleteEventArgs {Id = media.Id};
if (Deleting != null)
Deleting(media, e);
if (!e.Cancel)
{
repository.Delete(media);
uow.Commit();
if (!e.Cancel)
{
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
{
repository.Delete(media);
uow.Commit();
}
if (Deleted != null)
Deleted(media, e);
if (Deleted != null)
Deleted(media, e);
Audit.Add(AuditTypes.Delete, "Delete Media performed by user", userId == -1 ? 0 : userId, media.Id);
}
}
}
Audit.Add(AuditTypes.Delete, "Delete Media performed by user", userId == -1 ? 0 : userId, media.Id);
}
}
/// <summary>
/// Saves a single <see cref="IMedia"/> object
/// </summary>
/// <param name="media">The <see cref="IMedia"/> to save</param>
/// <param name="userId">Id of the User saving the Content</param>
public void Save(IMedia media, int userId = -1)
{
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
{
var e = new SaveEventArgs();
if (Saving != null)
Saving(media, e);
/// <summary>
/// Saves a single <see cref="IMedia"/> object
/// </summary>
/// <param name="media">The <see cref="IMedia"/> to save</param>
/// <param name="userId">Id of the User saving the Content</param>
public void Save(IMedia media, int userId = -1)
{
var e = new SaveEventArgs();
if (Saving != null)
Saving(media, e);
if (!e.Cancel)
{
SetUser(media, userId);
repository.AddOrUpdate(media);
uow.Commit();
if (!e.Cancel)
{
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
{
SetUser(media, userId);
repository.AddOrUpdate(media);
uow.Commit();
}
if (Saved != null)
Saved(media, e);
}
Audit.Add(AuditTypes.Save, "Save Media performed by user", media.CreatorId, media.Id);
}
}
if (Saved != null)
Saved(media, e);
}
Audit.Add(AuditTypes.Save, "Save Media performed by user", media.CreatorId, media.Id);
}
/// <summary>
/// Saves a collection of <see cref="IMedia"/> objects
/// </summary>
/// <param name="medias">Collection of <see cref="IMedia"/> to save</param>
/// <param name="userId">Id of the User saving the Content</param>
public void Save(IEnumerable<IMedia> medias, int userId = -1)
{
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
{
var e = new SaveEventArgs();
if (Saving != null)
Saving(medias, e);
/// <summary>
/// Saves a collection of <see cref="IMedia"/> objects
/// </summary>
/// <param name="medias">Collection of <see cref="IMedia"/> to save</param>
/// <param name="userId">Id of the User saving the Content</param>
public void Save(IEnumerable<IMedia> medias, int userId = -1)
{
var e = new SaveEventArgs();
if (Saving != null)
Saving(medias, e);
if (!e.Cancel)
{
foreach (var media in medias)
{
SetUser(media, userId);
repository.AddOrUpdate(media);
}
uow.Commit();
if (!e.Cancel)
{
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
{
foreach (var media in medias)
{
SetUser(media, userId);
repository.AddOrUpdate(media);
}
uow.Commit();
}
if (Saved != null)
Saved(medias, e);
if (Saved != null)
Saved(medias, e);
Audit.Add(AuditTypes.Save, "Save Media items performed by user", userId == -1 ? 0 : userId, -1);
}
}
}
Audit.Add(AuditTypes.Save, "Save Media items performed by user", userId == -1 ? 0 : userId, -1);
}
}
/// <summary>
/// <summary>
/// Internal method to set the HttpContextBase for testing.
/// </summary>
/// <param name="httpContext"><see cref="HttpContextBase"/></param>
@@ -457,6 +471,10 @@ namespace Umbraco.Core.Services
/// <summary>
/// Occurs after Create
/// </summary>
/// <remarks>
/// Please note that the Media object has been created, but not saved
/// so it does not have an identity yet (meaning no Id has been set).
/// </remarks>
public static event EventHandler<NewEventArgs> Created;
/// <summary>