Skip to content

Commit

Permalink
avniproject/avni-product#1531 | Introduce new endpoint "/web/location…
Browse files Browse the repository at this point in the history
…Hierarchies"
  • Loading branch information
himeshr committed May 31, 2024
1 parent bde033b commit 4dd8316
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ public TreeSet<String> determineAddressHierarchiesToBeSaved(JsonObject organisat
return filterHierarchiesWithCommonAncestries(addressLevelTypeHierarchies);
}

public HashMap<String, String> determineAddressHierarchiesForAllAddressLevelTypesInOrg() {
HashMap<String, String> hierarchyToDisplayNameMap = new HashMap<>();
List<AddressLevelType> addressLevelTypes = addressLevelTypeRepository.findAllByIsVoidedFalse();
TreeSet<String> addressLevelTypeHierarchies = buildHierarchyForAddressLevelTypes(addressLevelTypes);
TreeSet<String> filteredAddressLevelTypeHierarchies = filterHierarchiesWithCommonAncestries(addressLevelTypeHierarchies);
filteredAddressLevelTypeHierarchies.forEach(key -> {
hierarchyToDisplayNameMap.put(key, Arrays.stream(key.split("\\."))
.map(altId -> addressLevelTypeRepository.findOne(Long.parseLong(altId)).getName())
.collect(Collectors.joining(" -> ")));
});
return hierarchyToDisplayNameMap;
}

public TreeSet<String> buildHierarchyForAddressLevelTypes(List<AddressLevelType> addressLevelTypes) {
TreeSet<String> addressLevelTypeHierarchies = new TreeSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.UUID;
import java.util.*;

import static java.lang.String.format;
import static org.avni.server.util.AvniFiles.*;
Expand All @@ -54,6 +51,7 @@ public class ImportController {
private final FormElementRepository formElementRepository;
private final AccessControlService accessControlService;
private final ErrorBodyBuilder errorBodyBuilder;
private final LocationHierarchyService locationHierarchyService;

@Autowired
public ImportController(JobService jobService,
Expand All @@ -62,7 +60,7 @@ public ImportController(JobService jobService,
S3Service s3Service,
IndividualService individualService,
LocationService locationService,
FormElementRepository formElementRepository, AccessControlService accessControlService, ErrorBodyBuilder errorBodyBuilder) {
FormElementRepository formElementRepository, AccessControlService accessControlService, ErrorBodyBuilder errorBodyBuilder, LocationHierarchyService locationHierarchyService) {
this.jobService = jobService;
this.bulkUploadS3Service = bulkUploadS3Service;
this.importService = importService;
Expand All @@ -72,6 +70,7 @@ public ImportController(JobService jobService,
this.formElementRepository = formElementRepository;
this.accessControlService = accessControlService;
this.errorBodyBuilder = errorBodyBuilder;
this.locationHierarchyService = locationHierarchyService;
logger = LoggerFactory.getLogger(getClass());
}

Expand Down Expand Up @@ -166,4 +165,15 @@ public JsonObject getSubjectOrLocationObsValue(@RequestParam("type") String type
}
return response;
}

@GetMapping(value = "/web/locationHierarchies")
@ResponseBody
public HashMap<String, String> getAllAddressLevelTypeHierarchies() {
try {
return locationHierarchyService.determineAddressHierarchiesForAllAddressLevelTypesInOrg();
} catch (Exception exception) {
logger.error("Error getting web locationHierarchies", exception);
return null;
}
}
}

0 comments on commit 4dd8316

Please sign in to comment.