From 06ad1415fb40201a17c7cc9d6f0474b809edc868 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 14 Dec 2025 15:45:23 +0100 Subject: [PATCH] Bugfix: was mixing up user node and non-user node in RoutingService --- .../lcc/repositories/NodeRepository.java | 23 +++++++++++++++++++ .../service/calculation/RoutingService.java | 13 +++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/avatic/lcc/repositories/NodeRepository.java b/src/main/java/de/avatic/lcc/repositories/NodeRepository.java index 88f9e0b..7274846 100644 --- a/src/main/java/de/avatic/lcc/repositories/NodeRepository.java +++ b/src/main/java/de/avatic/lcc/repositories/NodeRepository.java @@ -5,6 +5,7 @@ import de.avatic.lcc.model.db.ValidityTuple; import de.avatic.lcc.model.db.nodes.Node; import de.avatic.lcc.repositories.pagination.SearchQueryPagination; import de.avatic.lcc.repositories.pagination.SearchQueryResult; +import de.avatic.lcc.util.exception.internalerror.DatabaseException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; @@ -165,6 +166,9 @@ public class NodeRepository { return Optional.empty(); } + if(node.isUserNode()) + throw new DatabaseException("Cannot update user node in node repository."); + String updateNodeSql = """ UPDATE node SET country_id = ?, @@ -389,6 +393,25 @@ public class NodeRepository { @Transactional public List getByDistance(Node node, Integer regionRadius) { + if(node.isUserNode()) { + String query = """ + SELECT * FROM node + WHERE is_deprecated = FALSE AND + ( + 6371 * acos( + cos(radians(?)) * + cos(radians(geo_lat)) * + cos(radians(geo_lng) - radians(?)) + + sin(radians(?)) * + sin(radians(geo_lat)) + ) + ) <= ? + """; + + return jdbcTemplate.query(query, new NodeMapper(), node.getGeoLat(), node.getGeoLng(), node.getGeoLat(), regionRadius); + } + + String query = """ SELECT * FROM node WHERE is_deprecated = FALSE AND id != ? AND diff --git a/src/main/java/de/avatic/lcc/service/calculation/RoutingService.java b/src/main/java/de/avatic/lcc/service/calculation/RoutingService.java index a23d92c..c406f2d 100644 --- a/src/main/java/de/avatic/lcc/service/calculation/RoutingService.java +++ b/src/main/java/de/avatic/lcc/service/calculation/RoutingService.java @@ -431,7 +431,7 @@ public class RoutingService { TemporaryRateObject finalSection = null; SourceConnectionType connectionType = SourceConnectionType.NONE; - if (source.getId().equals(chain.getLast().getId())) { + if (!source.isUserNode() && source.getId().equals(chain.getLast().getId())) { connectionType = SourceConnectionType.CHAIN_END_IS_SOURCE_NODE; } else if (nearByNodes != null) { nearByNode = nearByNodes.stream().filter(n -> n.getId().equals(chain.getLast().getId())).findFirst().orElse(null); @@ -442,7 +442,7 @@ public class RoutingService { if (connectionType == SourceConnectionType.NONE) { // find final section: check if chain end and source node are identical, then check if chain end can be connected to // source node, if this is not possible use a near-by node - finalSection = connectNodes(source, chain.getLast(), container); + finalSection = connectNodes(source, chain.getLast(), container); if (finalSection != null) connectionType = SourceConnectionType.FINAL_SECTION_WITH_SOURCE_NODE; } @@ -690,7 +690,7 @@ public class RoutingService { if (container.getRates().contains(matrixRateObj)) return container.getRates().stream().filter(r -> r.equals(matrixRateObj)).findFirst().orElseThrow(); - Optional containerRate = containerRateRepository.findRoute(startNode.getId(), endNode.getId(), TransportType.ROAD); + Optional containerRate = startNode.isUserNode() ? Optional.empty() : containerRateRepository.findRoute(startNode.getId(), endNode.getId(), TransportType.ROAD); if (containerRate.isPresent()) { containerRateObj.setRate(containerRate.get()); @@ -987,7 +987,10 @@ public class RoutingService { if (this.type.equals(TemporaryRateObjectType.MATRIX)) { return Objects.equals(this.fromNode.getCountryId(), that.fromNode.getCountryId()) && Objects.equals(this.toNode.getCountryId(), that.toNode.getCountryId()); } else if (this.type.equals(TemporaryRateObjectType.CONTAINER) || this.type.equals(TemporaryRateObjectType.MAIN_RUN) || this.type.equals(TemporaryRateObjectType.POST_RUN) || this.type.equals(TemporaryRateObjectType.NEAR_BY)) { - return Objects.equals(this.fromNode.getId(), that.fromNode.getId()) && Objects.equals(this.toNode.getId(), that.toNode.getId()); + return Objects.equals(this.fromNode.getId(), that.fromNode.getId()) + && Objects.equals(this.toNode.getId(), that.toNode.getId()) + && Objects.equals(this.fromNode.isUserNode(), that.fromNode.isUserNode()) + && Objects.equals(this.toNode.isUserNode(), that.toNode.isUserNode()); } } @@ -1000,7 +1003,7 @@ public class RoutingService { if (containerRate != null) return Objects.hash(containerRate.getFromNodeId(), containerRate.getToNodeId()); - return Objects.hash(null, null); + return Objects.hash(fromNode.getId(), toNode.getId(), fromNode.isUserNode(), toNode.isUserNode()); } public void setRate(ContainerRate containerRate) {