Skip to content

Commit

Permalink
#762 | 1) Moved AddressQuestionCreationService logic to DatabaseServi…
Browse files Browse the repository at this point in the history
…ce 2) Replaced getSubjectTypeNames usage of int literals with constants depictiing columnsNames 3)Created common method called by createQuestionsForSubjectTypes() and createQuestionsForPrograms(), createQuestionsForProgramsEncounters() and createQuestionsForEncounters() with List entityNames 4)Added CollectionItem , Now before creating questions, the existing ones are filtered out
  • Loading branch information
ombhardwajj committed Sep 11, 2024
1 parent d63b147 commit f621d61
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Repository;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

@Repository
Expand Down Expand Up @@ -47,6 +49,27 @@ public Database getDatabaseByName(Database database) {
}
}

public List<CollectionItem> getExistingCollectionItems(int collectionId) {
String url = metabaseApiUrl + "/collection/" + collectionId + "/items";
String jsonResponse = getForObject(url, String.class);

try {
JsonNode rootNode = objectMapper.readTree(jsonResponse);
JsonNode dataArray = rootNode.path("data");

List<CollectionItem> items = new ArrayList<>();
for (JsonNode itemNode : dataArray) {
CollectionItem item = new CollectionItem();
item.setName(itemNode.get("name").asText());
item.setId(itemNode.get("id").asInt());
items.add(item);
}
return items;
} catch (Exception e) {
throw new RuntimeException("Failed to fetch collection items", e);
}
}

public CollectionInfoResponse getCollectionByName(Database database) {
String url = metabaseApiUrl + "/collection";
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.avni.server.domain.metabase;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class CollectionItem {
private String name;
private int id;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;

public enum FieldAttribute {
ALL("all"),
FIELD("field"),
FIELDS("fields"),
SOURCE_TABLE("source-table"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MetabaseQueryBuilder forTable(TableDetails tableDetails) {

public MetabaseQueryBuilder joinWith(TableDetails addressTable, FieldDetails joinField1, FieldDetails joinField2) {
ObjectNode joinNode = objectMapper.createObjectNode();
joinNode.put(FieldAttribute.FIELDS.getAttributeName(), "all");
joinNode.put(FieldAttribute.FIELDS.getAttributeName(), FieldAttribute.ALL.getAttributeName());
joinNode.put(FieldAttribute.ALIAS.getAttributeName(), addressTable.getName());

ArrayNode conditionArray = objectMapper.createArrayNode();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DatabaseService {
public class DatabaseService implements QuestionCreationService{

private final DatabaseRepository databaseRepository;
private final MetabaseService metabaseService;
private final AddressQuestionCreationService addressQuestionCreationService;

private static final String ADDRESS_TABLE = "Address";

@Autowired
public DatabaseService(DatabaseRepository databaseRepository, MetabaseService metabaseService, AddressQuestionCreationService addressQuestionCreationService) {
public DatabaseService(DatabaseRepository databaseRepository, MetabaseService metabaseService) {
this.databaseRepository = databaseRepository;
this.metabaseService = metabaseService;
this.addressQuestionCreationService = addressQuestionCreationService;
}

public Database getGlobalDatabase() {
Expand All @@ -45,6 +45,33 @@ private void ensureSyncComplete() {
}
}

private List<String> filterOutExistingQuestions(List<String> entityNames) {
Set<String> existingItemNames = databaseRepository.getExistingCollectionItems(getGlobalCollection().getIdAsInt()).stream()
.map(item -> item.getName().trim().toLowerCase().replace(" ", "_"))
.collect(Collectors.toSet());

return entityNames.stream()
.filter(entityName -> !existingItemNames.contains(entityName.toLowerCase()))
.collect(Collectors.toList());
}

@Override
public void createQuestionForTable(TableDetails tableDetails, TableDetails addressTableDetails, FieldDetails addressFieldDetails, FieldDetails tableFieldDetails) {
Database database = getGlobalDatabase();
databaseRepository.createQuestionForTable(database, tableDetails, addressTableDetails, addressFieldDetails, tableFieldDetails);
}

@Override
public void createQuestionForTable(String tableName, String schema) {
Database database = getGlobalDatabase();

TableDetails tableDetails = new TableDetails();
tableDetails.setName(tableName);
TableDetails fetchedTableDetails = databaseRepository.findTableDetailsByName(database, tableDetails);

databaseRepository.createQuestionForASingleTable(database, fetchedTableDetails);
}

public List<String> getSubjectTypeNames() {
TableDetails fetchedMetadataTable = databaseRepository.findTableDetailsByName(getGlobalDatabase(), new TableDetails("table_metadata"));

Expand Down Expand Up @@ -86,49 +113,42 @@ public List<String> getProgramAndEncounterNames() {
return programNames;
}

public void createQuestionsForSubjectTypes(){
private void createQuestionsForEntities(List<String> entityNames, FieldDetails addressFieldDetails, FieldDetails entityFieldDetails) {
ensureSyncComplete();
List<String> subjectTypeNames = getSubjectTypeNames();
TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(getGlobalDatabase(), new TableDetails(ADDRESS_TABLE));

List<String> filteredEntities = filterOutExistingQuestions(entityNames);

TableDetails addressTableDetails = new TableDetails(ADDRESS_TABLE);
TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(getGlobalDatabase(), addressTableDetails);
for (String entityName : filteredEntities) {
TableDetails entityTableDetails = new TableDetails();
entityTableDetails.setName(entityName);
TableDetails fetchedEntityTableDetails = databaseRepository.findTableDetailsByName(getGlobalDatabase(), entityTableDetails);
createQuestionForTable(fetchedEntityTableDetails, fetchedAddressTableDetails, addressFieldDetails, entityFieldDetails);
}
}

public void createQuestionsForSubjectTypes() {
List<String> subjectTypeNames = getSubjectTypeNames();
FieldDetails addressFieldDetails = new FieldDetails("id");
FieldDetails subjectFieldDetails = new FieldDetails("address_id");


for (String subjectTypeName : subjectTypeNames) {
TableDetails subjectTableDetails = new TableDetails();
subjectTableDetails.setName(subjectTypeName);
TableDetails fetchedSubjectTableDetails = databaseRepository.findTableDetailsByName(getGlobalDatabase(), subjectTableDetails);
addressQuestionCreationService.createQuestionForTable(fetchedSubjectTableDetails, fetchedAddressTableDetails, addressFieldDetails, subjectFieldDetails);
}
createQuestionsForEntities(subjectTypeNames, addressFieldDetails, subjectFieldDetails);
}

public void createQuestionsForProgramsAndEncounters(){
ensureSyncComplete();
public void createQuestionsForProgramsAndEncounters() {
List<String> programAndEncounterNames = getProgramAndEncounterNames();

TableDetails fetchedAddressTableDetails = databaseRepository.findTableDetailsByName(getGlobalDatabase(), new TableDetails(ADDRESS_TABLE));

FieldDetails addressFieldDetails = new FieldDetails("id");
FieldDetails programFieldDetails = new FieldDetails("address_id");

for (String programName : programAndEncounterNames) {
TableDetails programTableDetails = new TableDetails();
programTableDetails.setName(programName);
TableDetails fetchedProgramTableDetails = databaseRepository.findTableDetailsByName(getGlobalDatabase(), programTableDetails);
addressQuestionCreationService.createQuestionForTable(fetchedProgramTableDetails, fetchedAddressTableDetails, addressFieldDetails, programFieldDetails);
}
FieldDetails programOrEncounterFieldDetails = new FieldDetails("address_id");
createQuestionsForEntities(programAndEncounterNames, addressFieldDetails, programOrEncounterFieldDetails);
}

public void createQuestionsForIndividualTables(){
public void createQuestionsForIndividualTables() {
ensureSyncComplete();

List<String> individualTables = Arrays.asList("address", "media", "sync_telemetry");

for (String tableName : individualTables) {
addressQuestionCreationService.createQuestionForTable(tableName, "!public");
List<String> filteredTables = filterOutExistingQuestions(individualTables);

for (String tableName : filteredTables) {
createQuestionForTable(tableName, "!public");
}
}

Expand Down

0 comments on commit f621d61

Please sign in to comment.