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 d715d0088..45a2f511f 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 @@ -2,7 +2,9 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; import org.avni.server.domain.metabase.*; +import org.avni.server.util.S; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.stereotype.Repository; import com.fasterxml.jackson.databind.ObjectMapper; @@ -24,9 +26,73 @@ public Database save(Database database) { return database; } - public MetabaseDatabaseInfo getDatabaseDetails(int databaseId) { - String url = metabaseApiUrl + "/database/" + databaseId + "?include=tables"; + public Database getDatabaseByName(String databaseName) { + String url = metabaseApiUrl + "/database"; + String jsonResponse = getForObject(url, String.class); + try { + List databases = objectMapper.readValue(jsonResponse, new TypeReference>() {}); + return databases.stream() + .filter(db -> db.getName().equals(databaseName)) + .findFirst() + .orElseThrow(() -> new RuntimeException("Database with name " + databaseName + " not found.")); + } catch (Exception e) { + throw new RuntimeException("Failed to retrieve database", e); + } + } + + public MetabaseCollectionInfo getCollectionByName(String collectionName) { + String url = metabaseApiUrl + "/collection"; + String jsonResponse = getForObject(url, String.class); + + try { + List collections = objectMapper.readValue(jsonResponse, new TypeReference>() {}); + return collections.stream() + .filter(coll -> coll.getName().equals(collectionName)) + .findFirst() + .orElseThrow(() -> new RuntimeException("Collection with name " + collectionName + " not found.")); + } catch (Exception e) { + throw new RuntimeException("Failed to retrieve collection", e); + } + } + + public void createQuestionForTable(Database database, TableDetails tableDetails, String addressTableName, String addressField, String tableField) throws Exception { + TableDetails addressTableDetails = getTableDetailsByDisplayName(database, addressTableName); + FieldDetails joinField1 = getFieldDetailsByName(database, addressTableName, addressField); + FieldDetails joinField2 = getFieldDetailsByName(database, tableDetails.getName(), tableField); + + ArrayNode joinsArray = objectMapper.createArrayNode(); + MetabaseQuery query = new MetabaseQueryBuilder(database, joinsArray) + .forTable(tableDetails) + .joinWith(addressTableDetails, joinField1, joinField2) + .build(); + + MetabaseRequestBody requestBody = new MetabaseRequestBody( + "Address + " + tableDetails.getDisplayName(), + query, + VisualizationType.TABLE, + null, + objectMapper.createObjectNode(), + getCollectionByName(database.getName()).getId(), + CardType.QUESTION + ); + + postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class); + } + + public FieldDetails getFieldDetailsByName(Database database, String tableName, String fieldName) { + List fieldsList = getFields(database); + String snakeCaseTableName = S.toSnakeCase(tableName); + + return fieldsList.stream() + .filter(field -> snakeCaseTableName.equals(field.getTableName()) && fieldName.equals(field.getName())) + .findFirst() + .orElseThrow(() -> new RuntimeException("Field " + fieldName + " not found in table " + tableName)); + } + + public MetabaseDatabaseInfo getDatabaseDetails(Database database) { + String url = metabaseApiUrl + "/database/" + database.getId() + "?include=tables"; String jsonResponse = getForObject(url, String.class); + try { return objectMapper.readValue(jsonResponse, MetabaseDatabaseInfo.class); } catch (Exception e) { @@ -34,9 +100,10 @@ public MetabaseDatabaseInfo getDatabaseDetails(int databaseId) { } } - public List getFields(int databaseId) { - String url = metabaseApiUrl + "/database/" + databaseId + "/fields"; + public List getFields(Database database) { + String url = metabaseApiUrl + "/database/" + database.getId() + "/fields"; String jsonResponse = getForObject(url, String.class); + try { return objectMapper.readValue(jsonResponse, new TypeReference>() {}); } catch (Exception e) { @@ -44,6 +111,14 @@ public List getFields(int databaseId) { } } + public TableDetails getTableDetailsByDisplayName(Database database, String tableName) { + MetabaseDatabaseInfo databaseInfo = getDatabaseDetails(database); + return databaseInfo.getTables().stream() + .filter(tableDetail -> tableDetail.nameMatches(tableName)) + .findFirst() + .orElseThrow(() -> new RuntimeException("Table with name " + tableName + " not found.")); + } + public DatabaseSyncStatus getInitialSyncStatus(int databaseId) { String url = metabaseApiUrl + "/database/" + databaseId; String jsonResponse = getForObject(url, String.class); @@ -63,4 +138,13 @@ public DatasetResponse getDataset(String requestBody) { throw new RuntimeException("Failed to parse dataset response", e); } } + + public DatasetResponse findAll(TableDetails table, Database database) { + String requestBody = createRequestBodyForDataset(database, table); + return getDataset(requestBody); + } + + private String createRequestBodyForDataset(Database database,TableDetails table) { + return "{\"database\":" + database.getId() + ",\"query\":{\"source-table\":" + table.getId() + "},\"type\":\"query\",\"parameters\":[]}"; + } } diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseCollectionInfo.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseCollectionInfo.java new file mode 100644 index 000000000..e67046408 --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseCollectionInfo.java @@ -0,0 +1,37 @@ +package org.avni.server.domain.metabase; + +public class MetabaseCollectionInfo { + private String name; + private int id; + private boolean isPersonal; + + public MetabaseCollectionInfo(String name, int id, boolean isPersonal) { + this.name = name; + this.id = id; + this.isPersonal = isPersonal; + } + + public String getName() { + return name; + } + + public int getId() { + return id; + } + + public boolean isPersonal() { + return isPersonal; + } + + public void setName(String name) { + this.name = name; + } + + public void setId(int id) { + this.id = id; + } + + public void setPersonal(boolean personal) { + isPersonal = personal; + } +} 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 bdbb6a6ce..5b2db827c 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 @@ -5,29 +5,24 @@ import com.fasterxml.jackson.databind.node.ObjectNode; public class MetabaseQuery { - private final int databaseId; - private final int sourceTable; + private final Database database; private final ArrayNode joins; - private final QueryType type = QueryType.QUERY; - - public MetabaseQuery(int databaseId,int sourceTable, ArrayNode joins) { - this.databaseId = databaseId; - this.sourceTable = sourceTable; + public MetabaseQuery(Database database, ArrayNode joins) { + this.database = database; this.joins = joins; } public int getDatabaseId() { - return databaseId; + return database.getId(); } + public ObjectNode toJson(ObjectMapper objectMapper) { ObjectNode queryNode = objectMapper.createObjectNode(); - queryNode.put("source-table", sourceTable); + queryNode.put("database", database.getId()); queryNode.set("joins", joins); - queryNode.put("type", type.toString()); - - + queryNode.put("type", "query"); return queryNode; } } 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 new file mode 100644 index 000000000..28460c685 --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/MetabaseQueryBuilder.java @@ -0,0 +1,41 @@ +// to be completed +package org.avni.server.domain.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; + +public class MetabaseQueryBuilder { + private final Database database; + private ArrayNode joins; + + public MetabaseQueryBuilder(Database database, ArrayNode joins) { + this.database = database; + this.joins = joins; + } + + public MetabaseQueryBuilder forTable(TableDetails tableDetails) { + // code to be added + return this; + } + + + public MetabaseQueryBuilder joinWith(TableDetails joinTable, FieldDetails originField, FieldDetails destinationField) { + // Build the join condition and add to the joins array + ObjectMapper objectMapper = new ObjectMapper(); + ArrayNode joinCondition = objectMapper.createArrayNode(); + + joinCondition.add(ConditionType.EQUAL.getOperator()); + joinCondition.add(objectMapper.createArrayNode().add("field").add(originField.getId())); + joinCondition.add(objectMapper.createArrayNode().add("field").add(destinationField.getId())); + + joins.add(joinCondition); + return this; + } + + + public MetabaseQuery build() { + return new MetabaseQuery(database, joins); + } +} diff --git a/avni-server-api/src/main/java/org/avni/server/domain/metabase/TableDetails.java b/avni-server-api/src/main/java/org/avni/server/domain/metabase/TableDetails.java index d57694aeb..b07844512 100644 --- a/avni-server-api/src/main/java/org/avni/server/domain/metabase/TableDetails.java +++ b/avni-server-api/src/main/java/org/avni/server/domain/metabase/TableDetails.java @@ -69,4 +69,7 @@ public String getDisplayName() { return displayName; } + public boolean nameMatches(String tableName) { + return tableName.equals(getDisplayName()); + } } 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 4f061266e..fca124e07 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 @@ -29,46 +29,14 @@ public AddressQuestionCreationService(@Lazy DatabaseService databaseService, Dat @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); + Database database = databaseService.getGlobalDatabase(); + TableDetails tableDetails = databaseRepository.getTableDetailsByDisplayName(database, tableName); - 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); + databaseRepository.createQuestionForTable(database, tableDetails, addressTableName, addressField, tableField); } @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); + // to be added } } 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 100626eb5..586529ace 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 @@ -12,6 +12,7 @@ import java.util.Arrays; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import org.avni.server.util.S; @@ -40,6 +41,11 @@ public DatabaseService(DatabaseRepository databaseRepository, ObjectMapper objec this.addressQuestionCreationService = addressQuestionCreationService; } + public Database getGlobalDatabase() { + int databaseId = metabaseService.getGlobalDatabaseId(); + return databaseRepository.getDatabaseByName(String.valueOf(databaseId)); + } + public int getDatabaseId() { if (databaseId == null) { databaseId = metabaseService.getGlobalDatabaseId(); @@ -54,8 +60,8 @@ public int getCollectionId() { return collectionId; } - public int getTableIdByName(String tableName) { - MetabaseDatabaseInfo databaseInfo = databaseRepository.getDatabaseDetails(getDatabaseId()); + public int getTableIdByDisplayName(String tableName) { + MetabaseDatabaseInfo databaseInfo = databaseRepository.getDatabaseDetails(getGlobalDatabase()); List tables = databaseInfo.getTables(); for (TableDetails table : tables) { @@ -66,8 +72,8 @@ public int getTableIdByName(String tableName) { return -1; } - public int getTableIdByName(String tableName, String schema) { - MetabaseDatabaseInfo databaseInfo = databaseRepository.getDatabaseDetails(getDatabaseId()); + public int getTableIdByDisplayName(String tableName, String schema) { + MetabaseDatabaseInfo databaseInfo = databaseRepository.getDatabaseDetails(getGlobalDatabase()); List tables = databaseInfo.getTables(); for (TableDetails table : tables) { @@ -84,13 +90,26 @@ public int getTableIdByName(String tableName, String schema) { return -1; } + public TableDetails getTableDetailsByDisplayName(String tableName) { + MetabaseDatabaseInfo databaseInfo = databaseRepository.getDatabaseDetails(getGlobalDatabase()); + List tables = databaseInfo.getTables(); + + // Handle Optional properly + Optional tableDetailsOptional = tables.stream() + .filter(tableDetail -> tableDetail.nameMatches(tableName)) + .findFirst(); + + return tableDetailsOptional.orElseThrow(() -> + new RuntimeException("Table not found: " + tableName)); + } + private String createRequestBodyForDataset(int sourceTableId) { return "{\"database\":" + getDatabaseId() + ",\"query\":{\"source-table\":" + sourceTableId + "},\"type\":\"query\",\"parameters\":[]}"; } public int getFieldIdByTableNameAndFieldName(String tableName, String fieldName) { - List fieldsList = databaseRepository.getFields(getDatabaseId()); + List fieldsList = databaseRepository.getFields(getGlobalDatabase()); String snakeCaseTableName = S.toSnakeCase(tableName); for (FieldDetails field : fieldsList) { @@ -108,53 +127,52 @@ public SyncStatus getInitialSyncStatus() { } - private DatasetResponse getTableMetadata() { - int tableMetadataId = getTableIdByName("Table Metadata"); - String requestBody = createRequestBodyForDataset(tableMetadataId); - return databaseRepository.getDataset(requestBody); + private DatasetResponse getTableMetadata(Database database) { + TableDetails metadataTable = databaseRepository.getTableDetailsByDisplayName(database, "table_metadata"); + return databaseRepository.findAll(metadataTable, database); } public List getSubjectTypeNames() { - DatasetResponse tableMetadata = getTableMetadata(); + Database database = getGlobalDatabase(); + TableDetails metadataTable = databaseRepository.getTableDetailsByDisplayName(database, "table_metadata"); + + DatasetResponse datasetResponse = databaseRepository.findAll(metadataTable, database); + List> rows = datasetResponse.getData().getRows(); + List subjectTypeNames = new ArrayList<>(); - List> rows = tableMetadata.getData().getRows(); for (List row : rows) { String type = row.get(2); - if (Arrays.asList( - TableType.INDIVIDUAL.getTypeName(), - TableType.HOUSEHOLD.getTypeName(), - TableType.GROUP.getTypeName(), - TableType.PERSON.getTypeName() - ).contains(type)) { - String rawName = row.get(1); - subjectTypeNames.add(S.formatName(rawName)); + if (type.equalsIgnoreCase(TableType.INDIVIDUAL.getTypeName()) || + type.equalsIgnoreCase(TableType.HOUSEHOLD.getTypeName()) || + type.equalsIgnoreCase(TableType.GROUP.getTypeName()) || + type.equalsIgnoreCase(TableType.PERSON.getTypeName())) { + subjectTypeNames.add(row.get(1)); } } - System.out.println("The subject type names: " + subjectTypeNames); return subjectTypeNames; } public List getProgramAndEncounterNames() { - DatasetResponse tableMetadata = getTableMetadata(); + Database database = getGlobalDatabase(); + TableDetails metadataTable = databaseRepository.getTableDetailsByDisplayName(database, "table_metadata"); + + DatasetResponse datasetResponse = databaseRepository.findAll(metadataTable, database); + List> rows = datasetResponse.getData().getRows(); + List programNames = new ArrayList<>(); - List> rows = tableMetadata.getData().getRows(); for (List row : rows) { String type = row.get(2); - if (Arrays.asList( - TableType.PROGRAM_ENCOUNTER.getTypeName(), - TableType.PROGRAM_ENROLMENT.getTypeName() - ).contains(type)) { - String rawName = row.get(1); - programNames.add(S.formatName(rawName)); + if (type.equalsIgnoreCase(TableType.PROGRAM_ENCOUNTER.getTypeName()) || + type.equalsIgnoreCase(TableType.PROGRAM_ENROLMENT.getTypeName())) { + programNames.add(row.get(1)); } } - System.out.println("The program and encounter names: " + programNames); return programNames; } @@ -169,27 +187,8 @@ private List extractTableNames(JsonNode databaseDetails) { return tableNames; } - 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); - - MetabaseJoin join = new MetabaseJoin("all", tableName, tableId, joinFieldId1, joinFieldId2, tableName, objectMapper); - - ArrayNode joinsArray = objectMapper.createArrayNode(); - joinsArray.add(join.toJson(objectMapper)); - - MetabaseQuery query = new MetabaseQuery(getDatabaseId(),addressTableId, joinsArray); - - MetabaseRequestBody requestBody = new MetabaseRequestBody( - "Address + " + tableName, query, VisualizationType.TABLE, null, objectMapper.createObjectNode(), getCollectionId(), CardType.QUESTION); - - databaseRepository.postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class); - } - private void createQuestionForTable(String tableName, String schema) { - int tableId = getTableIdByName(tableName, schema); + int tableId = getTableIdByDisplayName(tableName, schema); ObjectNode datasetQuery = objectMapper.createObjectNode(); datasetQuery.put("database", getDatabaseId()); @@ -219,12 +218,15 @@ private void createQuestionForTable(String tableName, String schema) { public void createQuestionsForSubjectTypes() throws Exception { SyncStatus syncStatus = getInitialSyncStatus(); if (syncStatus != SyncStatus.COMPLETE) { - throw new RuntimeException("Database initial sync is not complete."); + throw new RuntimeException("Database sync is not complete. Cannot create questions."); } + Database database = getGlobalDatabase(); List subjectTypeNames = getSubjectTypeNames(); for (String subjectTypeName : subjectTypeNames) { + TableDetails subjectTable = databaseRepository.getTableDetailsByDisplayName(database, subjectTypeName); + addressQuestionCreationService.createQuestionForTable(subjectTypeName, "Address", "id", "address_id"); } } @@ -232,35 +234,39 @@ public void createQuestionsForSubjectTypes() throws Exception { public void createQuestionsForProgramsAndEncounters() throws Exception { SyncStatus syncStatus = getInitialSyncStatus(); if (syncStatus != SyncStatus.COMPLETE) { - throw new RuntimeException("Database initial sync is not complete."); + throw new RuntimeException("Database sync is not complete. Cannot create questions."); } - List programNames = getProgramAndEncounterNames(); + Database database = getGlobalDatabase(); - MetabaseDatabaseInfo databaseInfo = databaseRepository.getDatabaseDetails(getDatabaseId()); - List allTableNames = databaseInfo.getTables() - .stream() - .map(TableDetails::getDisplayName) - .collect(Collectors.toList()); + List programAndEncounterNames = getProgramAndEncounterNames(); - for (String programName : programNames) { - for (String tableName : allTableNames) { - if (tableName.equalsIgnoreCase(programName)) { - addressQuestionCreationService.createQuestionForTable(tableName, "Address", "id", "address_id"); - } - } + for (String programName : programAndEncounterNames) { + TableDetails programTable = databaseRepository.getTableDetailsByDisplayName(database, programName); + addressQuestionCreationService.createQuestionForTable(programName, "Address", "id", "address_id"); } } - public void createQuestionsForIndivdualTables() { - List tablesToCreateQuestionsFor = Arrays.asList( - "Address", - "Media", - "Sync Telemetry" - ); + public void createQuestionsForIndividualTables() throws Exception { + SyncStatus syncStatus = getInitialSyncStatus(); + if (syncStatus != SyncStatus.COMPLETE) { + throw new RuntimeException("Database sync is not complete. Cannot create questions."); + } + + Database database = getGlobalDatabase(); + List tablesToCreateQuestionsFor = Arrays.asList("Address", "Media", "Sync Telemetry"); for (String tableName : tablesToCreateQuestionsFor) { - createQuestionForTable(tableName, "!public"); + TableDetails tableDetails = databaseRepository.getTableDetailsByDisplayName(database, tableName); + databaseRepository.createQuestionForTable(database, tableDetails, "Address", "id", "address_id"); } } + + public void createQuestions() throws Exception { + createQuestionsForSubjectTypes(); + + createQuestionsForProgramsAndEncounters(); + + createQuestionsForIndividualTables(); + } } 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 fad55d9ea..4ac97ec05 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 @@ -23,7 +23,7 @@ public class MetabaseService { private final CollectionPermissionsRepository collectionPermissionsRepository; private final CollectionRepository collectionRepository; private Database globalDatabase; - private CollectionResponse globalCollection; + private MetabaseCollectionInfo globalCollection; @Autowired public MetabaseService(OrganisationService organisationService, @@ -51,7 +51,7 @@ public void setupMetabase() { this.globalDatabase = database; CollectionResponse metabaseCollection = collectionRepository.save(new Collection(name, name + " collection")); - this.globalCollection = metabaseCollection; + this.globalCollection = new MetabaseCollectionInfo(null, metabaseCollection.getId(), false); Group metabaseGroup = groupPermissionsRepository.save(new Group(name)); @@ -69,10 +69,18 @@ public void createQuestionsForSubjectTypes() throws Exception{ } public int getGlobalDatabaseId() { + if (globalDatabase == null) { + Organisation currentOrganisation = organisationService.getCurrentOrganisation(); + globalDatabase = databaseRepository.getDatabaseByName(currentOrganisation.getName()); + } return globalDatabase.getId(); } public int getGlobalCollectionId() { + if (globalCollection == null) { + Organisation currentOrganisation = organisationService.getCurrentOrganisation(); + globalCollection = databaseRepository.getCollectionByName(currentOrganisation.getName() + " collection"); + } return globalCollection.getId(); } } 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 d8ba5ff11..3aac32847 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 @@ -30,9 +30,7 @@ public void setupMetabase() { @PostMapping("/create-questions") public void createQuestions() throws Exception{ - databaseService.createQuestionsForSubjectTypes(); - databaseService.createQuestionsForProgramsAndEncounters(); - databaseService.createQuestionsForIndivdualTables(); + databaseService.createQuestions(); } @GetMapping("/sync-status")