From 19b66fd5515b5fe5c4472677e3dc7eb71cfb1421 Mon Sep 17 00:00:00 2001 From: Om Bhardwaj <115864495+ombhardwajj@users.noreply.github.com> Date: Sun, 8 Sep 2024 12:08:01 +0530 Subject: [PATCH] avniproject#762 | Dataset Request Body Created --- .../dao/metabase/DatabaseRepository.java | 19 ++++++------ .../domain/metabase/DatasetRequestBody.java | 29 +++++++++++++++++++ .../server/domain/metabase/FieldDetails.java | 8 +++++ .../AddressQuestionCreationService.java | 4 +-- .../service/metabase/DatabaseService.java | 16 ++++++++-- .../metabase/QuestionCreationService.java | 3 +- 6 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 avni-server-api/src/main/java/org/avni/server/domain/metabase/DatasetRequestBody.java diff --git a/avni-server-api/src/main/java/org/avni/server/dao/metabase/DatabaseRepository.java b/avni-server-api/src/main/java/org/avni/server/dao/metabase/DatabaseRepository.java index 3b20ad76d..ce957a560 100644 --- a/avni-server-api/src/main/java/org/avni/server/dao/metabase/DatabaseRepository.java +++ b/avni-server-api/src/main/java/org/avni/server/dao/metabase/DatabaseRepository.java @@ -78,9 +78,9 @@ public CollectionInfoResponse getCollectionByName(String collectionName) { } } - public void createQuestionForTable(Database database, TableDetails tableDetails, TableDetails addressTableDetails, String addressField, String tableField) { - FieldDetails joinField1 = getFieldDetailsByName(database, addressTableDetails.getName(), addressField); - FieldDetails joinField2 = getFieldDetailsByName(database, tableDetails.getName(), tableField); + public void createQuestionForTable(Database database, TableDetails tableDetails, TableDetails addressTableDetails, FieldDetails addressField, FieldDetails tableField) { + FieldDetails joinField1 = getFieldDetailsByName(database, addressTableDetails, addressField); + FieldDetails joinField2 = getFieldDetailsByName(database, tableDetails, tableField); ArrayNode joinsArray = objectMapper.createArrayNode(); MetabaseQuery query = new MetabaseQueryBuilder(database, joinsArray, objectMapper) @@ -101,14 +101,14 @@ public void createQuestionForTable(Database database, TableDetails tableDetails, postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class); } - public FieldDetails getFieldDetailsByName(Database database, String tableName, String fieldName) { + public FieldDetails getFieldDetailsByName(Database database, TableDetails tableDetails, FieldDetails fieldDetails) { List fieldsList = getFields(database); - String snakeCaseTableName = S.toSnakeCase(tableName); + String snakeCaseTableName = S.toSnakeCase(tableDetails.getName()); return fieldsList.stream() - .filter(field -> snakeCaseTableName.equals(field.getTableName()) && fieldName.equals(field.getName())) + .filter(field -> snakeCaseTableName.equals(field.getTableName()) && fieldDetails.getName().equals(field.getName())) .findFirst() - .orElseThrow(() -> new RuntimeException("Field " + fieldName + " not found in table " + tableName)); + .orElseThrow(() -> new RuntimeException("Field " + fieldDetails.getName() + " not found in table " + tableDetails.getName())); } public MetabaseDatabaseInfo getDatabaseDetails(Database database) { @@ -166,7 +166,8 @@ public DatasetResponse findAll(TableDetails table, Database database) { return getDataset(requestBody); } - private String createRequestBodyForDataset(Database database,TableDetails table) { - return "{\"database\":" + database.getId() + ",\"query\":{\"source-table\":" + table.getId() + "},\"type\":\"query\",\"parameters\":[]}"; + private String createRequestBodyForDataset(Database database, TableDetails table) { + DatasetRequestBody requestBody = new DatasetRequestBody(database, table); + return requestBody.toJson(objectMapper).toString(); } } diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/DatasetRequestBody.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/DatasetRequestBody.java new file mode 100644 index 000000000..75846eb05 --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/DatasetRequestBody.java @@ -0,0 +1,29 @@ +package org.avni.server.domain.metabase; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +public class DatasetRequestBody { + + private final Database database; + private final TableDetails table; + + public DatasetRequestBody(Database database, TableDetails table) { + this.database = database; + this.table = table; + } + + public ObjectNode toJson(ObjectMapper objectMapper) { + ObjectNode rootNode = objectMapper.createObjectNode(); + rootNode.put("database", database.getId()); + + ObjectNode queryNode = objectMapper.createObjectNode(); + queryNode.put("source-table", table.getId()); + + rootNode.set("query", queryNode); + rootNode.put("type", "query"); + rootNode.set("parameters", objectMapper.createArrayNode()); + + return rootNode; + } +} diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/FieldDetails.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/FieldDetails.java index 94b8e5827..5b723307b 100644 --- a/avni-server-api/src/main/java/org/avni/server/domain/metabase/FieldDetails.java +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/FieldDetails.java @@ -9,9 +9,17 @@ public class FieldDetails { @JsonProperty("id") private int id; + public void setId(int id) { + this.id = id; + } + @JsonProperty("name") private String name; + public void setName(String name) { + this.name = name; + } + @JsonProperty("display_name") private String displayName; diff --git a/avni-server-api/src/main/java/org/avni/server/service/metabase/AddressQuestionCreationService.java b/avni-server-api/src/main/java/org/avni/server/service/metabase/AddressQuestionCreationService.java index 2e8d35115..0449ebb79 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/metabase/AddressQuestionCreationService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/metabase/AddressQuestionCreationService.java @@ -26,9 +26,9 @@ public AddressQuestionCreationService(@Lazy DatabaseService databaseService, Dat } @Override - public void createQuestionForTable(TableDetails tableDetails, TableDetails addressTableDetails, String addressField, String tableField) throws Exception { + public void createQuestionForTable(TableDetails tableDetails, TableDetails addressTableDetails, FieldDetails addressFieldDetails, FieldDetails tableFieldDetails) throws Exception { Database database = databaseService.getGlobalDatabase(); - databaseRepository.createQuestionForTable(database, tableDetails, addressTableDetails, addressField, tableField); + databaseRepository.createQuestionForTable(database, tableDetails, addressTableDetails, addressFieldDetails, tableFieldDetails); } 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 c7af8bb69..d32c979d9 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 @@ -127,13 +127,19 @@ public void createQuestionsForSubjectTypes() throws Exception { TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(database, addressTableDetails); + FieldDetails addressFieldDetails = new FieldDetails(); + addressFieldDetails.setName("id"); + + FieldDetails subjectFieldDetails = new FieldDetails(); + subjectFieldDetails.setName("address_id"); + for (String subjectTypeName : subjectTypeNames) { TableDetails subjectTableDetails = new TableDetails(); subjectTableDetails.setName(subjectTypeName); TableDetails fetchedSubjectTableDetails = databaseRepository.findTableDetailsByName(database, subjectTableDetails); - addressQuestionCreationService.createQuestionForTable(fetchedSubjectTableDetails, fetchedAddressTableDetails, "id", "address_id"); + addressQuestionCreationService.createQuestionForTable(fetchedSubjectTableDetails, fetchedAddressTableDetails, addressFieldDetails, subjectFieldDetails); } } @@ -153,13 +159,19 @@ public void createQuestionsForProgramsAndEncounters() throws Exception { TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(database, addressTableDetails); + FieldDetails addressFieldDetails = new FieldDetails(); + addressFieldDetails.setName("id"); + + FieldDetails programFieldDetails = new FieldDetails(); + programFieldDetails.setName("address_id"); + for (String programName : programAndEncounterNames) { TableDetails programTableDetails = new TableDetails(); programTableDetails.setName(programName); TableDetails fetchedProgramTableDetails = databaseRepository.findTableDetailsByName(database, programTableDetails); - addressQuestionCreationService.createQuestionForTable(fetchedProgramTableDetails, fetchedAddressTableDetails, "id", "address_id"); + addressQuestionCreationService.createQuestionForTable(fetchedProgramTableDetails, fetchedAddressTableDetails, addressFieldDetails, programFieldDetails); } } diff --git a/avni-server-api/src/main/java/org/avni/server/service/metabase/QuestionCreationService.java b/avni-server-api/src/main/java/org/avni/server/service/metabase/QuestionCreationService.java index 1755ff964..8ae178f68 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/metabase/QuestionCreationService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/metabase/QuestionCreationService.java @@ -1,7 +1,8 @@ package org.avni.server.service.metabase; +import org.avni.server.domain.metabase.FieldDetails; import org.avni.server.domain.metabase.TableDetails; public interface QuestionCreationService { - void createQuestionForTable(TableDetails tableDetails, TableDetails addressTableDetails, String addressField, String tableField) throws Exception; + void createQuestionForTable(TableDetails tableDetails, TableDetails addressTableDetails, FieldDetails addressFieldDetails, FieldDetails tableFieldDetails) throws Exception; void createQuestionForTable(String tableName, String schema) throws Exception; }