use of com.ibm.cohort.datarow.model.CodeKey in project quality-measure-and-cohort-service by Alvearie.
the class DataRowRetrieveProvider method retrieve.
@Override
public Iterable<Object> retrieve(String context, String contextPath, Object contextValue, String dataType, String templateId, String codePath, Iterable<Code> codes, String valueSet, String datePath, String dateLowPath, String dateHighPath, Interval dateRange) {
Iterable<Object> result;
// Fast fail for an unsupported operation scenario
if (datePath != null || dateLowPath != null || dateHighPath != null) {
throw new UnsupportedOperationException("Date-based filtering is not supported at this time.");
}
Iterable<Object> allRows = data.get(dataType);
if (codePath != null) {
// Calculate an index of code to matching rows based on the dataType and
// codePath
Map<String, Map<Object, List<Object>>> codePathToCodeMap = indexes.computeIfAbsent(dataType, key -> new HashMap<>());
Map<Object, List<Object>> indexedRows = codePathToCodeMap.computeIfAbsent(codePath, key -> {
Map<Object, List<Object>> codeMap = new HashMap<>();
if (allRows != null) {
for (Object obj : allRows) {
DataRow row = (DataRow) obj;
Object code = row.getValue(codePath);
if (code != null) {
if (code instanceof Code) {
code = new CodeKey((Code) code);
} else {
code = new CodeKey().withCode(String.valueOf(code));
}
List<Object> list = codeMap.computeIfAbsent(code, codeKey -> new ArrayList<>());
list.add(row);
}
}
}
return codeMap;
});
if (valueSet != null) {
// expand the valueset into codes
ValueSetInfo valueSetInfo = new ValueSetInfo().withId(valueSet);
codes = terminologyProvider.expand(valueSetInfo);
}
if (codes != null) {
List<Object> allMatches = new ArrayList<>();
for (Code codeToCheck : codes) {
CodeKey indexKey = new CodeKey(codeToCheck);
List<Object> matches = indexedRows.get(indexKey);
if (matches != null) {
allMatches.addAll(matches);
}
}
result = allMatches;
} else {
throw new IllegalArgumentException(String.format("No codes found for filtered retrieve of dataType %s, codePath %s", dataType, codePath));
}
} else {
result = (allRows != null) ? allRows : Collections.emptyList();
}
return result;
}
Aggregations