Skip to content

Commit

Permalink
avniproject#762 | Proper object for field details created
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhardwajj committed Aug 31, 2024
1 parent 6b4edfb commit b456232
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.avni.server.dao.metabase;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import org.avni.server.domain.metabase.Database;
import org.avni.server.domain.metabase.FieldDetails;
import org.avni.server.domain.metabase.MetabaseDatabaseInfo;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Repository;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

@Repository
public class DatabaseRepository extends MetabaseConnector {
private final ObjectMapper objectMapper;
Expand All @@ -32,9 +36,14 @@ public MetabaseDatabaseInfo getDatabaseDetails(int databaseId) {
}
}

public JsonNode getFields(int databaseId) {
public List<FieldDetails> getFields(int databaseId) {
String url = metabaseApiUrl + "/database/" + databaseId + "/fields";
return getForObject(url, JsonNode.class);
String jsonResponse = getForObject(url, String.class);
try {
return objectMapper.readValue(jsonResponse, new TypeReference<List<FieldDetails>>() {});
} catch (Exception e) {
throw new RuntimeException("Failed to parse fields", e);
}
}

public JsonNode getInitialSyncStatus(int databaseId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.avni.server.domain.metabase;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class FieldDetails {

@JsonProperty("id")
private int id;

@JsonProperty("name")
private String name;

@JsonProperty("display_name")
private String displayName;

@JsonProperty("base_type")
private String baseType;

@JsonProperty("semantic_type")
private String semanticType;

@JsonProperty("table_name")
private String tableName;

@JsonProperty("schema")
private String schema;

public int getId() {
return id;
}

public String getName() {
return name;
}

public String getDisplayName() {
return displayName;
}

public String getBaseType() {
return baseType;
}

public String getSemanticType() {
return semanticType;
}

public String getTableName() {
return tableName;
}


public String getSchema() {
return schema;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ private String createRequestBodyForDataset(int sourceTableId) {
}

public int getFieldIdByTableNameAndFieldName(String tableName, String fieldName) {
JsonNode fieldsArray = databaseRepository.getFields(getDatabaseId());
List<FieldDetails> fieldsList = databaseRepository.getFields(getDatabaseId());
String snakeCaseTableName = S.toSnakeCase(tableName);
for (JsonNode fieldNode : fieldsArray) {
if (snakeCaseTableName.equals(fieldNode.path("table_name").asText()) && fieldName.equals(fieldNode.path("name").asText())) {
return fieldNode.path("id").asInt();

for (FieldDetails field : fieldsList) {
if (snakeCaseTableName.equals(field.getTableName()) && fieldName.equals(field.getName())) {
return field.getId();
}
}
return -1;
Expand Down

0 comments on commit b456232

Please sign in to comment.