From 38edb47628ec93d6fca80892462b8d00bff0151f Mon Sep 17 00:00:00 2001 From: Rune Nielsen Date: Thu, 11 May 2023 14:06:04 +0200 Subject: [PATCH] now checks shadow table if route element was created by gdb-integrator (#110) --- .../IGeoDatabase.cs | 2 ++ .../Postgres/Postgis.cs | 14 ++++++++++++++ .../Factories/RouteNodeCommandFactory.cs | 8 ++++---- .../Factories/RouteSegmentCommandFactory.cs | 10 ++++------ .../Factories/RouteNodeCommandFactoryTest.cs | 7 ++++--- .../Factories/RouteSegmentCommandFactoryTest.cs | 6 +++--- 6 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/OpenFTTH.GDBIntegrator.GeoDatabase/IGeoDatabase.cs b/src/OpenFTTH.GDBIntegrator.GeoDatabase/IGeoDatabase.cs index eaed11e..b2ec26e 100644 --- a/src/OpenFTTH.GDBIntegrator.GeoDatabase/IGeoDatabase.cs +++ b/src/OpenFTTH.GDBIntegrator.GeoDatabase/IGeoDatabase.cs @@ -7,7 +7,9 @@ namespace OpenFTTH.GDBIntegrator.GeoDatabase { public interface IGeoDatabase { + Task RouteNodeInShadowTableExists(Guid mrid); Task GetRouteNodeShadowTable(Guid mrid, bool includeDeleted = false); + Task RouteSegmentInShadowTableExists(Guid mrid); Task GetRouteSegmentShadowTable(Guid mrid, bool includeDeleted = false); Task> GetIntersectingStartRouteNodes(RouteSegment routeSegment); Task> GetIntersectingStartRouteNodes(byte[] coord); diff --git a/src/OpenFTTH.GDBIntegrator.GeoDatabase/Postgres/Postgis.cs b/src/OpenFTTH.GDBIntegrator.GeoDatabase/Postgres/Postgis.cs index b9d6a4f..319bf6d 100644 --- a/src/OpenFTTH.GDBIntegrator.GeoDatabase/Postgres/Postgis.cs +++ b/src/OpenFTTH.GDBIntegrator.GeoDatabase/Postgres/Postgis.cs @@ -67,6 +67,13 @@ public async Task RollbackTransaction() await DisposeConnection(); } + public async Task RouteNodeInShadowTableExists(Guid mrid) + { + var connection = GetNpgsqlConnection(); + var query = @"SELECT exists(SELECT 1 FROM route_network_integrator.route_node WHERE mrid = @mrid)"; + return await connection.QueryFirstAsync(query, new { mrid }); + } + public async Task GetRouteNodeShadowTable(Guid mrid, bool includeDeleted = false) { var connection = GetNpgsqlConnection(); @@ -146,6 +153,13 @@ naming_description AS namingDescription return routeNodes.FirstOrDefault(); } + public async Task RouteSegmentInShadowTableExists(Guid mrid) + { + var connection = GetNpgsqlConnection(); + var query = @"SELECT exists(SELECT 1 FROM route_network_integrator.route_segment WHERE mrid = @mrid)"; + return await connection.QueryFirstAsync(query, new { mrid }); + } + public async Task GetRouteSegmentShadowTable(Guid mrid, bool includeDeleted = false) { var connection = GetNpgsqlConnection(); diff --git a/src/OpenFTTH.GDBIntegrator.Integrator/Factories/RouteNodeCommandFactory.cs b/src/OpenFTTH.GDBIntegrator.Integrator/Factories/RouteNodeCommandFactory.cs index e944bb2..1994bde 100644 --- a/src/OpenFTTH.GDBIntegrator.Integrator/Factories/RouteNodeCommandFactory.cs +++ b/src/OpenFTTH.GDBIntegrator.Integrator/Factories/RouteNodeCommandFactory.cs @@ -74,8 +74,11 @@ public async Task> CreateDigitizedEvent(RouteNode routeNode) if (routeNode is null) throw new ArgumentNullException($"Parameter {nameof(routeNode)} cannot be null"); - if (IsCreatedByApplication(routeNode)) + // If we find the route node is in the shadow table, it means that it was created by the application and we therefore do nothing. + if (await _geoDatabase.RouteNodeInShadowTableExists(routeNode.Mrid)) + { return new List { new DoNothing($"{nameof(RouteNode)} with id: '{routeNode.Mrid}' was created by {routeNode.ApplicationName} therefore do nothing.") }; + } await _geoDatabase.InsertRouteNodeShadowTable(routeNode); @@ -121,9 +124,6 @@ private async Task IsValidNodeUpdate(RouteNode shadowTableNode, RouteNode private bool AlreadyUpdated(RouteNode routeNode, RouteNode routeNodeShadowTable) => routeNode.MarkAsDeleted == routeNodeShadowTable.MarkAsDeleted && routeNode.GetGeoJsonCoordinate() == routeNodeShadowTable.GetGeoJsonCoordinate(); - private bool IsCreatedByApplication(RouteNode routeNode) - => routeNode.ApplicationName == _applicationSettings.ApplicationName; - private bool IsModifiedDistanceLessThanTolerance(RouteNode shadowTableNode, RouteNode after) { var distance = after.GetPoint().Distance(shadowTableNode.GetPoint()); diff --git a/src/OpenFTTH.GDBIntegrator.Integrator/Factories/RouteSegmentCommandFactory.cs b/src/OpenFTTH.GDBIntegrator.Integrator/Factories/RouteSegmentCommandFactory.cs index f3181c6..e8e14b4 100644 --- a/src/OpenFTTH.GDBIntegrator.Integrator/Factories/RouteSegmentCommandFactory.cs +++ b/src/OpenFTTH.GDBIntegrator.Integrator/Factories/RouteSegmentCommandFactory.cs @@ -107,8 +107,11 @@ public async Task> CreateDigitizedEvent(RouteSegment if (routeSegment is null) throw new ArgumentNullException($"Parameter {nameof(routeSegment)} must not be null"); - if (IsCreatedByApplication(routeSegment)) + // If we find the route segment is in the shadow table, it means that it was created by the application and we therefore do nothing. + if (await _geoDatabase.RouteSegmentInShadowTableExists(routeSegment.Mrid)) + { return new List(); + } // Update integrator "shadow table" with the used digitized segment await _geoDatabase.InsertRouteSegmentShadowTable(routeSegment); @@ -164,11 +167,6 @@ private bool AlreadyUpdated(RouteSegment routeSegment, RouteSegment shadowTableR return routeSegment.MarkAsDeleted == shadowTableRouteSegment.MarkAsDeleted && routeSegment.GetGeoJsonCoordinate() == shadowTableRouteSegment.GetGeoJsonCoordinate(); } - private bool IsCreatedByApplication(RouteSegment routeSegment) - { - return routeSegment.ApplicationName == _applicationSettings.ApplicationName; - } - private RouteSegmentDeleted CreateRouteSegmentDeleted(RouteSegment routeSegment) { return new RouteSegmentDeleted diff --git a/test/OpenFTTH.GDBIntegrator.Integrator.Tests/Factories/RouteNodeCommandFactoryTest.cs b/test/OpenFTTH.GDBIntegrator.Integrator.Tests/Factories/RouteNodeCommandFactoryTest.cs index 7ccdd3f..eca6ce1 100644 --- a/test/OpenFTTH.GDBIntegrator.Integrator.Tests/Factories/RouteNodeCommandFactoryTest.cs +++ b/test/OpenFTTH.GDBIntegrator.Integrator.Tests/Factories/RouteNodeCommandFactoryTest.cs @@ -39,13 +39,14 @@ public async Task CreateDigitizedEvent_ShouldReturnDoNothing_OnRouteNodeApplicat var applicationSetting = A.Fake>(); var geoDatabase = A.Fake(); var routeNodeValidator = A.Fake(); + var routeNodeMrid = Guid.NewGuid(); - A.CallTo(() => applicationSetting.Value).Returns( - new ApplicationSetting { ApplicationName = "GDB_INTEGRATOR" }); + A.CallTo(() => geoDatabase.RouteNodeInShadowTableExists(routeNodeMrid)) + .Returns(true); var factory = new RouteNodeCommandFactory(applicationSetting, geoDatabase, routeNodeValidator); - var routeNode = new RouteNode(Guid.Empty, null, Guid.Empty, String.Empty, "GDB_INTEGRATOR"); + var routeNode = new RouteNode(routeNodeMrid, null, Guid.Empty, String.Empty, "GDB_INTEGRATOR"); var result = (DoNothing)((await factory.CreateDigitizedEvent(routeNode)).First()); result.Should().BeOfType(); diff --git a/test/OpenFTTH.GDBIntegrator.Integrator.Tests/Factories/RouteSegmentCommandFactoryTest.cs b/test/OpenFTTH.GDBIntegrator.Integrator.Tests/Factories/RouteSegmentCommandFactoryTest.cs index b5cc345..bd7e349 100644 --- a/test/OpenFTTH.GDBIntegrator.Integrator.Tests/Factories/RouteSegmentCommandFactoryTest.cs +++ b/test/OpenFTTH.GDBIntegrator.Integrator.Tests/Factories/RouteSegmentCommandFactoryTest.cs @@ -43,10 +43,10 @@ public async Task CreateDigitizedEvent_ShouldReturnNull_OnRouteSegmentApplicatio var routeSegmentValidator = A.Fake(); var geoDatabase = A.Fake(); var routeNodeFactory = A.Fake(); - var routeSegment = new RouteSegment { ApplicationName = "GDB_INTEGRATOR" }; + var routeSegment = new RouteSegment { Mrid = Guid.NewGuid(), ApplicationName = "GDB_INTEGRATOR" }; - A.CallTo(() => applicationSettings.Value) - .Returns(new ApplicationSetting { ApplicationName = "GDB_INTEGRATOR" }); + A.CallTo(() => geoDatabase.RouteSegmentInShadowTableExists(routeSegment.Mrid)) + .Returns(true); var factory = new RouteSegmentCommandFactory(applicationSettings, routeSegmentValidator, geoDatabase, routeNodeFactory);