diff --git a/src/main/java/org/avniproject/etl/domain/metadata/SchemaMetadata.java b/src/main/java/org/avniproject/etl/domain/metadata/SchemaMetadata.java index 48c6b0e..9521470 100644 --- a/src/main/java/org/avniproject/etl/domain/metadata/SchemaMetadata.java +++ b/src/main/java/org/avniproject/etl/domain/metadata/SchemaMetadata.java @@ -115,4 +115,10 @@ public String getCountsByType() { List strings = grouped.entrySet().stream().map(x -> String.format("%s-%d", x.getKey(), x.getValue().size())).collect(Collectors.toList()); return String.join(";", strings); } + + public Optional findTableByForm(String formUuid) { + return this.tableMetadata.stream() + .filter(table -> table.getFormUuid() != null && table.getFormUuid().equals(formUuid)) + .findFirst(); + } } diff --git a/src/main/java/org/avniproject/etl/domain/metadata/TableMetadata.java b/src/main/java/org/avniproject/etl/domain/metadata/TableMetadata.java index 0bbc1f5..a45f97e 100644 --- a/src/main/java/org/avniproject/etl/domain/metadata/TableMetadata.java +++ b/src/main/java/org/avniproject/etl/domain/metadata/TableMetadata.java @@ -92,6 +92,10 @@ public List findColumnsMatchingConceptType(ColumnMetadata.Concep return this.getColumnMetadataList().stream().filter(columnMetadata -> Arrays.stream(conceptTypes).anyMatch(conceptType -> nullSafeEquals(columnMetadata.getConceptType(), conceptType))).collect(Collectors.toList()); } + public Optional findColumnMatchingConcept(String conceptUuid) { + return this.getColumnMetadataList().stream().filter(columnMetadata -> columnMetadata.getConceptUuid() != null && columnMetadata.getConceptUuid().equals(conceptUuid)).findFirst(); + } + public void mergeWith(TableMetadata oldTableMetadata) { setId(oldTableMetadata.getId()); getColumnMetadataList() diff --git a/src/main/java/org/avniproject/etl/dto/ConceptFilter.java b/src/main/java/org/avniproject/etl/dto/ConceptFilter.java index 97040dc..e3386d6 100644 --- a/src/main/java/org/avniproject/etl/dto/ConceptFilter.java +++ b/src/main/java/org/avniproject/etl/dto/ConceptFilter.java @@ -5,6 +5,7 @@ public class ConceptFilter { private String conceptUuid; + private String formUuid; private List values; private String from; private String to; @@ -17,6 +18,14 @@ public void setConceptUuid(String conceptUuid) { this.conceptUuid = conceptUuid; } + public String getFormUuid() { + return formUuid; + } + + public void setFormUuid(String formUuid) { + this.formUuid = formUuid; + } + public List getValues() { if (values == null) { values = new ArrayList<>(); diff --git a/src/main/java/org/avniproject/etl/repository/MediaTableRepository.java b/src/main/java/org/avniproject/etl/repository/MediaTableRepository.java index e618b74..2d119a8 100644 --- a/src/main/java/org/avniproject/etl/repository/MediaTableRepository.java +++ b/src/main/java/org/avniproject/etl/repository/MediaTableRepository.java @@ -17,7 +17,6 @@ import org.springframework.stereotype.Repository; import java.util.*; -import java.util.stream.Stream; import static org.avniproject.etl.repository.JdbcContextWrapper.runInSchemaUserContext; @@ -38,47 +37,30 @@ public class MediaTableRepository { private List determineConceptFilterTablesAndColumns(List conceptFilters) { logger.debug("searching concepts: " + conceptFilters); - List conceptFilterTablesAndColumns = new ArrayList<>(); List textConceptSearchTypes = Arrays.asList( ColumnMetadata.ConceptType.Text, ColumnMetadata.ConceptType.Id, ColumnMetadata.ConceptType.Notes ); - List supportedConceptSearchTypes = new ArrayList<>(Arrays.asList( - ColumnMetadata.ConceptType.Numeric, - ColumnMetadata.ConceptType.Date, - ColumnMetadata.ConceptType.SingleSelect, - ColumnMetadata.ConceptType.MultiSelect - )); - supportedConceptSearchTypes.addAll(textConceptSearchTypes); SchemaMetadata schema = schemaMetadataRepository.getExistingSchemaMetadata(); - List tablesToSearch = Stream.of(schema.getAllSubjectTables(), - schema.getAllProgramEnrolmentTables(), - schema.getAllProgramEncounterTables(), - schema.getAllEncounterTables()) - .flatMap(Collection::stream) - .toList(); - logger.debug("Searching tables: " + tablesToSearch); - for (ConceptFilter conceptFilter : conceptFilters) { - String conceptUuid = conceptFilter.getConceptUuid(); - for (TableMetadata tableMetadata : tablesToSearch) { - Optional column = tableMetadata.getColumnMetadataList() - .stream() - .filter(columnMetadata -> Objects.equals(columnMetadata.getConceptUuid(), conceptUuid) - && supportedConceptSearchTypes.contains(columnMetadata.getConceptType()) - ).findFirst(); - if (column.isPresent()) { - ColumnMetadata columnMetadata = column.get(); - conceptFilterTablesAndColumns.add(new ConceptFilterSearch(tableMetadata.getName(), - columnMetadata.getName(), conceptFilter.getValues(), - conceptFilter.getFrom(), conceptFilter.getTo(), - columnMetadata.getConceptType().equals(ColumnMetadata.ConceptType.Numeric), - !textConceptSearchTypes.contains(columnMetadata.getConceptType()))); - break; - } - } - } + + List conceptFilterTablesAndColumns = conceptFilters.stream().map(conceptFilter -> { + TableMetadata table = schema.findTableByForm(conceptFilter.getFormUuid()).orElse(null); + if (table != null) { + ColumnMetadata column = table.findColumnMatchingConcept(conceptFilter.getConceptUuid()).orElse(null); + return column != null ? new ConceptFilterSearch(table.getName(), + column.getName(), + conceptFilter.getValues(), + conceptFilter.getFrom(), + conceptFilter.getTo(), + column.getConceptType().equals(ColumnMetadata.ConceptType.Numeric), + !textConceptSearchTypes.contains(column.getConceptType())) + : null; + } + return null; + }).toList(); + logger.debug("Returning conceptFilterTablesAndColumns: " + conceptFilterTablesAndColumns); return conceptFilterTablesAndColumns; }