Skip to content

Commit

Permalink
#96 | Update search by concept logic
Browse files Browse the repository at this point in the history
  • Loading branch information
1t5j0y committed Apr 17, 2024
1 parent 5082614 commit 6d6bab6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,10 @@ public String getCountsByType() {
List<String> strings = grouped.entrySet().stream().map(x -> String.format("%s-%d", x.getKey(), x.getValue().size())).collect(Collectors.toList());
return String.join(";", strings);
}

public Optional<TableMetadata> findTableByForm(String formUuid) {
return this.tableMetadata.stream()
.filter(table -> table.getFormUuid() != null && table.getFormUuid().equals(formUuid))
.findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public List<ColumnMetadata> findColumnsMatchingConceptType(ColumnMetadata.Concep
return this.getColumnMetadataList().stream().filter(columnMetadata -> Arrays.stream(conceptTypes).anyMatch(conceptType -> nullSafeEquals(columnMetadata.getConceptType(), conceptType))).collect(Collectors.toList());
}

public Optional<ColumnMetadata> 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()
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/avniproject/etl/dto/ConceptFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public class ConceptFilter {
private String conceptUuid;
private String formUuid;
private List<String> values;
private String from;
private String to;
Expand All @@ -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<String> getValues() {
if (values == null) {
values = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,47 +37,30 @@ public class MediaTableRepository {

private List<ConceptFilterSearch> determineConceptFilterTablesAndColumns(List<ConceptFilter> conceptFilters) {
logger.debug("searching concepts: " + conceptFilters);
List<ConceptFilterSearch> conceptFilterTablesAndColumns = new ArrayList<>();
List<ColumnMetadata.ConceptType> textConceptSearchTypes = Arrays.asList(
ColumnMetadata.ConceptType.Text,
ColumnMetadata.ConceptType.Id,
ColumnMetadata.ConceptType.Notes
);
List<ColumnMetadata.ConceptType> 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<TableMetadata> 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<ColumnMetadata> 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<ConceptFilterSearch> 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;
}
Expand Down

0 comments on commit 6d6bab6

Please sign in to comment.