Skip to content

Commit

Permalink
avniproject#762 | DatasetResponse added
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhardwajj committed Aug 31, 2024
1 parent e21282d commit 935706b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import org.avni.server.domain.metabase.Database;
import org.avni.server.domain.metabase.DatabaseSyncStatus;
import org.avni.server.domain.metabase.FieldDetails;
import org.avni.server.domain.metabase.MetabaseDatabaseInfo;
import org.avni.server.domain.metabase.*;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Repository;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -57,8 +54,13 @@ public DatabaseSyncStatus getInitialSyncStatus(int databaseId) {
}
}

public JsonNode getDataset(String requestBody) {
public DatasetResponse getDataset(String requestBody) {
String url = metabaseApiUrl + "/dataset";
return postForObject(url, requestBody, JsonNode.class);
String jsonResponse = postForObject(url, requestBody, String.class);
try {
return objectMapper.readValue(jsonResponse, DatasetResponse.class);
} catch (Exception e) {
throw new RuntimeException("Failed to parse dataset response", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.avni.server.domain.metabase;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class DatasetResponse {

@JsonProperty("data")
private Data data;

public Data getData() {
return data;
}

public void setData(Data data) {
this.data = data;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Data {

@JsonProperty("rows")
private List<List<String>> rows;

public List<List<String>> getRows() {
return rows;
}

public void setRows(List<List<String>> rows) {
this.rows = rows;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,46 +106,58 @@ public SyncStatus getInitialSyncStatus() {
}


private JsonNode getTableMetadata() {
private DatasetResponse getTableMetadata() {
int tableMetadataId = getTableIdByName("Table Metadata");
String requestBody = createRequestBodyForDataset(tableMetadataId);
return databaseRepository.getDataset(requestBody);
}

public List<String> getSubjectTypeNames() {
JsonNode tableMetadata = getTableMetadata();
DatasetResponse tableMetadata = getTableMetadata();
List<String> subjectTypeNames = new ArrayList<>();

JsonNode rows = tableMetadata.path("data").path("rows");
for (JsonNode row : rows) {
String type = row.get(2).asText();
if (Arrays.asList(TableType.INDIVIDUAL.getTypeName(), TableType.HOUSEHOLD.getTypeName(), TableType.GROUP.getTypeName(), TableType.PERSON.getTypeName()).contains(type)) {
String rawName = row.get(1).asText();
List<List<String>> rows = tableMetadata.getData().getRows();
for (List<String> row : rows) {
String type = row.get(2);
if (Arrays.asList(
TableType.INDIVIDUAL.getTypeName(),
TableType.HOUSEHOLD.getTypeName(),
TableType.GROUP.getTypeName(),
TableType.PERSON.getTypeName()
).contains(type)) {
String rawName = row.get(1);
subjectTypeNames.add(S.formatName(rawName));
}
}
System.out.println("The subject type names::" + subjectTypeNames);

System.out.println("The subject type names: " + subjectTypeNames);
return subjectTypeNames;
}



public List<String> getProgramAndEncounterNames() {
JsonNode tableMetadata = getTableMetadata();
DatasetResponse tableMetadata = getTableMetadata();
List<String> programNames = new ArrayList<>();

JsonNode rows = tableMetadata.path("data").path("rows");
for (JsonNode row : rows) {
String type = row.get(2).asText();
if (Arrays.asList(TableType.PROGRAM_ENCOUNTER.getTypeName(), TableType.PROGRAM_ENROLMENT.getTypeName()).contains(type)) {
String rawName = row.get(1).asText();
List<List<String>> rows = tableMetadata.getData().getRows();
for (List<String> row : rows) {
String type = row.get(2);
if (Arrays.asList(
TableType.PROGRAM_ENCOUNTER.getTypeName(),
TableType.PROGRAM_ENROLMENT.getTypeName()
).contains(type)) {
String rawName = row.get(1);
programNames.add(S.formatName(rawName));
}
}
System.out.println("The program and encounter::" + programNames);

System.out.println("The program and encounter names: " + programNames);
return programNames;
}



private List<String> extractTableNames(JsonNode databaseDetails) {
List<String> tableNames = new ArrayList<>();
JsonNode tablesArray = databaseDetails.path("tables");
Expand Down

0 comments on commit 935706b

Please sign in to comment.