Skip to content

Commit

Permalink
#785 | Handle multiple address levels with the same title of the same…
Browse files Browse the repository at this point in the history
… type while fetching by address map
  • Loading branch information
1t5j0y committed Oct 28, 2024
1 parent 1c89414 commit eb8b76b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<AddressLevel> findByTitleIgnoreCaseAndTypeNameIgnoreCaseAndIsVoidedFalse(String title, String type);

default AddressLevel findLocation(String title, String type) {
default List<AddressLevel> findLocation(String title, String type) {
return this.findByTitleIgnoreCaseAndTypeNameIgnoreCaseAndIsVoidedFalse(title, type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -146,19 +147,24 @@ public Optional<AddressLevel> findByAddressMap(Map<String, String> addressMap) {
if (addressMap == null || addressMap.isEmpty()) {
return Optional.empty();
}
List<AddressLevel> 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<AddressLevel> 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<Long> addressLevelIds = fetchedAddressLevels.stream().map(AddressLevel::getId).collect(Collectors.toList());
List<AddressLevel> 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<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> addressLevelMap = new HashMap<>();

Expand Down Expand Up @@ -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());
}
}

0 comments on commit eb8b76b

Please sign in to comment.