diff --git a/avni-server-api/src/main/java/org/avni/server/service/metabase/DatabaseService.java b/avni-server-api/src/main/java/org/avni/server/service/metabase/DatabaseService.java index 533b547ee..5ac6cd9ec 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/metabase/DatabaseService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/metabase/DatabaseService.java @@ -103,37 +103,51 @@ public String getInitialSyncStatus() { return responseBody.path("initial_sync_status").asText(); } - public List getSubjectTypeNames() { - int tableId = getTableIdByName("Subject Type"); - String requestBody = createRequestBodyForDataset(tableId); + private JsonNode getTableMetadata() { + int tableMetadataId = getTableIdByName("Table Metadata"); + String requestBody = createRequestBodyForDataset(tableMetadataId); + return databaseRepository.getDataset(requestBody); + } + + private String formatName(String rawName) { + String[] parts = rawName.split("_"); + StringBuilder formattedName = new StringBuilder(); - JsonNode response = databaseRepository.getDataset(requestBody); + for (String part : parts) { + formattedName.append(part.substring(0, 1).toUpperCase()) + .append(part.substring(1)) + .append(" "); + } - JsonNode dataNode = response.path("data"); - JsonNode rows = dataNode.path("rows"); + return formattedName.toString().trim(); + } + public List getSubjectTypeNames() { + JsonNode tableMetadata = getTableMetadata(); List subjectTypeNames = new ArrayList<>(); + + JsonNode rows = tableMetadata.path("data").path("rows"); for (JsonNode row : rows) { - String name = row.get(4).asText(); - boolean isVoided = row.get(6).asBoolean(); - if (!isVoided) { - subjectTypeNames.add(name); + String type = row.get(2).asText(); + if (Arrays.asList("Individual", "Household", "Group", "Person").contains(type)) { + String rawName = row.get(1).asText(); + subjectTypeNames.add(formatName(rawName)); } } return subjectTypeNames; } - private List getProgramNamesFromOperationalProgramsTable() { - int operationalProgramsTableId = getTableIdByName("All Operational Programs"); - - String requestBody = createRequestBodyForDataset(operationalProgramsTableId); - JsonNode response = databaseRepository.getDataset(requestBody); - + public List getProgramAndEncounterNames() { + JsonNode tableMetadata = getTableMetadata(); List programNames = new ArrayList<>(); - JsonNode rows = response.path("data").path("rows"); + + JsonNode rows = tableMetadata.path("data").path("rows"); for (JsonNode row : rows) { - String programName = row.get(1).asText(); - programNames.add(programName); + String type = row.get(2).asText(); + if (Arrays.asList("ProgramEncounter", "ProgramEnrolment").contains(type)) { + String rawName = row.get(1).asText(); + programNames.add(formatName(rawName)); + } } return programNames; } @@ -238,9 +252,9 @@ public void createQuestionsForProgramsAndEncounters() { throw new RuntimeException("Database initial sync is not complete."); } - List programNames = getProgramNamesFromOperationalProgramsTable(); + List programNames = getProgramAndEncounterNames(); - JsonNode databaseDetails = databaseRepository.getDatabaseDetails(databaseId); + JsonNode databaseDetails = databaseRepository.getDatabaseDetails(getDatabaseId()); List allTableNames = extractTableNames(databaseDetails); for (String programName : programNames) {