From fabe5f83a782bcf6389a5f3bfd88465500195443 Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Sat, 15 Dec 2012 11:17:32 -0100 Subject: [PATCH] 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. --- .../UnitOfWork/PetaPocoUnitOfWork.cs | 2 - src/Umbraco.Core/Services/ContentService.cs | 11 +- src/Umbraco.Core/Services/MediaService.cs | 376 +++++++++--------- 3 files changed, 204 insertions(+), 185 deletions(-) diff --git a/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWork.cs b/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWork.cs index 6f9f5fc220..4cdf4de85e 100644 --- a/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWork.cs +++ b/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWork.cs @@ -93,7 +93,6 @@ namespace Umbraco.Core.Persistence.UnitOfWork /// 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(); diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index d1a5aba49f..4865cb876c 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -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 } } - /// /// Gets a collection of objects by the Id of the /// @@ -1369,7 +1368,11 @@ namespace Umbraco.Core.Services /// /// Occurs after Create /// - public static event EventHandler Created; + /// + /// 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). + /// + public static event EventHandler Created; /// /// Occurs before Copy diff --git a/src/Umbraco.Core/Services/MediaService.cs b/src/Umbraco.Core/Services/MediaService.cs index 8ec6207c4a..b0cfaf3a80 100644 --- a/src/Umbraco.Core/Services/MediaService.cs +++ b/src/Umbraco.Core/Services/MediaService.cs @@ -39,54 +39,54 @@ namespace Umbraco.Core.Services _userService = userService; } - /// - /// Creates an object using the alias of the - /// that this Media is based on. - /// - /// Id of Parent for the new Media item - /// Alias of the - /// Optional id of the user creating the media item - /// - public IMedia CreateMedia(int parentId, string mediaTypeAlias, int userId = -1) - { - var uow = _uowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow)) - { - var query = Query.Builder.Where(x => x.Alias == mediaTypeAlias); - var mediaTypes = repository.GetByQuery(query); + /// + /// Creates an object using the alias of the + /// that this Media is based on. + /// + /// Id of Parent for the new Media item + /// Alias of the + /// Optional id of the user creating the media item + /// + 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.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; + } - /// + /// /// Gets an object by Id /// /// Id of the Content to retrieve @@ -100,6 +100,21 @@ namespace Umbraco.Core.Services } } + /// + /// Gets an object by its 'UniqueId' + /// + /// Guid key of the Media to retrieve + /// + public IMedia GetById(Guid key) + { + using (var repository = _repositoryFactory.CreateMediaRepository(_uowProvider.GetUnitOfWork())) + { + var query = Query.Builder.Where(x => x.Key == key); + var contents = repository.GetByQuery(query); + return contents.SingleOrDefault(); + } + } + /// /// Gets a collection of objects by Parent Id /// @@ -209,37 +224,37 @@ namespace Umbraco.Core.Services } } - /// - /// Deletes an object by moving it to the Recycle Bin - /// - /// The to delete - /// Id of the User deleting the Media - 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 + /// + /// Deletes an object by moving it to the Recycle Bin + /// + /// The to delete + /// Id of the User deleting the Media + 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); - } - } - } - - /// + /// /// Empties the Recycle Bin by deleting all that resides in the bin /// 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); } - /// - /// Deletes all media of specified type. All children of deleted media is moved to Recycle Bin. - /// - /// This needs extra care and attention as its potentially a dangerous and extensive operation - /// Id of the - /// Optional id of the user deleting the media - 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.Builder.Where(x => x.ContentTypeId == mediaTypeId); - var contents = repository.GetByQuery(query); + /// + /// Deletes all media of specified type. All children of deleted media is moved to Recycle Bin. + /// + /// This needs extra care and attention as its potentially a dangerous and extensive operation + /// Id of the + /// Optional id of the user deleting the media + 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.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); + } + } - /// - /// Permanently deletes an object - /// - /// - /// Please note that this method will completely remove the Media from the database, - /// but current not from the file system. - /// - /// The to delete - /// Id of the User deleting the Media - 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); + /// + /// Permanently deletes an object + /// + /// + /// Please note that this method will completely remove the Media from the database, + /// but current not from the file system. + /// + /// The to delete + /// Id of the User deleting the Media + 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); + } + } - /// - /// Saves a single object - /// - /// The to save - /// Id of the User saving the Content - 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); + /// + /// Saves a single object + /// + /// The to save + /// Id of the User saving the Content + 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); + } - /// - /// Saves a collection of objects - /// - /// Collection of to save - /// Id of the User saving the Content - public void Save(IEnumerable medias, int userId = -1) - { - var uow = _uowProvider.GetUnitOfWork(); - using (var repository = _repositoryFactory.CreateMediaRepository(uow)) - { - var e = new SaveEventArgs(); - if (Saving != null) - Saving(medias, e); + /// + /// Saves a collection of objects + /// + /// Collection of to save + /// Id of the User saving the Content + public void Save(IEnumerable 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); + } + } - /// + /// /// Internal method to set the HttpContextBase for testing. /// /// @@ -457,6 +471,10 @@ namespace Umbraco.Core.Services /// /// Occurs after Create /// + /// + /// 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). + /// public static event EventHandler Created; ///