From 4dd8316f4ce67fc82a2ba307d65e449c66198332 Mon Sep 17 00:00:00 2001 From: himeshr Date: Fri, 31 May 2024 12:34:55 +0530 Subject: [PATCH] avniproject/avni-product#1531 | Introduce new endpoint "/web/locationHierarchies" --- .../service/LocationHierarchyService.java | 13 ++++++++++++ .../org/avni/server/web/ImportController.java | 20 ++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/avni-server-api/src/main/java/org/avni/server/service/LocationHierarchyService.java b/avni-server-api/src/main/java/org/avni/server/service/LocationHierarchyService.java index bd519f17f..bfb51aadb 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/LocationHierarchyService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/LocationHierarchyService.java @@ -71,6 +71,19 @@ public TreeSet determineAddressHierarchiesToBeSaved(JsonObject organisat return filterHierarchiesWithCommonAncestries(addressLevelTypeHierarchies); } + public HashMap determineAddressHierarchiesForAllAddressLevelTypesInOrg() { + HashMap hierarchyToDisplayNameMap = new HashMap<>(); + List addressLevelTypes = addressLevelTypeRepository.findAllByIsVoidedFalse(); + TreeSet addressLevelTypeHierarchies = buildHierarchyForAddressLevelTypes(addressLevelTypes); + TreeSet 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 buildHierarchyForAddressLevelTypes(List addressLevelTypes) { TreeSet addressLevelTypeHierarchies = new TreeSet<>(); diff --git a/avni-server-api/src/main/java/org/avni/server/web/ImportController.java b/avni-server-api/src/main/java/org/avni/server/web/ImportController.java index 67755d1f8..333e22b1f 100644 --- a/avni-server-api/src/main/java/org/avni/server/web/ImportController.java +++ b/avni-server-api/src/main/java/org/avni/server/web/ImportController.java @@ -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.*; @@ -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, @@ -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; @@ -72,6 +70,7 @@ public ImportController(JobService jobService, this.formElementRepository = formElementRepository; this.accessControlService = accessControlService; this.errorBodyBuilder = errorBodyBuilder; + this.locationHierarchyService = locationHierarchyService; logger = LoggerFactory.getLogger(getClass()); } @@ -166,4 +165,15 @@ public JsonObject getSubjectOrLocationObsValue(@RequestParam("type") String type } return response; } + + @GetMapping(value = "/web/locationHierarchies") + @ResponseBody + public HashMap getAllAddressLevelTypeHierarchies() { + try { + return locationHierarchyService.determineAddressHierarchiesForAllAddressLevelTypesInOrg(); + } catch (Exception exception) { + logger.error("Error getting web locationHierarchies", exception); + return null; + } + } }