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 a5138896d..aa71b9529 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 @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import org.avni.server.domain.metabase.*; +import org.avni.server.util.ObjectMapperSingleton; import org.avni.server.util.S; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.stereotype.Repository; @@ -17,7 +18,7 @@ public class DatabaseRepository extends MetabaseConnector { private final ObjectMapper objectMapper; public DatabaseRepository(RestTemplateBuilder restTemplateBuilder) { super(restTemplateBuilder); - this.objectMapper = new ObjectMapper(); + this.objectMapper = ObjectMapperSingleton.getObjectMapper(); } public Database save(Database database) { @@ -91,7 +92,7 @@ public void createQuestionForTable(Database database, TableDetails tableDetails, FieldDetails joinField2 = getFieldDetailsByName(database, tableDetails, destinationField); ArrayNode joinsArray = objectMapper.createArrayNode(); - MetabaseQuery query = new MetabaseQueryBuilder(database, joinsArray, objectMapper) + MetabaseQuery query = new MetabaseQueryBuilder(database, joinsArray) .forTable(tableDetails) .joinWith(addressTableDetails, joinField1, joinField2) .build(); @@ -105,11 +106,11 @@ public void createQuestionForTable(Database database, TableDetails tableDetails, getCollectionByName(database).getIdAsInt() ); - postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class); + postForObject(metabaseApiUrl + "/card", requestBody.toJson(), JsonNode.class); } public void createQuestionForASingleTable(Database database, TableDetails tableDetails) { - MetabaseQuery query = new MetabaseQueryBuilder(database, objectMapper.createArrayNode(), objectMapper) + MetabaseQuery query = new MetabaseQueryBuilder(database, objectMapper.createArrayNode()) .forTable(tableDetails) .build(); @@ -122,7 +123,7 @@ public void createQuestionForASingleTable(Database database, TableDetails tableD getCollectionByName(database).getIdAsInt() ); - postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class); + postForObject(metabaseApiUrl + "/card", requestBody.toJson(), JsonNode.class); } public FieldDetails getFieldDetailsByName(Database database, TableDetails tableDetails, FieldDetails fieldDetails) { @@ -177,7 +178,7 @@ public DatabaseSyncStatus getInitialSyncStatus(Database database) { public DatasetResponse getDataset(DatasetRequestBody requestBody) { String url = metabaseApiUrl + "/dataset"; - String jsonRequestBody = requestBody.toJson(objectMapper).toString(); + String jsonRequestBody = requestBody.toJson().toString(); String jsonResponse = postForObject(url, jsonRequestBody, String.class); try { return objectMapper.readValue(jsonResponse, DatasetResponse.class); 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 index 75846eb05..a599c4b31 100644 --- 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 @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; +import org.avni.server.util.ObjectMapperSingleton; public class DatasetRequestBody { @@ -13,16 +14,16 @@ public DatasetRequestBody(Database database, TableDetails table) { this.table = table; } - public ObjectNode toJson(ObjectMapper objectMapper) { - ObjectNode rootNode = objectMapper.createObjectNode(); + public ObjectNode toJson() { + ObjectNode rootNode = ObjectMapperSingleton.getObjectMapper().createObjectNode(); rootNode.put("database", database.getId()); - ObjectNode queryNode = objectMapper.createObjectNode(); + ObjectNode queryNode = ObjectMapperSingleton.getObjectMapper().createObjectNode(); queryNode.put("source-table", table.getId()); rootNode.set("query", queryNode); rootNode.put("type", "query"); - rootNode.set("parameters", objectMapper.createArrayNode()); + rootNode.set("parameters", ObjectMapperSingleton.getObjectMapper().createArrayNode()); return rootNode; } diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/FieldAttribute.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/FieldAttribute.java index c00910731..8a3b1f9ac 100644 --- a/avni-server-api/src/main/java/org/avni/server/domain/metabase/FieldAttribute.java +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/FieldAttribute.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; +import org.avni.server.util.ObjectMapperSingleton; public enum FieldAttribute { ALL("all"), @@ -24,8 +25,8 @@ public String getAttributeName() { return attributeName; } - public ObjectNode toJson(ObjectMapper objectMapper, Object value) { - ObjectNode attributeNode = objectMapper.createObjectNode(); + public ObjectNode toJson(Object value) { + ObjectNode attributeNode = ObjectMapperSingleton.getObjectMapper().createObjectNode(); attributeNode.put(attributeName, value.toString()); return attributeNode; } diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseQueryBuilder.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseQueryBuilder.java index 1997cb8ec..19b36de6b 100644 --- a/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseQueryBuilder.java +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseQueryBuilder.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import org.avni.server.util.ObjectMapperSingleton; public class MetabaseQueryBuilder { private final Database database; @@ -10,10 +11,10 @@ public class MetabaseQueryBuilder { private final ObjectMapper objectMapper; private ObjectNode queryNode; - public MetabaseQueryBuilder(Database database, ArrayNode joinsArray, ObjectMapper objectMapper) { + public MetabaseQueryBuilder(Database database, ArrayNode joinsArray) { this.database = database; this.joinsArray = joinsArray; - this.objectMapper = objectMapper; + this.objectMapper = ObjectMapperSingleton.getObjectMapper(); this.queryNode = objectMapper.createObjectNode(); } 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 0de5a6fe5..6ea217556 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 @@ -1,8 +1,8 @@ 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; +import org.avni.server.util.ObjectMapperSingleton; // Refer docs : https://www.metabase.com/docs/latest/api/card#post-apicard public class MetabaseRequestBody { @@ -24,11 +24,11 @@ public MetabaseRequestBody(String name, MetabaseQuery datasetQuery, Visualizatio this.collectionId = collectionId; } - public ObjectNode toJson(ObjectMapper objectMapper) { - ObjectNode rootNode = objectMapper.createObjectNode(); + public ObjectNode toJson() { + ObjectNode rootNode = ObjectMapperSingleton.getObjectMapper().createObjectNode(); rootNode.put("name", name); - ObjectNode datasetQueryNode = objectMapper.createObjectNode(); + ObjectNode datasetQueryNode = ObjectMapperSingleton.getObjectMapper().createObjectNode(); datasetQueryNode.put("database", datasetQuery.getDatabaseId()); datasetQueryNode.put("type", "query"); datasetQueryNode.set("query", datasetQuery.toJson()); 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 3aac32847..cf5c8b658 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 @@ -37,5 +37,4 @@ public void createQuestions() throws Exception{ public SyncStatus getSyncStatus() { return databaseService.getInitialSyncStatus(); } - }