From 7e728af93c2def7ccca15c217c35eafc3ace01e2 Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Mon, 10 Dec 2012 13:04:18 -0100 Subject: [PATCH] Adding events and audit trail to DataTypeService --- .../Services/ContentTypeService.cs | 10 -- src/Umbraco.Core/Services/DataTypeService.cs | 94 ++++++++++++++----- src/Umbraco.Core/Services/IDataTypeService.cs | 2 +- 3 files changed, 72 insertions(+), 34 deletions(-) diff --git a/src/Umbraco.Core/Services/ContentTypeService.cs b/src/Umbraco.Core/Services/ContentTypeService.cs index 86f25f2ea9..68b88ae6cf 100644 --- a/src/Umbraco.Core/Services/ContentTypeService.cs +++ b/src/Umbraco.Core/Services/ContentTypeService.cs @@ -527,16 +527,6 @@ namespace Umbraco.Core.Services /// Occurs after Save /// public static event EventHandler Saved; - - /// - /// Occurs before Create - /// - public static event EventHandler Creating; - - /// - /// Occurs after Create - /// - public static event EventHandler Created; #endregion } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/DataTypeService.cs b/src/Umbraco.Core/Services/DataTypeService.cs index cf9f68896e..95e725f7fc 100644 --- a/src/Umbraco.Core/Services/DataTypeService.cs +++ b/src/Umbraco.Core/Services/DataTypeService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Umbraco.Core.Auditing; using Umbraco.Core.Models; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.Querying; @@ -27,6 +28,7 @@ namespace Umbraco.Core.Services { _unitOfWork = provider.GetUnitOfWork(); _dataTypeService = RepositoryResolver.Current.Factory.CreateDataTypeDefinitionRepository(_unitOfWork); + _contentTypeRepository = RepositoryResolver.Current.Factory.CreateContentTypeRepository(_unitOfWork); } /// @@ -86,11 +88,23 @@ namespace Umbraco.Core.Services /// /// to save /// Id of the user issueing the save - public void Save(IDataTypeDefinition dataTypeDefinition, int userId) + public void Save(IDataTypeDefinition dataTypeDefinition, int userId = -1) { - var repository = _dataTypeService; - repository.AddOrUpdate(dataTypeDefinition); - _unitOfWork.Commit(); + var e = new SaveEventArgs(); + if (Saving != null) + Saving(dataTypeDefinition, e); + + if (!e.Cancel) + { + dataTypeDefinition.CreatorId = userId > -1 ? userId : 0; + _dataTypeService.AddOrUpdate(dataTypeDefinition); + _unitOfWork.Commit(); + + if (Saved != null) + Saved(dataTypeDefinition, e); + + Audit.Add(AuditTypes.Save, string.Format("Save DataTypeDefinition issued by user"), userId == -1 ? 0 : userId, dataTypeDefinition.Id); + } } /// @@ -101,34 +115,46 @@ namespace Umbraco.Core.Services /// all the data that references this . /// /// to delete - /// Id of the user issueing the deletion - public void Delete(IDataTypeDefinition dataTypeDefinition, int userId) - { - //Find ContentTypes using this IDataTypeDefinition on a PropertyType - var contentTypeRepository = _contentTypeRepository; - var query = Query.Builder.Where(x => x.DataTypeId == dataTypeDefinition.Id); - var contentTypes = contentTypeRepository.GetByQuery(query); + /// Optional Id of the user issueing the deletion + public void Delete(IDataTypeDefinition dataTypeDefinition, int userId = -1) + { + var e = new DeleteEventArgs { Id = dataTypeDefinition.Id }; + if (Deleting != null) + Deleting(dataTypeDefinition, e); - //Loop through the list of results and remove the PropertyTypes that references the DataTypeDefinition that is being deleted - foreach (var contentType in contentTypes) + if (!e.Cancel) { - if(contentType == null) continue; + //Find ContentTypes using this IDataTypeDefinition on a PropertyType + var contentTypeRepository = _contentTypeRepository; + var query = Query.Builder.Where(x => x.DataTypeId == dataTypeDefinition.Id); + var contentTypes = contentTypeRepository.GetByQuery(query); - foreach (var group in contentType.PropertyGroups) + //Loop through the list of results and remove the PropertyTypes that references the DataTypeDefinition that is being deleted + foreach (var contentType in contentTypes) { - var types = group.PropertyTypes.Where(x => x.DataTypeId == dataTypeDefinition.Id); - foreach (var propertyType in types) + if (contentType == null) continue; + + foreach (var group in contentType.PropertyGroups) { - group.PropertyTypes.Remove(propertyType); + var types = group.PropertyTypes.Where(x => x.DataTypeId == dataTypeDefinition.Id); + foreach (var propertyType in types) + { + group.PropertyTypes.Remove(propertyType); + } } + + contentTypeRepository.AddOrUpdate(contentType); } - contentTypeRepository.AddOrUpdate(contentType); - } + var repository = _dataTypeService; + repository.Delete(dataTypeDefinition); + _unitOfWork.Commit(); - var repository = _dataTypeService; - repository.Delete(dataTypeDefinition); - _unitOfWork.Commit(); + if (Deleted != null) + Deleted(dataTypeDefinition, e); + + Audit.Add(AuditTypes.Delete, string.Format("Delete DataTypeDefinition issued by user"), userId == -1 ? 0 : userId, dataTypeDefinition.Id); + } } /// @@ -149,5 +175,27 @@ namespace Umbraco.Core.Services { return DataTypesResolver.Current.DataTypes; } + + #region Event Handlers + /// + /// Occurs before Delete + /// + public static event EventHandler Deleting; + + /// + /// Occurs after Delete + /// + public static event EventHandler Deleted; + + /// + /// Occurs before Save + /// + public static event EventHandler Saving; + + /// + /// Occurs after Save + /// + public static event EventHandler Saved; + #endregion } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/IDataTypeService.cs b/src/Umbraco.Core/Services/IDataTypeService.cs index d900a2c473..e0c0be15bc 100644 --- a/src/Umbraco.Core/Services/IDataTypeService.cs +++ b/src/Umbraco.Core/Services/IDataTypeService.cs @@ -47,7 +47,7 @@ namespace Umbraco.Core.Services /// /// to delete /// Id of the user issueing the deletion - void Delete(IDataTypeDefinition dataTypeDefinition, int userId); + void Delete(IDataTypeDefinition dataTypeDefinition, int userId = -1); /// /// Gets the specified by it's unique ID