diff --git a/avni-server-api/src/main/java/org/avni/server/dao/LocationRepository.java b/avni-server-api/src/main/java/org/avni/server/dao/LocationRepository.java index c72b2087e..45bbe7b57 100644 --- a/avni-server-api/src/main/java/org/avni/server/dao/LocationRepository.java +++ b/avni-server-api/src/main/java/org/avni/server/dao/LocationRepository.java @@ -150,9 +150,9 @@ default AddressLevel findChildLocation(String title, String type, String parentN return this.findLocationByTitleTypeAndParentName(title, type, parentName); } - AddressLevel findByTitleIgnoreCaseAndTypeNameIgnoreCaseAndIsVoidedFalse(String title, String type); + List findByTitleIgnoreCaseAndTypeNameIgnoreCaseAndIsVoidedFalse(String title, String type); - default AddressLevel findLocation(String title, String type) { + default List findLocation(String title, String type) { return this.findByTitleIgnoreCaseAndTypeNameIgnoreCaseAndIsVoidedFalse(title, type); } diff --git a/avni-server-api/src/main/java/org/avni/server/service/AddressLevelService.java b/avni-server-api/src/main/java/org/avni/server/service/AddressLevelService.java index fbfd59d40..18adc857f 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/AddressLevelService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/AddressLevelService.java @@ -15,6 +15,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; import java.util.*; import java.util.stream.Collectors; @@ -146,19 +147,24 @@ public Optional findByAddressMap(Map addressMap) { if (addressMap == null || addressMap.isEmpty()) { return Optional.empty(); } - List addressLevels = addressMap.entrySet().stream().map(entry -> locationRepository.findLocation(entry.getValue(), entry.getKey())) - .filter(Objects::nonNull).sorted(Comparator.comparing(al -> al.getLineage().length())) - .collect(Collectors.toList()); + Set fetchedAddressLevels = addressMap.entrySet().stream().map(entry -> locationRepository.findLocation(entry.getValue(), entry.getKey())) + .flatMap(Collection::stream) + .filter(Objects::nonNull) + .sorted(Comparator.comparing(al -> StringUtils.countOccurrencesOf(al.getLineage(), "."))) + .collect(Collectors.toCollection(LinkedHashSet::new)); - if (addressLevels.isEmpty() || addressLevels.size() != addressMap.size()) { - return Optional.empty(); - } + if (fetchedAddressLevels.isEmpty()) return Optional.empty(); + + List addressLevelIds = fetchedAddressLevels.stream().map(AddressLevel::getId).collect(Collectors.toList()); + List addressLevels = fetchedAddressLevels.stream().filter(al -> al.getParentId() == null || addressLevelIds.contains(al.getParentId())).collect(Collectors.toList()); + + if (addressLevels.size() != addressMap.size()) return Optional.empty(); String typeHierarchyForAddressMap = addressLevels.stream().map(al -> String.valueOf(al.getTypeId())).collect(Collectors.joining(".")); String lineageForAddressMap = addressLevels.stream().map(al -> String.valueOf(al.getId())).collect(Collectors.joining(".")); TreeSet addressLevelTypeHierarchies = locationHierarchyService.fetchAndFilterHierarchies(); - if (addressLevelTypeHierarchies.stream().anyMatch(hierarchy -> hierarchy.contains(typeHierarchyForAddressMap))) { + if (addressLevelTypeHierarchies.stream().anyMatch(hierarchy -> hierarchy.equals(typeHierarchyForAddressMap))) { AddressLevel matchedAddressLevel = addressLevels.get(addressLevels.size() - 1); if (matchedAddressLevel.getLineage().equals(lineageForAddressMap)) { return Optional.of(matchedAddressLevel); diff --git a/avni-server-api/src/test/java/org/avni/server/service/AddressLevelServiceIntegrationTest.java b/avni-server-api/src/test/java/org/avni/server/service/AddressLevelServiceIntegrationTest.java index a58f86dd4..972194dc0 100644 --- a/avni-server-api/src/test/java/org/avni/server/service/AddressLevelServiceIntegrationTest.java +++ b/avni-server-api/src/test/java/org/avni/server/service/AddressLevelServiceIntegrationTest.java @@ -61,9 +61,9 @@ public void findByAddressMap() { AddressLevel gp2AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("gp2").title("gp2").type(grandParent).build()); AddressLevel parent2AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("parent2").title("parent2").type(parent).parent(gp2AddressLevel).build()); AddressLevel child2AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("child2").title("child2").type(child).parent(parent2AddressLevel).build()); -// AddressLevel gp3AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("gp3").title("gp3").type(grandParent).build()); -// AddressLevel parent3AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("parent3").title("parent2").type(parent).parent(gp3AddressLevel).build()); -// AddressLevel child3AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("child3").title("child1").type(child).parent(parent3AddressLevel).build()); + AddressLevel gp3AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("gp3").title("gp3").type(grandParent).build()); + AddressLevel parent3AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("parent3").title("parent2").type(parent).parent(gp3AddressLevel).build()); + AddressLevel child3AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("child3").title("child1").type(child).parent(parent3AddressLevel).build()); Map addressLevelMap = new HashMap<>(); @@ -91,12 +91,13 @@ public void findByAddressMap() { addressLevelMap.remove(grandParent.getName()); assertEquals(Optional.empty(), addressLevelService.findByAddressMap(addressLevelMap)); -// //valid locations in valid hierarchy and related, but with duplicate title -// addressLevelMap.put(grandParent.getName(), gp3AddressLevel.getTitle()); -// assertEquals(Optional.empty(), addressLevelService.findByAddressMap(addressLevelMap)); -// addressLevelMap.put(parent.getName(), parent3AddressLevel.getTitle()); -// assertEquals(Optional.empty(), addressLevelService.findByAddressMap(addressLevelMap)); -// addressLevelMap.put(child.getName(), child3AddressLevel.getTitle()); -// assertEquals(child3AddressLevel, addressLevelService.findByAddressMap(addressLevelMap).get()); + //valid locations in valid hierarchy and related, but with duplicate title + addressLevelMap.put(grandParent.getName(), gp3AddressLevel.getTitle()); + assertEquals(Optional.empty(), addressLevelService.findByAddressMap(addressLevelMap)); + addressLevelMap.remove(parent.getName().toUpperCase()); + addressLevelMap.put(parent.getName(), parent3AddressLevel.getTitle()); + assertEquals(Optional.empty(), addressLevelService.findByAddressMap(addressLevelMap)); + addressLevelMap.put(child.getName(), child3AddressLevel.getTitle()); + assertEquals(child3AddressLevel, addressLevelService.findByAddressMap(addressLevelMap).get()); } }