Skip to content

Commit

Permalink
avni-webapp#613 | Give correct error when updating location.
Browse files Browse the repository at this point in the history
- This error is thrown if location with same name and parent already exists.
  • Loading branch information
hithacker committed Jun 21, 2021
1 parent 302ab5b commit 0fa7090
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ public boolean containsSubLocation(String title, AddressLevelType type) {
.orElse(null);
}

public boolean containsSubLocationExcept(String title, AddressLevelType type, AddressLevel exclude) {
return null !=
subLocations
.stream()
.filter(location -> !location.getId().equals(exclude.getId()))
.filter(location -> location.getTitle().equals(title) &&
location.getType().equals(type)
).findFirst()
.orElse(null);
}

public Set<Catchment> getCatchments() {
return catchments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ public AddressLevel update(LocationEditContract locationEditContract, Long id) {
throw new RuntimeException("Empty 'title' received");
}

if (location.isTopLevel() && locationRepository.findByTitleIgnoreCaseAndTypeAndParentIsNull(locationEditContract.getTitle().trim(), location.getType()) != null) {
throw new RuntimeException("Location with same name '%s' and type '%s' already exists");
} else {
AddressLevel parent = locationRepository.findOne(locationEditContract.getParentId());
if (!titleIsValid(location, parent, locationEditContract.getTitle().trim(), location.getType())) {
String message = String.format("Location with same name '%s' and type '%s' and parent '%s' already exists",
locationEditContract.getTitle(),
location.getType().getName(),
parent.getTitle());
throw new RuntimeException(message);
}
}

if (locationEditContract.getParentId() != null && !locationEditContract.getParentId().equals(location.getParentId())) {
Long oldParentId = location.getParentId();
Long newParentId = locationEditContract.getParentId();
Expand All @@ -160,12 +173,7 @@ public AddressLevel update(LocationEditContract locationEditContract, Long id) {
location.setLineage(updateLineage(lineage, oldParentId, newParentId));
location.setParent(locationRepository.findOne(newParentId));
}
if (!titleIsValid(location, locationEditContract.getTitle().trim(), location.getType())) {
String message = String.format("Location with same name '%s' and type '%s' exists at this level",
locationEditContract.getTitle(),
location.getType().getName());
throw new RuntimeException(message);
}

location.setTitle(locationEditContract.getTitle());
locationRepository.save(location);
return location;
Expand Down Expand Up @@ -205,6 +213,11 @@ private boolean titleIsValid(AddressLevel location, String title, AddressLevelTy
|| (!location.isTopLevel() && !location.getParent().containsSubLocation(title, type));
}

private boolean titleIsValid(AddressLevel location, AddressLevel parent, String title, AddressLevelType type) {
return (location.isTopLevel() && locationRepository.findByTitleIgnoreCaseAndTypeAndParentIsNull(title, type) == null)
|| (!location.isTopLevel() && !parent.containsSubLocationExcept(title, type, location));
}

public AddressLevelType createAddressLevelType(AddressLevelTypeContract contract) {
AddressLevelType addressLevelType = addressLevelTypeRepository.findByUuid(contract.getUuid());
if (addressLevelType == null) {
Expand Down

0 comments on commit 0fa7090

Please sign in to comment.