diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseJoin.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseJoin.java new file mode 100644 index 000000000..9e5b913e9 --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseJoin.java @@ -0,0 +1,28 @@ +package org.avni.server.domain.metabase; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +public class MetabaseJoin { + private final String fields; + private final String alias; + private final int sourceTable; + private final JsonNode condition; + + public MetabaseJoin(String fields, String alias, int sourceTable, JsonNode condition) { + this.fields = fields; + this.alias = alias; + this.sourceTable = sourceTable; + this.condition = condition; + } + + public ObjectNode toJson(ObjectMapper objectMapper) { + ObjectNode joinNode = objectMapper.createObjectNode(); + joinNode.put("fields", this.fields); + joinNode.put("alias", this.alias); + joinNode.put("source-table", this.sourceTable); + joinNode.set("condition", this.condition); + return joinNode; + } +} diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseQuery.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseQuery.java new file mode 100644 index 000000000..a2084cfa2 --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseQuery.java @@ -0,0 +1,22 @@ +package org.avni.server.domain.metabase; + +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +public class MetabaseQuery { + private final int sourceTable; + private final ArrayNode joins; + + public MetabaseQuery(int sourceTable, ArrayNode joins) { + this.sourceTable = sourceTable; + this.joins = joins; + } + + public ObjectNode toJson(ObjectMapper objectMapper) { + ObjectNode queryNode = objectMapper.createObjectNode(); + queryNode.put("source-table", this.sourceTable); + queryNode.set("joins", this.joins); + return queryNode; + } +} 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 d41ee35a5..75ff22f9b 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 @@ -1,10 +1,13 @@ -ckage org.avni.server.service.metabase; +package org.avni.server.service.metabase; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import org.avni.server.dao.metabase.DatabaseRepository; +import org.avni.server.domain.metabase.TableType; +import org.avni.server.domain.metabase.MetabaseJoin; +import org.avni.server.domain.metabase.MetabaseQuery; import org.avni.server.util.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -12,6 +15,7 @@ import java.util.Arrays; import java.util.ArrayList; import java.util.List; +import java.util.HashMap; @Service public class DatabaseService { @@ -165,35 +169,26 @@ private List extractTableNames(JsonNode databaseDetails) { return tableNames; } - private void createQuestionForTable(String tableName, String addressTableName, String addressField, String tableField) { + private void createQuestionForTable(String tableName, String addressTableName, String addressField, String tableField) throws Exception { int addressTableId = getTableIdByName(addressTableName); int joinFieldId1 = getFieldIdByTableNameAndFieldName(addressTableName, addressField); int tableId = getTableIdByName(tableName); int joinFieldId2 = getFieldIdByTableNameAndFieldName(tableName, tableField); - ObjectNode datasetQuery = objectMapper.createObjectNode(); - datasetQuery.put("database", getDatabaseId()); - datasetQuery.put("type", "query"); - - ObjectNode query = objectMapper.createObjectNode(); - query.put("source-table", addressTableId); - - ArrayNode joins = objectMapper.createArrayNode(); - ObjectNode join = objectMapper.createObjectNode(); - join.put("fields", "all"); - join.put("alias", tableName); + JsonNode conditionNode = objectMapper.readTree( + "[\"=\", [\"field\", " + joinFieldId1 + ", {\"base-type\": \"type/Integer\"}], [\"field\", " + joinFieldId2 + ", {\"base-type\": \"type/Integer\", \"join-alias\": \"" + tableName + "\"}]]" + ); + MetabaseJoin join = new MetabaseJoin("all", tableName, tableId, conditionNode); // Change: tableId as integer - ArrayNode condition = objectMapper.createArrayNode(); - condition.add("="); - condition.add(objectMapper.createArrayNode().add("field").add(joinFieldId1).add(objectMapper.createObjectNode().put("base-type", "type/Integer"))); - condition.add(objectMapper.createArrayNode().add("field").add(joinFieldId2).add(objectMapper.createObjectNode().put("base-type", "type/Integer").put("join-alias", tableName))); + ArrayNode joinsArray = objectMapper.createArrayNode(); + joinsArray.add(join.toJson(objectMapper)); - join.set("condition", condition); - join.put("source-table", tableId); - joins.add(join); + MetabaseQuery query = new MetabaseQuery(addressTableId, joinsArray); // Change: addressTableId as integer - query.set("joins", joins); - datasetQuery.set("query", query); + ObjectNode datasetQuery = objectMapper.createObjectNode(); + datasetQuery.put("database", getDatabaseId()); + datasetQuery.put("type", "query"); + datasetQuery.set("query", query.toJson(objectMapper)); ObjectNode body = objectMapper.createObjectNode(); body.put("name", "Address + " + tableName); @@ -208,6 +203,8 @@ private void createQuestionForTable(String tableName, String addressTableName, S databaseRepository.postForObject(metabaseApiUrl + "/card", body, JsonNode.class); } + + private void createQuestionForTable(String tableName, String schema) { int tableId = getTableIdByName(tableName, schema); @@ -236,7 +233,7 @@ private void createQuestionForTable(String tableName, String schema) { ); } - public void createQuestionsForSubjectTypes() { + public void createQuestionsForSubjectTypes() throws Exception { String syncStatus = getInitialSyncStatus(); if (!"complete".equals(syncStatus)) { @@ -250,7 +247,7 @@ public void createQuestionsForSubjectTypes() { } } - public void createQuestionsForProgramsAndEncounters() { + public void createQuestionsForProgramsAndEncounters() throws Exception{ String syncStatus = getInitialSyncStatus(); if (!"complete".equals(syncStatus)) { throw new RuntimeException("Database initial sync is not complete."); diff --git a/avni-server-api/src/main/java/org/avni/server/service/metabase/MetabaseService.java b/avni-server-api/src/main/java/org/avni/server/service/metabase/MetabaseService.java index c1a08c55d..fad55d9ea 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/metabase/MetabaseService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/metabase/MetabaseService.java @@ -64,7 +64,7 @@ public void setupMetabase() { collectionPermissionsRepository.updateCollectionPermissions(collectionPermissions, metabaseGroup.getId(), metabaseCollection.getId()); } - public void createQuestionsForSubjectTypes() { + public void createQuestionsForSubjectTypes() throws Exception{ databaseService.createQuestionsForSubjectTypes(); } diff --git a/avni-server-api/src/main/java/org/avni/server/web/MetabaseController.java b/avni-server-api/src/main/java/org/avni/server/web/MetabaseController.java index 6eb423fbf..f5624ff1f 100644 --- a/avni-server-api/src/main/java/org/avni/server/web/MetabaseController.java +++ b/avni-server-api/src/main/java/org/avni/server/web/MetabaseController.java @@ -28,7 +28,7 @@ public void setupMetabase() { } @PostMapping("/create-questions") - public void createQuestions() { + public void createQuestions() throws Exception{ databaseService.createQuestionsForSubjectTypes(); databaseService.createQuestionsForProgramsAndEncounters(); databaseService.createQuestionsForIndivdualTables();