From 08f3705f8670783def191b5b892a4eed92086ff1 Mon Sep 17 00:00:00 2001 From: Om Bhardwaj <115864495+ombhardwajj@users.noreply.github.com> Date: Wed, 28 Aug 2024 16:19:47 +0530 Subject: [PATCH] avniproject#762 | Enums Added --- .../avni/server/domain/metabase/BaseType.java | 17 ++++++++++++ .../avni/server/domain/metabase/CardType.java | 15 +++++++++++ .../server/domain/metabase/ConditionType.java | 16 ++++++++++++ .../server/domain/metabase/MetabaseJoin.java | 23 +++++++++------- .../server/domain/metabase/MetabaseQuery.java | 6 +++-- .../domain/metabase/MetabaseRequestBody.java | 9 ++++--- .../server/domain/metabase/QueryType.java | 21 +++++++++++++++ .../server/domain/metabase/SyncStatus.java | 26 +++++++++++++++++++ .../domain/metabase/VisualizationType.java | 21 +++++++++++++++ .../service/metabase/DatabaseService.java | 21 +++++++-------- .../avni/server/web/MetabaseController.java | 3 ++- 11 files changed, 151 insertions(+), 27 deletions(-) create mode 100644 avni-server-api/src/main/java/org/avni/server/domain/metabase/BaseType.java create mode 100644 avni-server-api/src/main/java/org/avni/server/domain/metabase/CardType.java create mode 100644 avni-server-api/src/main/java/org/avni/server/domain/metabase/ConditionType.java create mode 100644 avni-server-api/src/main/java/org/avni/server/domain/metabase/QueryType.java create mode 100644 avni-server-api/src/main/java/org/avni/server/domain/metabase/SyncStatus.java create mode 100644 avni-server-api/src/main/java/org/avni/server/domain/metabase/VisualizationType.java diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/BaseType.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/BaseType.java new file mode 100644 index 000000000..e9404dd72 --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/BaseType.java @@ -0,0 +1,17 @@ +package org.avni.server.domain.metabase; + +public enum BaseType { + INTEGER("type/Integer"), + TEXT("type/Text"), + BOOLEAN("type/Boolean"); + + private final String typeName; + + BaseType(String typeName) { + this.typeName = typeName; + } + + public String getTypeName() { + return typeName; + } +} diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/CardType.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/CardType.java new file mode 100644 index 000000000..6afc67c28 --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/CardType.java @@ -0,0 +1,15 @@ +package org.avni.server.domain.metabase; + +public enum CardType { + QUESTION("question"); + + private final String type; + + CardType(String type) { + this.type = type; + } + + public String getType() { + return type; + } +} diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/ConditionType.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/ConditionType.java new file mode 100644 index 000000000..0a9a1c6c1 --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/ConditionType.java @@ -0,0 +1,16 @@ +package org.avni.server.domain.metabase; + +public enum ConditionType { + EQUAL("="), + NOT_EQUAL("!="); + + private final String operator; + + ConditionType(String operator) { + this.operator = operator; + } + + public String getOperator() { + return operator; + } +} 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 index 884189476..f3bde2044 100644 --- 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 @@ -2,25 +2,29 @@ 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; public class MetabaseJoin { - private final String fields; - private final String alias; - private final int sourceTable; - private final JsonNode condition; + private String fields; + private String alias; + private int sourceTable; + private JsonNode condition; public MetabaseJoin(String fields, String alias, int sourceTable, int joinFieldId1, int joinFieldId2, String tableName, ObjectMapper objectMapper) throws Exception { this.fields = fields; this.alias = alias; this.sourceTable = sourceTable; - this.condition = createConditionNode(joinFieldId1, joinFieldId2, tableName, objectMapper); + + this.condition = createConditionNode(joinFieldId1, joinFieldId2, tableName, BaseType.INTEGER, objectMapper); } - private JsonNode createConditionNode(int joinFieldId1, int joinFieldId2, String tableName, ObjectMapper objectMapper) throws Exception { - return objectMapper.readTree( - "[\"=\", [\"field\", " + joinFieldId1 + ", {\"base-type\": \"type/Integer\"}], [\"field\", " + joinFieldId2 + ", {\"base-type\": \"type/Integer\", \"join-alias\": \"" + tableName + "\"}]]" - ); + private JsonNode createConditionNode(int joinFieldId1, int joinFieldId2, String tableName, BaseType baseType, ObjectMapper objectMapper) throws Exception { + ArrayNode conditionNode = objectMapper.createArrayNode(); + conditionNode.add(ConditionType.EQUAL.getOperator()); + conditionNode.add(objectMapper.createArrayNode().add("field").add(joinFieldId1).add(objectMapper.createObjectNode().put("base-type", baseType.getTypeName()))); + conditionNode.add(objectMapper.createArrayNode().add("field").add(joinFieldId2).add(objectMapper.createObjectNode().put("base-type", baseType.getTypeName()).put("join-alias", tableName))); + return conditionNode; } public ObjectNode toJson(ObjectMapper objectMapper) { @@ -29,6 +33,7 @@ public ObjectNode toJson(ObjectMapper objectMapper) { 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 index 3e031cd4e..bdbb6a6ce 100644 --- 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 @@ -8,7 +8,8 @@ public class MetabaseQuery { private final int databaseId; private final int sourceTable; private final ArrayNode joins; - private final String type = "query"; + private final QueryType type = QueryType.QUERY; + public MetabaseQuery(int databaseId,int sourceTable, ArrayNode joins) { this.databaseId = databaseId; @@ -24,7 +25,8 @@ public ObjectNode toJson(ObjectMapper objectMapper) { ObjectNode queryNode = objectMapper.createObjectNode(); queryNode.put("source-table", sourceTable); queryNode.set("joins", joins); - queryNode.put("type", type); + queryNode.put("type", type.toString()); + return queryNode; } diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseRequestBody.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseRequestBody.java index acd919abe..68b3e4572 100644 --- a/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseRequestBody.java +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseRequestBody.java @@ -7,20 +7,22 @@ public class MetabaseRequestBody { private String name; private MetabaseQuery datasetQuery; - private String display; + private VisualizationType display; private String description; private ObjectNode visualizationSettings; private int collectionId; private Integer collectionPosition; private JsonNode resultMetadata; + private CardType cardType; - public MetabaseRequestBody(String name, MetabaseQuery datasetQuery, String display, String description, ObjectNode visualizationSettings, int collectionId) { + public MetabaseRequestBody(String name, MetabaseQuery datasetQuery, VisualizationType display, String description, ObjectNode visualizationSettings, int collectionId, CardType cardType) { this.name = name; this.datasetQuery = datasetQuery; this.display = display; this.description = description; this.visualizationSettings = visualizationSettings; this.collectionId = collectionId; + this.cardType = cardType; } public ObjectNode toJson(ObjectMapper objectMapper) { @@ -33,7 +35,8 @@ public ObjectNode toJson(ObjectMapper objectMapper) { datasetQueryNode.set("query", datasetQuery.toJson(objectMapper)); rootNode.set("dataset_query", datasetQueryNode); - rootNode.put("display", display); + rootNode.put("display", display.toString()); + rootNode.put("type", cardType.getType()); rootNode.putNull("description"); rootNode.set("visualization_settings", visualizationSettings); rootNode.put("collection_id", collectionId); diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/QueryType.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/QueryType.java new file mode 100644 index 000000000..afe4c7758 --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/QueryType.java @@ -0,0 +1,21 @@ +package org.avni.server.domain.metabase; + +public enum QueryType { + QUERY("query"), + DASHBOARD("dashboard"); + + private final String typeName; + + QueryType(String typeName) { + this.typeName = typeName; + } + + public String getTypeName() { + return typeName; + } + + @Override + public String toString() { + return typeName; + } +} diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/SyncStatus.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/SyncStatus.java new file mode 100644 index 000000000..b9b732b8d --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/SyncStatus.java @@ -0,0 +1,26 @@ +package org.avni.server.domain.metabase; + +public enum SyncStatus { + COMPLETE("complete"), + INCOMPLETE("incomplete"); + + private final String status; + + SyncStatus(String status) { + this.status = status; + } + + public String getStatus() { + return status; + } + + public static SyncStatus fromString(String status) { + for (SyncStatus s : SyncStatus.values()) { + if (s.getStatus().equalsIgnoreCase(status)) { + return s; + } + } + return null; + } + +} diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/VisualizationType.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/VisualizationType.java new file mode 100644 index 000000000..94dc0611c --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/VisualizationType.java @@ -0,0 +1,21 @@ +package org.avni.server.domain.metabase; + +public enum VisualizationType { + TABLE("table"), + CHART("chart"); + + private final String typeName; + + VisualizationType(String typeName) { + this.typeName = typeName; + } + + public String getTypeName() { + return typeName; + } + + @Override + public String toString() { + return typeName; + } +} 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 8f9c2d0ca..fa8f5d4ec 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 @@ -5,10 +5,7 @@ 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.domain.metabase.MetabaseRequestBody; +import org.avni.server.domain.metabase.*; import org.avni.server.util.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -103,9 +100,10 @@ public int getFieldIdByTableNameAndFieldName(String tableName, String fieldName) return -1; } - public String getInitialSyncStatus() { + public SyncStatus getInitialSyncStatus() { JsonNode responseBody = databaseRepository.getInitialSyncStatus(getDatabaseId()); - return responseBody.path("initial_sync_status").asText(); + String status = responseBody.path("initial_sync_status").asText(); + return SyncStatus.fromString(status); } private JsonNode getTableMetadata() { @@ -184,7 +182,7 @@ private void createQuestionForTable(String tableName, String addressTableName, S MetabaseQuery query = new MetabaseQuery(getDatabaseId(),addressTableId, joinsArray); MetabaseRequestBody requestBody = new MetabaseRequestBody( - "Address + " + tableName, query, "table", null, objectMapper.createObjectNode(), getCollectionId()); + "Address + " + tableName, query, VisualizationType.TABLE, null, objectMapper.createObjectNode(), getCollectionId(), CardType.QUESTION); databaseRepository.postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class); } @@ -218,9 +216,8 @@ private void createQuestionForTable(String tableName, String schema) { } public void createQuestionsForSubjectTypes() throws Exception { - - String syncStatus = getInitialSyncStatus(); - if (!"complete".equals(syncStatus)) { + SyncStatus syncStatus = getInitialSyncStatus(); + if (syncStatus != SyncStatus.COMPLETE) { throw new RuntimeException("Database initial sync is not complete."); } @@ -232,8 +229,8 @@ public void createQuestionsForSubjectTypes() throws Exception { } public void createQuestionsForProgramsAndEncounters() throws Exception{ - String syncStatus = getInitialSyncStatus(); - if (!"complete".equals(syncStatus)) { + SyncStatus syncStatus = getInitialSyncStatus(); + if (syncStatus != SyncStatus.COMPLETE) { throw new RuntimeException("Database initial sync is not complete."); } 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 f5624ff1f..d8ba5ff11 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 @@ -1,4 +1,5 @@ package org.avni.server.web; +import org.avni.server.domain.metabase.SyncStatus; import org.avni.server.service.metabase.DatabaseService; import org.avni.server.domain.accessControl.PrivilegeType; import org.avni.server.dao.metabase.MetabaseConnector; @@ -35,7 +36,7 @@ public void createQuestions() throws Exception{ } @GetMapping("/sync-status") - public String getSyncStatus() { + public SyncStatus getSyncStatus() { return databaseService.getInitialSyncStatus(); }