From 6423914f015b69fbed060e80893520f681f7f513 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Wed, 13 Feb 2013 04:22:46 +0600 Subject: [PATCH] Creates ServerRegistrationService --- .../Models/Rdbms/ServerRegistrationDto.cs | 12 +- src/Umbraco.Core/Models/ServerRegistration.cs | 11 +- .../Factories/ServerRegistrationFactory.cs | 7 +- .../Mappers/ServerRegistrationMapper.cs | 1 + .../Persistence/RepositoryFactory.cs | 7 + src/Umbraco.Core/Services/ContentService.cs | 2 +- .../Services/ServerRegistrationService.cs | 103 +++++++++ src/Umbraco.Core/Umbraco.Core.csproj | 1 + .../ServerRegistrationRepositoryTest.cs | 201 +++++++++++------- 9 files changed, 261 insertions(+), 84 deletions(-) create mode 100644 src/Umbraco.Core/Services/ServerRegistrationService.cs diff --git a/src/Umbraco.Core/Models/Rdbms/ServerRegistrationDto.cs b/src/Umbraco.Core/Models/Rdbms/ServerRegistrationDto.cs index f7da0ea2ef..e9c79ae57e 100644 --- a/src/Umbraco.Core/Models/Rdbms/ServerRegistrationDto.cs +++ b/src/Umbraco.Core/Models/Rdbms/ServerRegistrationDto.cs @@ -14,9 +14,17 @@ namespace Umbraco.Core.Models.Rdbms public int Id { get; set; } [Column("address")] - [Length(100)] + [Length(500)] public string Address { get; set; } + /// + /// A unique column in the database, a computer name must always be unique! + /// + [Column("computerName")] + [Length(255)] + [Index(IndexTypes.UniqueNonClustered, Name = "IX_computerName")] + public string ComputerName { get; set; } + [Column("registeredDate")] [Constraint(Default = "getdate()")] public DateTime DateRegistered { get; set; } @@ -28,5 +36,7 @@ namespace Umbraco.Core.Models.Rdbms [Column("isActive")] [Index(IndexTypes.NonClustered)] public bool IsActive { get; set; } + + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Models/ServerRegistration.cs b/src/Umbraco.Core/Models/ServerRegistration.cs index 7e8fa70f44..692c8ab3bd 100644 --- a/src/Umbraco.Core/Models/ServerRegistration.cs +++ b/src/Umbraco.Core/Models/ServerRegistration.cs @@ -16,31 +16,36 @@ namespace Umbraco.Core.Models /// /// /// + /// /// /// - public ServerRegistration(int id, string serverAddress, DateTime createDate, DateTime updateDate) + public ServerRegistration(int id, string serverAddress, string computerName, DateTime createDate, DateTime updateDate) { UpdateDate = updateDate; CreateDate = createDate; Key = Id.ToString().EncodeAsGuid(); Id = id; ServerAddress = serverAddress; + ComputerName = computerName; } /// /// Creates a new instance for persisting a new item /// /// + /// /// - public ServerRegistration(string serverAddress, DateTime createDate) + public ServerRegistration(string serverAddress, string computerName, DateTime createDate) { CreateDate = createDate; UpdateDate = createDate; Key = 0.ToString().EncodeAsGuid(); ServerAddress = serverAddress; + ComputerName = computerName; } - public string ServerAddress { get; set; } + public string ServerAddress { get; set; } + public string ComputerName { get; set; } public bool IsActive { get; set; } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Factories/ServerRegistrationFactory.cs b/src/Umbraco.Core/Persistence/Factories/ServerRegistrationFactory.cs index 36759f4759..edeeff0fab 100644 --- a/src/Umbraco.Core/Persistence/Factories/ServerRegistrationFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/ServerRegistrationFactory.cs @@ -9,8 +9,8 @@ namespace Umbraco.Core.Persistence.Factories #region Implementation of IEntityFactory public ServerRegistration BuildEntity(ServerRegistrationDto dto) - { - return new ServerRegistration(dto.Id, dto.Address, dto.DateRegistered, dto.LastNotified); + { + return new ServerRegistration(dto.Id, dto.ComputerName, dto.Address, dto.DateRegistered, dto.LastNotified); } public ServerRegistrationDto BuildDto(ServerRegistration entity) @@ -20,7 +20,8 @@ namespace Umbraco.Core.Persistence.Factories Address = entity.ServerAddress, DateRegistered = entity.CreateDate, IsActive = entity.IsActive, - LastNotified = entity.UpdateDate + LastNotified = entity.UpdateDate, + ComputerName = entity.ComputerName }; if (entity.HasIdentity) dto.Id = short.Parse(entity.Id.ToString(CultureInfo.InvariantCulture)); diff --git a/src/Umbraco.Core/Persistence/Mappers/ServerRegistrationMapper.cs b/src/Umbraco.Core/Persistence/Mappers/ServerRegistrationMapper.cs index 14bac03138..ee8c7dbe8c 100644 --- a/src/Umbraco.Core/Persistence/Mappers/ServerRegistrationMapper.cs +++ b/src/Umbraco.Core/Persistence/Mappers/ServerRegistrationMapper.cs @@ -26,6 +26,7 @@ namespace Umbraco.Core.Persistence.Mappers CacheMap(src => src.ServerAddress, dto => dto.Address); CacheMap(src => src.CreateDate, dto => dto.DateRegistered); CacheMap(src => src.UpdateDate, dto => dto.LastNotified); + CacheMap(src => src.ComputerName, dto => dto.ComputerName); } internal override string Map(string propertyName) diff --git a/src/Umbraco.Core/Persistence/RepositoryFactory.cs b/src/Umbraco.Core/Persistence/RepositoryFactory.cs index 6b7b46ccc7..ab48efb013 100644 --- a/src/Umbraco.Core/Persistence/RepositoryFactory.cs +++ b/src/Umbraco.Core/Persistence/RepositoryFactory.cs @@ -9,6 +9,13 @@ namespace Umbraco.Core.Persistence /// public class RepositoryFactory { + internal virtual ServerRegistrationRepository CreateServerRegistrationRepository(IDatabaseUnitOfWork uow) + { + return new ServerRegistrationRepository( + uow, + NullCacheProvider.Current); + } + internal virtual IUserTypeRepository CreateUserTypeRepository(IDatabaseUnitOfWork uow) { return new UserTypeRepository( diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index 0fdb4b06c6..653d9159fd 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -18,7 +18,7 @@ using Umbraco.Core.Publishing; namespace Umbraco.Core.Services { - /// + /// /// Represents the Content Service, which is an easy access to operations involving /// public class ContentService : IContentService diff --git a/src/Umbraco.Core/Services/ServerRegistrationService.cs b/src/Umbraco.Core/Services/ServerRegistrationService.cs new file mode 100644 index 0000000000..380dfc2169 --- /dev/null +++ b/src/Umbraco.Core/Services/ServerRegistrationService.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Umbraco.Core.Models; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.Querying; +using Umbraco.Core.Persistence.UnitOfWork; + +namespace Umbraco.Core.Services +{ + /// + /// Service to manage server registrations in the database + /// + internal class ServerRegistrationService + { + private readonly RepositoryFactory _repositoryFactory; + private readonly IDatabaseUnitOfWorkProvider _uowProvider; + + public ServerRegistrationService() + : this(new RepositoryFactory()) + {} + + public ServerRegistrationService(RepositoryFactory repositoryFactory) + : this(new PetaPocoUnitOfWorkProvider(), repositoryFactory) + {} + + public ServerRegistrationService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory) + { + if (provider == null) throw new ArgumentNullException("provider"); + if (repositoryFactory == null) throw new ArgumentNullException("repositoryFactory"); + _uowProvider = provider; + _repositoryFactory = repositoryFactory; + } + + /// + /// Called to 'call home' to ensure the current server has an active record + /// + /// + public void EnsureActive(string address) + { + + var uow = _uowProvider.GetUnitOfWork(); + using (var repo = _repositoryFactory.CreateServerRegistrationRepository(uow)) + { + //NOTE: we cannot use Environment.MachineName as this does not work in medium trust + // found this out in CDF a while back: http://clientdependency.codeplex.com/workitem/13191 + + var computerName = System.Net.Dns.GetHostName(); + var query = Query.Builder.Where(x => x.ComputerName.ToUpper() == computerName.ToUpper()); + var found = repo.GetByQuery(query).ToArray(); + ServerRegistration server; + if (found.Any()) + { + server = found.First(); + server.ServerAddress = address; //This should not really change but it might! + server.UpdateDate = DateTime.UtcNow; //Stick with Utc dates since these might be globally distributed + server.IsActive = true; + } + else + { + server = new ServerRegistration(address, computerName, DateTime.UtcNow); + } + repo.AddOrUpdate(server); + uow.Commit(); + } + } + + /// + /// Deactivates a server by name + /// + /// + public void DeactiveServer(string computerName) + { + var uow = _uowProvider.GetUnitOfWork(); + using (var repo = _repositoryFactory.CreateServerRegistrationRepository(uow)) + { + var query = Query.Builder.Where(x => x.ComputerName.ToUpper() == computerName.ToUpper()); + var found = repo.GetByQuery(query).ToArray(); + if (found.Any()) + { + var server = found.First(); + server.IsActive = false; + repo.AddOrUpdate(server); + uow.Commit(); + } + } + } + + /// + /// Return all active servers + /// + /// + public IEnumerable GetActiveServers() + { + var uow = _uowProvider.GetUnitOfWork(); + using (var repo = _repositoryFactory.CreateServerRegistrationRepository(uow)) + { + var query = Query.Builder.Where(x => x.IsActive); + return repo.GetByQuery(query).ToArray(); + } + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index b8f4386b79..3b3754d4f7 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -673,6 +673,7 @@ + diff --git a/src/Umbraco.Tests/Persistence/Repositories/ServerRegistrationRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/ServerRegistrationRepositoryTest.cs index eabea4c0a4..ef1bda3c5c 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/ServerRegistrationRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/ServerRegistrationRepositoryTest.cs @@ -1,4 +1,5 @@ using System; +using System.Data.SqlServerCe; using System.Linq; using NUnit.Framework; using Umbraco.Core.Models; @@ -20,6 +21,42 @@ namespace Umbraco.Tests.Persistence.Repositories CreateTestData(); } + [Test] + public void Cannot_Add_Duplicate_Computer_Names() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); + + // Act + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + var server = new ServerRegistration("http://shazwazza.com", "COMPUTER1", DateTime.Now); + repository.AddOrUpdate(server); + + Assert.Throws(unitOfWork.Commit); + } + + } + + [Test] + public void Cannot_Update_To_Duplicate_Computer_Names() + { + // Arrange + var provider = new PetaPocoUnitOfWorkProvider(); + var unitOfWork = provider.GetUnitOfWork(); + + // Act + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + var server = repository.Get(1); + server.ComputerName = "COMPUTER2"; + repository.AddOrUpdate(server); + Assert.Throws(unitOfWork.Commit); + } + + } + [Test] public void Can_Instantiate_Repository() { @@ -28,10 +65,11 @@ namespace Umbraco.Tests.Persistence.Repositories var unitOfWork = provider.GetUnitOfWork(); // Act - var repository = new ServerRegistrationRepository(unitOfWork); - - // Assert - Assert.That(repository, Is.Not.Null); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Assert + Assert.That(repository, Is.Not.Null); + } } [Test] @@ -40,15 +78,18 @@ namespace Umbraco.Tests.Persistence.Repositories // Arrange var provider = new PetaPocoUnitOfWorkProvider(); var unitOfWork = provider.GetUnitOfWork(); - var repository = new ServerRegistrationRepository(unitOfWork); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var server = repository.Get(1); - // Act - var server = repository.Get(1); + // Assert + Assert.That(server, Is.Not.Null); + Assert.That(server.HasIdentity, Is.True); + Assert.That(server.ServerAddress, Is.EqualTo("http://localhost")); + } - // Assert - Assert.That(server, Is.Not.Null); - Assert.That(server.HasIdentity, Is.True); - Assert.That(server.ServerAddress, Is.EqualTo("http://localhost")); + } [Test] @@ -57,13 +98,15 @@ namespace Umbraco.Tests.Persistence.Repositories // Arrange var provider = new PetaPocoUnitOfWorkProvider(); var unitOfWork = provider.GetUnitOfWork(); - var repository = new ServerRegistrationRepository(unitOfWork); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var servers = repository.GetAll(); - // Act - var servers = repository.GetAll(); - - // Assert - Assert.That(servers.Count(), Is.EqualTo(3)); + // Assert + Assert.That(servers.Count(), Is.EqualTo(3)); + } + } [Test] @@ -72,14 +115,15 @@ namespace Umbraco.Tests.Persistence.Repositories // Arrange var provider = new PetaPocoUnitOfWorkProvider(); var unitOfWork = provider.GetUnitOfWork(); - var repository = new ServerRegistrationRepository(unitOfWork); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var query = Query.Builder.Where(x => x.ComputerName.ToUpper() == "COMPUTER3"); + var result = repository.GetByQuery(query); - // Act - var query = Query.Builder.Where(x => x.IsActive); - var result = repository.GetByQuery(query); - - // Assert - Assert.AreEqual(1, result.Count()); + // Assert + Assert.AreEqual(1, result.Count()); + } } [Test] @@ -88,14 +132,15 @@ namespace Umbraco.Tests.Persistence.Repositories // Arrange var provider = new PetaPocoUnitOfWorkProvider(); var unitOfWork = provider.GetUnitOfWork(); - var repository = new ServerRegistrationRepository(unitOfWork); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var query = Query.Builder.Where(x => x.ServerAddress.StartsWith("http://")); + int count = repository.Count(query); - // Act - var query = Query.Builder.Where(x => x.ServerAddress.StartsWith("http://")); - int count = repository.Count(query); - - // Assert - Assert.That(count, Is.EqualTo(2)); + // Assert + Assert.That(count, Is.EqualTo(2)); + } } [Test] @@ -104,16 +149,17 @@ namespace Umbraco.Tests.Persistence.Repositories // Arrange var provider = new PetaPocoUnitOfWorkProvider(); var unitOfWork = provider.GetUnitOfWork(); - var repository = new ServerRegistrationRepository(unitOfWork); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var server = new ServerRegistration("http://shazwazza.com", "COMPUTER4", DateTime.Now); + repository.AddOrUpdate(server); + unitOfWork.Commit(); - // Act - var server = new ServerRegistration("http://shazwazza.com", DateTime.Now); - repository.AddOrUpdate(server); - unitOfWork.Commit(); - - // Assert - Assert.That(server.HasIdentity, Is.True); - Assert.That(server.Id, Is.EqualTo(4));//With 3 existing entries the Id should be 4 + // Assert + Assert.That(server.HasIdentity, Is.True); + Assert.That(server.Id, Is.EqualTo(4));//With 3 existing entries the Id should be 4 + } } [Test] @@ -122,22 +168,23 @@ namespace Umbraco.Tests.Persistence.Repositories // Arrange var provider = new PetaPocoUnitOfWorkProvider(); var unitOfWork = provider.GetUnitOfWork(); - var repository = new ServerRegistrationRepository(unitOfWork); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var server = repository.Get(2); + server.ServerAddress = "https://umbraco.com"; + server.IsActive = true; - // Act - var server = repository.Get(2); - server.ServerAddress = "https://umbraco.com"; - server.IsActive = true; + repository.AddOrUpdate(server); + unitOfWork.Commit(); - repository.AddOrUpdate(server); - unitOfWork.Commit(); + var serverUpdated = repository.Get(2); - var serverUpdated = repository.Get(2); - - // Assert - Assert.That(serverUpdated, Is.Not.Null); - Assert.That(serverUpdated.ServerAddress, Is.EqualTo("https://umbraco.com")); - Assert.That(serverUpdated.IsActive, Is.EqualTo(true)); + // Assert + Assert.That(serverUpdated, Is.Not.Null); + Assert.That(serverUpdated.ServerAddress, Is.EqualTo("https://umbraco.com")); + Assert.That(serverUpdated.IsActive, Is.EqualTo(true)); + } } [Test] @@ -146,18 +193,19 @@ namespace Umbraco.Tests.Persistence.Repositories // Arrange var provider = new PetaPocoUnitOfWorkProvider(); var unitOfWork = provider.GetUnitOfWork(); - var repository = new ServerRegistrationRepository(unitOfWork); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var server = repository.Get(3); + Assert.IsNotNull(server); + repository.Delete(server); + unitOfWork.Commit(); - // Act - var server = repository.Get(3); - Assert.IsNotNull(server); - repository.Delete(server); - unitOfWork.Commit(); + var exists = repository.Exists(3); - var exists = repository.Exists(3); - - // Assert - Assert.That(exists, Is.False); + // Assert + Assert.That(exists, Is.False); + } } [Test] @@ -166,15 +214,16 @@ namespace Umbraco.Tests.Persistence.Repositories // Arrange var provider = new PetaPocoUnitOfWorkProvider(); var unitOfWork = provider.GetUnitOfWork(); - var repository = new ServerRegistrationRepository(unitOfWork); + using (var repository = new ServerRegistrationRepository(unitOfWork)) + { + // Act + var exists = repository.Exists(3); + var doesntExist = repository.Exists(10); - // Act - var exists = repository.Exists(3); - var doesntExist = repository.Exists(10); - - // Assert - Assert.That(exists, Is.True); - Assert.That(doesntExist, Is.False); + // Assert + Assert.That(exists, Is.True); + Assert.That(doesntExist, Is.False); + } } [TearDown] @@ -185,13 +234,13 @@ namespace Umbraco.Tests.Persistence.Repositories public void CreateTestData() { - var provider = new PetaPocoUnitOfWorkProvider(); - using(var unitOfWork = provider.GetUnitOfWork()) + var provider = new PetaPocoUnitOfWorkProvider(); + using (var unitOfWork = provider.GetUnitOfWork()) using (var repository = new ServerRegistrationRepository(unitOfWork)) { - repository.AddOrUpdate(new ServerRegistration("http://localhost", DateTime.Now) {IsActive = true}); - repository.AddOrUpdate(new ServerRegistration("http://www.mydomain.com", DateTime.Now)); - repository.AddOrUpdate(new ServerRegistration("https://www.another.domain.com", DateTime.Now)); + repository.AddOrUpdate(new ServerRegistration("http://localhost", "COMPUTER1", DateTime.Now) { IsActive = true }); + repository.AddOrUpdate(new ServerRegistration("http://www.mydomain.com", "COMPUTER2", DateTime.Now)); + repository.AddOrUpdate(new ServerRegistration("https://www.another.domain.com", "Computer3", DateTime.Now)); unitOfWork.Commit(); }