From 3ff0cca03381740a2b39b83f7e3d3b8d90d8ac05 Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 16 Dec 2013 18:23:40 +1100 Subject: [PATCH] back ports entity service updates --- .../Interfaces/IEntityRepository.cs | 2 + src/Umbraco.Core/Services/EntityService.cs | 75 +++++++++++++++++-- src/Umbraco.Core/Services/IEntityService.cs | 36 +++++++++ .../Services/EntityServiceTests.cs | 42 +++++++---- 4 files changed, 135 insertions(+), 20 deletions(-) diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IEntityRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IEntityRepository.cs index 6df0293be0..510ef18421 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IEntityRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IEntityRepository.cs @@ -7,6 +7,8 @@ namespace Umbraco.Core.Persistence.Repositories { public interface IEntityRepository : IRepository { + IUmbracoEntity GetByKey(Guid key); + IUmbracoEntity GetByKey(Guid key, Guid objectTypeId); IUmbracoEntity Get(int id); IUmbracoEntity Get(int id, Guid objectTypeId); IEnumerable GetAll(Guid objectTypeId, params int[] ids); diff --git a/src/Umbraco.Core/Services/EntityService.cs b/src/Umbraco.Core/Services/EntityService.cs index 7e103c687b..3837603d88 100644 --- a/src/Umbraco.Core/Services/EntityService.cs +++ b/src/Umbraco.Core/Services/EntityService.cs @@ -39,6 +39,27 @@ namespace Umbraco.Core.Services }; } + public IUmbracoEntity GetByKey(Guid key, bool loadBaseType = true) + { + if (loadBaseType) + { + using (var repository = _repositoryFactory.CreateEntityRepository(_uowProvider.GetUnitOfWork())) + { + return repository.GetByKey(key); + } + } + + //SD: TODO: Need to enable this at some stage ... just need to ask Morten what the deal is with what this does. + throw new NotSupportedException(); + + //var objectType = GetObjectType(key); + //var entityType = GetEntityType(objectType); + //var typeFullName = entityType.FullName; + //var entity = _supportedObjectTypes[typeFullName].Item2(id); + + //return entity; + } + /// /// Gets an UmbracoEntity by its Id, and optionally loads the complete object graph. /// @@ -66,6 +87,27 @@ namespace Umbraco.Core.Services return entity; } + public IUmbracoEntity GetByKey(Guid key, UmbracoObjectTypes umbracoObjectType, bool loadBaseType = true) + { + if (loadBaseType) + { + var objectTypeId = umbracoObjectType.GetGuid(); + using (var repository = _repositoryFactory.CreateEntityRepository(_uowProvider.GetUnitOfWork())) + { + return repository.GetByKey(key, objectTypeId); + } + } + + //SD: TODO: Need to enable this at some stage ... just need to ask Morten what the deal is with what this does. + throw new NotSupportedException(); + + //var entityType = GetEntityType(umbracoObjectType); + //var typeFullName = entityType.FullName; + //var entity = _supportedObjectTypes[typeFullName].Item2(id); + + //return entity; + } + /// /// Gets an UmbracoEntity by its Id and UmbracoObjectType, and optionally loads the complete object graph. /// @@ -94,6 +136,11 @@ namespace Umbraco.Core.Services return entity; } + public IUmbracoEntity GetByKey(Guid key, bool loadBaseType = true) where T : IUmbracoEntity + { + throw new NotImplementedException(); + } + /// /// Gets an UmbracoEntity by its Id and specified Type. Optionally loads the complete object graph. /// @@ -255,12 +302,12 @@ namespace Umbraco.Core.Services /// An enumerable list of objects public virtual IEnumerable GetAll() where T : IUmbracoEntity { - var typeFullName = typeof (T).FullName; + var typeFullName = typeof(T).FullName; Mandate.That(_supportedObjectTypes.ContainsKey(typeFullName), () => - { - throw new NotSupportedException - ("The passed in type is not supported"); - }); + { + throw new NotSupportedException + ("The passed in type is not supported"); + }); var objectType = _supportedObjectTypes[typeFullName].Item1; return GetAll(objectType); @@ -326,6 +373,22 @@ namespace Umbraco.Core.Services } } + /// + /// Gets the UmbracoObjectType from the integer id of an IUmbracoEntity. + /// + /// Unique Id of the entity + /// + public virtual UmbracoObjectTypes GetObjectType(Guid key) + { + using (var uow = _uowProvider.GetUnitOfWork()) + { + var sql = new Sql().Select("nodeObjectType").From().Where(x => x.UniqueId == key); + var nodeObjectTypeId = uow.Database.ExecuteScalar(sql); + var objectTypeId = new Guid(nodeObjectTypeId); + return UmbracoObjectTypesExtensions.GetUmbracoObjectType(objectTypeId); + } + } + /// /// Gets the UmbracoObjectType from an IUmbracoEntity. /// @@ -367,7 +430,7 @@ namespace Umbraco.Core.Services if (attribute == null) throw new NullReferenceException("The passed in UmbracoObjectType does not contain an UmbracoObjectTypeAttribute, which is used to retrieve the Type."); - if(attribute.ModelType == null) + if (attribute.ModelType == null) throw new NullReferenceException("The passed in UmbracoObjectType does not contain a Type definition"); return attribute.ModelType; diff --git a/src/Umbraco.Core/Services/IEntityService.cs b/src/Umbraco.Core/Services/IEntityService.cs index c32d755c3f..e7cbece1e1 100644 --- a/src/Umbraco.Core/Services/IEntityService.cs +++ b/src/Umbraco.Core/Services/IEntityService.cs @@ -7,6 +7,18 @@ namespace Umbraco.Core.Services { public interface IEntityService { + + /// + /// Gets an UmbracoEntity by its Id, and optionally loads the complete object graph. + /// + /// + /// By default this will load the base type with a minimum set of properties. + /// + /// Unique Id of the object to retrieve + /// Optional bool to load the complete object graph when set to False. + /// An + IUmbracoEntity GetByKey(Guid key, bool loadBaseType = true); + /// /// Gets an UmbracoEntity by its Id, and optionally loads the complete object graph. /// @@ -18,6 +30,18 @@ namespace Umbraco.Core.Services /// An IUmbracoEntity Get(int id, bool loadBaseType = true); + /// + /// Gets an UmbracoEntity by its Id and UmbracoObjectType, and optionally loads the complete object graph. + /// + /// + /// By default this will load the base type with a minimum set of properties. + /// + /// Unique Id of the object to retrieve + /// UmbracoObjectType of the entity to retrieve + /// Optional bool to load the complete object graph when set to False. + /// An + IUmbracoEntity GetByKey(Guid key, UmbracoObjectTypes umbracoObjectType, bool loadBaseType = true); + /// /// Gets an UmbracoEntity by its Id and UmbracoObjectType, and optionally loads the complete object graph. /// @@ -30,6 +54,18 @@ namespace Umbraco.Core.Services /// An IUmbracoEntity Get(int id, UmbracoObjectTypes umbracoObjectType, bool loadBaseType = true); + /// + /// Gets an UmbracoEntity by its Id and specified Type. Optionally loads the complete object graph. + /// + /// + /// By default this will load the base type with a minimum set of properties. + /// + /// Type of the model to retrieve. Must be based on an + /// Unique Id of the object to retrieve + /// Optional bool to load the complete object graph when set to False. + /// An + IUmbracoEntity GetByKey(Guid key, bool loadBaseType = true) where T : IUmbracoEntity; + /// /// Gets an UmbracoEntity by its Id and specified Type. Optionally loads the complete object graph. /// diff --git a/src/Umbraco.Tests/Services/EntityServiceTests.cs b/src/Umbraco.Tests/Services/EntityServiceTests.cs index 427aa1a6c4..00384b1233 100644 --- a/src/Umbraco.Tests/Services/EntityServiceTests.cs +++ b/src/Umbraco.Tests/Services/EntityServiceTests.cs @@ -3,6 +3,7 @@ using System.Linq; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Models; +using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Entities; namespace Umbraco.Tests.Services @@ -25,6 +26,11 @@ namespace Umbraco.Tests.Services base.TearDown(); } + protected override DatabaseBehavior DatabaseTestBehavior + { + get { return DatabaseBehavior.NewSchemaPerFixture; } + } + [Test] public void EntityService_Can_Find_All_Content_By_UmbracoObjectTypes() { @@ -132,28 +138,36 @@ namespace Umbraco.Tests.Services Assert.That( entities.Any( x => - ((UmbracoEntity) x).UmbracoProperties.Any( + ((UmbracoEntity)x).UmbracoProperties.Any( y => y.DataTypeControlId == new Guid(Constants.PropertyEditors.UploadField))), Is.True); } + private static bool _isSetup = false; + public override void CreateTestData() { - base.CreateTestData(); + if (_isSetup == false) + { + _isSetup = true; - //Create and Save folder-Media -> 1050 - var folderMediaType = ServiceContext.ContentTypeService.GetMediaType(1031); - var folder = MockedMedia.CreateMediaFolder(folderMediaType, -1); - ServiceContext.MediaService.Save(folder, 0); + base.CreateTestData(); - //Create and Save image-Media -> 1051 - var imageMediaType = ServiceContext.ContentTypeService.GetMediaType(1032); - var image = MockedMedia.CreateMediaImage(imageMediaType, folder.Id); - ServiceContext.MediaService.Save(image, 0); + //Create and Save folder-Media -> 1050 + var folderMediaType = ServiceContext.ContentTypeService.GetMediaType(1031); + var folder = MockedMedia.CreateMediaFolder(folderMediaType, -1); + ServiceContext.MediaService.Save(folder, 0); + + //Create and Save image-Media -> 1051 + var imageMediaType = ServiceContext.ContentTypeService.GetMediaType(1032); + var image = MockedMedia.CreateMediaImage(imageMediaType, folder.Id); + ServiceContext.MediaService.Save(image, 0); + + //Create and Save file-Media -> 1052 + var fileMediaType = ServiceContext.ContentTypeService.GetMediaType(1033); + var file = MockedMedia.CreateMediaFile(fileMediaType, folder.Id); + ServiceContext.MediaService.Save(file, 0); + } - //Create and Save file-Media -> 1052 - var fileMediaType = ServiceContext.ContentTypeService.GetMediaType(1033); - var file = MockedMedia.CreateMediaFile(fileMediaType, folder.Id); - ServiceContext.MediaService.Save(file, 0); } } } \ No newline at end of file