From 5f58deb26d21214279860b6debe89437c18935d6 Mon Sep 17 00:00:00 2001 From: Om Bhardwaj <115864495+ombhardwajj@users.noreply.github.com> Date: Sat, 31 Aug 2024 17:40:29 +0530 Subject: [PATCH] avniproject#762 | Added AddressQuestionCreationService and QuestionCreationService --- .../AddressQuestionCreationService.java | 74 +++++++++++++++++++ .../service/metabase/DatabaseService.java | 12 +-- .../metabase/QuestionCreationService.java | 6 ++ 3 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 avni-server-api/src/main/java/org/avni/server/service/metabase/AddressQuestionCreationService.java create mode 100644 avni-server-api/src/main/java/org/avni/server/service/metabase/QuestionCreationService.java 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 new file mode 100644 index 000000000..4f061266e --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/service/metabase/AddressQuestionCreationService.java @@ -0,0 +1,74 @@ +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.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Service; + +@Service +public class AddressQuestionCreationService implements QuestionCreationService { + + private final DatabaseService databaseService; + private final DatabaseRepository databaseRepository; + private final ObjectMapper objectMapper; + private final String metabaseApiUrl; + + @Autowired + public AddressQuestionCreationService(@Lazy DatabaseService databaseService, DatabaseRepository databaseRepository, ObjectMapper objectMapper, @Value("${metabase.api.url}") String metabaseApiUrl) { + this.databaseService = databaseService; + this.databaseRepository = databaseRepository; + this.objectMapper = objectMapper; + this.metabaseApiUrl = metabaseApiUrl; + } + + @Override + public void createQuestionForTable(String tableName, String addressTableName, String addressField, String tableField) throws Exception { + int addressTableId = databaseService.getTableIdByName(addressTableName); + int joinFieldId1 = databaseService.getFieldIdByTableNameAndFieldName(addressTableName, addressField); + int tableId = databaseService.getTableIdByName(tableName); + int joinFieldId2 = databaseService.getFieldIdByTableNameAndFieldName(tableName, tableField); + + MetabaseJoin join = new MetabaseJoin("all", tableName, tableId, joinFieldId1, joinFieldId2, tableName, objectMapper); + + ArrayNode joinsArray = objectMapper.createArrayNode(); + joinsArray.add(join.toJson(objectMapper)); + + MetabaseQuery query = new MetabaseQuery(databaseService.getDatabaseId(), addressTableId, joinsArray); + + MetabaseRequestBody requestBody = new MetabaseRequestBody( + "Address + " + tableName, query, VisualizationType.TABLE, null, objectMapper.createObjectNode(), databaseService.getCollectionId(), CardType.QUESTION); + + databaseRepository.postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class); + } + + @Override + public void createQuestionForTable(String tableName, String schema) { + int tableId = databaseService.getTableIdByName(tableName, schema); + + ObjectNode datasetQuery = objectMapper.createObjectNode(); + datasetQuery.put("database", databaseService.getDatabaseId()); + datasetQuery.put("type", "query"); + + ObjectNode query = objectMapper.createObjectNode(); + query.put("source-table", tableId); + datasetQuery.set("query", query); + + ObjectNode body = objectMapper.createObjectNode(); + body.put("name", tableName); + body.set("dataset_query", datasetQuery); + body.put("display", "table"); + body.putNull("description"); + body.set("visualization_settings", objectMapper.createObjectNode()); + body.put("collection_id", databaseService.getCollectionId()); + body.putNull("collection_position"); + body.putNull("result_metadata"); + + databaseRepository.postForObject(metabaseApiUrl + "/card", body, JsonNode.class); + } +} 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 cb801687d..100626eb5 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 @@ -22,6 +22,7 @@ public class DatabaseService { private final DatabaseRepository databaseRepository; private final ObjectMapper objectMapper; private final MetabaseService metabaseService; + private final AddressQuestionCreationService addressQuestionCreationService; private Integer databaseId; private Integer collectionId; @@ -32,20 +33,21 @@ public class DatabaseService { private String apiKey; @Autowired - public DatabaseService(DatabaseRepository databaseRepository, ObjectMapper objectMapper, MetabaseService metabaseService) { + public DatabaseService(DatabaseRepository databaseRepository, ObjectMapper objectMapper, MetabaseService metabaseService, AddressQuestionCreationService addressQuestionCreationService) { this.databaseRepository = databaseRepository; this.objectMapper = objectMapper; this.metabaseService = metabaseService; + this.addressQuestionCreationService = addressQuestionCreationService; } - private int getDatabaseId() { + public int getDatabaseId() { if (databaseId == null) { databaseId = metabaseService.getGlobalDatabaseId(); } return databaseId; } - private int getCollectionId() { + public int getCollectionId() { if (collectionId == null) { collectionId = metabaseService.getGlobalCollectionId(); } @@ -223,7 +225,7 @@ public void createQuestionsForSubjectTypes() throws Exception { List subjectTypeNames = getSubjectTypeNames(); for (String subjectTypeName : subjectTypeNames) { - createQuestionForTable(subjectTypeName, "Address", "id", "address_id"); + addressQuestionCreationService.createQuestionForTable(subjectTypeName, "Address", "id", "address_id"); } } @@ -244,7 +246,7 @@ public void createQuestionsForProgramsAndEncounters() throws Exception { for (String programName : programNames) { for (String tableName : allTableNames) { if (tableName.equalsIgnoreCase(programName)) { - createQuestionForTable(tableName, "Address", "id", "address_id"); + addressQuestionCreationService.createQuestionForTable(tableName, "Address", "id", "address_id"); } } } 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 new file mode 100644 index 000000000..58babec9f --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/service/metabase/QuestionCreationService.java @@ -0,0 +1,6 @@ +package org.avni.server.service.metabase; + +public interface QuestionCreationService { + void createQuestionForTable(String tableName, String addressTableName, String addressField, String tableField) throws Exception; + void createQuestionForTable(String tableName, String schema); +}