Search in sources :

Example 1 with CodeKey

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CodeKey(com.ibm.cohort.datarow.model.CodeKey) DataRow(com.ibm.cohort.datarow.model.DataRow) Code(org.opencds.cqf.cql.engine.runtime.Code) ValueSetInfo(org.opencds.cqf.cql.engine.terminology.ValueSetInfo) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

CodeKey (com.ibm.cohort.datarow.model.CodeKey)1 DataRow (com.ibm.cohort.datarow.model.DataRow)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Code (org.opencds.cqf.cql.engine.runtime.Code)1 ValueSetInfo (org.opencds.cqf.cql.engine.terminology.ValueSetInfo)1