Skip to content

Commit

Permalink
avniproject#762 | Corrected the QuestionCreation RequestBody
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhardwajj committed Sep 8, 2024
1 parent 19b66fd commit 05cf2ed
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,21 @@ public void createQuestionForTable(Database database, TableDetails tableDetails,
.build();

MetabaseRequestBody requestBody = new MetabaseRequestBody(
"Address + " + tableDetails.getDisplayName(),
tableDetails.getDisplayName(),
query,
VisualizationType.TABLE,
null,
objectMapper.createObjectNode(),
getCollectionByName(database.getName()).getIdAsInt(),
CardType.QUESTION
getCollectionByName(database.getName()).getIdAsInt()
);

System.out.println("Final Request Body: " + requestBody.toJson(objectMapper).toPrettyString());

postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class);
}



public FieldDetails getFieldDetailsByName(Database database, TableDetails tableDetails, FieldDetails fieldDetails) {
List<FieldDetails> fieldsList = getFields(database);
String snakeCaseTableName = S.toSnakeCase(tableDetails.getName());
Expand Down Expand Up @@ -151,9 +154,10 @@ public DatabaseSyncStatus getInitialSyncStatus(Database database) {
}
}

public DatasetResponse getDataset(String requestBody) {
public DatasetResponse getDataset(DatasetRequestBody requestBody) {
String url = metabaseApiUrl + "/dataset";
String jsonResponse = postForObject(url, requestBody, String.class);
String jsonRequestBody = requestBody.toJson(objectMapper).toString();
String jsonResponse = postForObject(url, jsonRequestBody, String.class);
try {
return objectMapper.readValue(jsonResponse, DatasetResponse.class);
} catch (Exception e) {
Expand All @@ -162,12 +166,11 @@ public DatasetResponse getDataset(String requestBody) {
}

public DatasetResponse findAll(TableDetails table, Database database) {
String requestBody = createRequestBodyForDataset(database, table);
DatasetRequestBody requestBody = createRequestBodyForDataset(database, table);
return getDataset(requestBody);
}

private String createRequestBodyForDataset(Database database, TableDetails table) {
DatasetRequestBody requestBody = new DatasetRequestBody(database, table);
return requestBody.toJson(objectMapper).toString();
private DatasetRequestBody createRequestBodyForDataset(Database database, TableDetails table) {
return new DatasetRequestBody(database, table);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ public MetabaseQueryBuilder(Database database, ArrayNode joinsArray, ObjectMappe

public MetabaseQueryBuilder forTable(TableDetails tableDetails) {
queryNode.put("source-table", tableDetails.getId());
queryNode.put("database", database.getId());
return this;
}

public MetabaseQueryBuilder joinWith(TableDetails addressTable, FieldDetails joinField1, FieldDetails joinField2) {
public MetabaseQueryBuilder joinWith(TableDetails addressTable, FieldDetails joinField1, FieldDetails joinField2) {
ObjectNode joinNode = objectMapper.createObjectNode();
joinNode.put("fields", "all");
joinNode.put("alias", addressTable.getName());
Expand All @@ -34,24 +33,25 @@ public MetabaseQueryBuilder joinWith(TableDetails addressTable, FieldDetails jo

ArrayNode leftField = objectMapper.createArrayNode();
leftField.add("field");
leftField.add(joinField1.getId());
leftField.add(joinField2.getId());
leftField.add(objectMapper.createObjectNode().put("base-type", "type/Integer"));
conditionArray.add(leftField);

ArrayNode rightField = objectMapper.createArrayNode();
rightField.add("field");
rightField.add(joinField2.getId());
rightField.add(joinField1.getId());
rightField.add(objectMapper.createObjectNode().put("base-type", "type/Integer").put("join-alias", addressTable.getName()));
conditionArray.add(rightField);

joinNode.set("condition", conditionArray);
joinsArray.add(joinNode);
queryNode.set("joins", joinsArray);

return this;
}


public MetabaseQuery build() {
queryNode.put("type", "query");
return new MetabaseQuery(database.getId(), queryNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ public class MetabaseRequestBody {
private int collectionId;
private Integer collectionPosition;
private JsonNode resultMetadata;
private CardType cardType;

public MetabaseRequestBody(String name, MetabaseQuery datasetQuery, VisualizationType display, String description, ObjectNode visualizationSettings, int collectionId, CardType cardType) {
public MetabaseRequestBody(String name, MetabaseQuery datasetQuery, VisualizationType display, String description, ObjectNode visualizationSettings, int collectionId) {
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) {
Expand All @@ -36,7 +34,6 @@ public ObjectNode toJson(ObjectMapper objectMapper) {

rootNode.set("dataset_query", datasetQueryNode);
rootNode.put("display", display.toString());
rootNode.put("type", cardType.getType());
rootNode.putNull("description");
rootNode.set("visualization_settings", visualizationSettings);
rootNode.put("collection_id", collectionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public void createQuestionForTable(String tableName, String schema) throws Excep
VisualizationType.TABLE,
null,
objectMapper.createObjectNode(),
databaseService.getCollectionId(),
CardType.QUESTION
databaseService.getCollectionId()
);
databaseRepository.postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,13 @@ public void createQuestionsForIndividualTables() {
VisualizationType.TABLE,
null,
objectMapper.createObjectNode(),
databaseRepository.getCollectionByName(database.getName()).getIdAsInt(),
CardType.QUESTION
databaseRepository.getCollectionByName(database.getName()).getIdAsInt()
);

databaseRepository.postForObject(metabaseApiUrl + "/card", requestBody.toJson(objectMapper), JsonNode.class);
}
}


public void createQuestions() throws Exception {
createQuestionsForSubjectTypes();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ public void setupMetabase() {
collectionPermissionsRepository.updateCollectionPermissions(collectionPermissions, metabaseGroup.getId(), metabaseCollection.getId());
}

public void createQuestionsForSubjectTypes() throws Exception{
databaseService.createQuestionsForSubjectTypes();
}

public int getGlobalDatabaseId() {
if (globalDatabase == null) {
Organisation currentOrganisation = organisationService.getCurrentOrganisation();
Expand All @@ -79,7 +75,7 @@ public int getGlobalDatabaseId() {
public int getGlobalCollectionId() {
if (globalCollection == null) {
Organisation currentOrganisation = organisationService.getCurrentOrganisation();
globalCollection = databaseRepository.getCollectionByName(currentOrganisation.getName() + " collection");
globalCollection = databaseRepository.getCollectionByName(currentOrganisation.getName());
}
return globalCollection.getIdAsInt();
}
Expand Down

0 comments on commit 05cf2ed

Please sign in to comment.