Search in sources :

Example 1 with DateTime

use of org.opencds.cqf.cql.engine.runtime.DateTime in project cqf-ruler by DBCG.

the class PrefetchDataProviderR4 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) {
    if (codePath == null && (codes != null || valueSet != null)) {
        throw new IllegalArgumentException("A code path must be provided when filtering on codes or a valueset.");
    }
    if (dataType == null) {
        throw new IllegalArgumentException("A data type (i.e. Procedure, Valueset, etc...) must be specified for clinical data retrieval");
    }
    // not be in the pre-fetch bundle, or might required a lookup by Id
    if (context.equals("Patient") && contextPath == null) {
        return Collections.emptyList();
    }
    List<Object> resourcesOfType = prefetchResources.get(dataType);
    if (resourcesOfType == null) {
        return Collections.emptyList();
    }
    // no resources or no filtering -> return list
    if (resourcesOfType.isEmpty() || (dateRange == null && codePath == null)) {
        return resourcesOfType;
    }
    List<Object> returnList = new ArrayList<>();
    for (Object resource : resourcesOfType) {
        boolean includeResource = true;
        if (dateRange != null) {
            if (datePath != null) {
                if (dateHighPath != null || dateLowPath != null) {
                    throw new IllegalArgumentException("If the datePath is specified, the dateLowPath and dateHighPath attributes must not be present.");
                }
                Object dateObject = PrefetchDataProviderHelper.getR4DateTime(this.resolver.resolvePath(resource, datePath));
                DateTime date = dateObject instanceof DateTime ? (DateTime) dateObject : null;
                Interval dateInterval = dateObject instanceof Interval ? (Interval) dateObject : null;
                String precision = PrefetchDataProviderHelper.getPrecision(Arrays.asList(dateRange, date));
                if (date != null && !(InEvaluator.in(date, dateRange, precision))) {
                    includeResource = false;
                } else // TODO - add precision to includes evaluator
                if (dateInterval != null && !(IncludesEvaluator.includes(dateRange, dateInterval, precision))) {
                    includeResource = false;
                }
            } else {
                if (dateHighPath == null && dateLowPath == null) {
                    throw new IllegalArgumentException("If the datePath is not given, either the lowDatePath or highDatePath must be provided.");
                }
                DateTime lowDate = dateLowPath == null ? null : (DateTime) PrefetchDataProviderHelper.getR4DateTime(this.resolver.resolvePath(resource, dateLowPath));
                DateTime highDate = dateHighPath == null ? null : (DateTime) PrefetchDataProviderHelper.getR4DateTime(this.resolver.resolvePath(resource, dateHighPath));
                String precision = PrefetchDataProviderHelper.getPrecision(Arrays.asList(dateRange, lowDate, highDate));
                Interval interval = new Interval(lowDate, true, highDate, true);
                // TODO - add precision to includes evaluator
                if (!IncludesEvaluator.includes(dateRange, interval, precision)) {
                    includeResource = false;
                }
            }
        }
        if (codePath != null && !codePath.equals("") && includeResource) {
            if (valueSet != null && terminologyProvider != null) {
                if (valueSet.startsWith("urn:oid:")) {
                    valueSet = valueSet.replace("urn:oid:", "");
                }
                ValueSetInfo valueSetInfo = new ValueSetInfo().withId(valueSet);
                codes = terminologyProvider.expand(valueSetInfo);
            }
            if (codes != null) {
                Object codeObject = PrefetchDataProviderHelper.getR4Code(this.resolver.resolvePath(resource, codePath));
                includeResource = PrefetchDataProviderHelper.checkCodeMembership(codes, codeObject, this.codeUtil);
            }
        }
        if (includeResource) {
            returnList.add(resource);
        }
    }
    return returnList;
}
Also used : ArrayList(java.util.ArrayList) ValueSetInfo(org.opencds.cqf.cql.engine.terminology.ValueSetInfo) DateTime(org.opencds.cqf.cql.engine.runtime.DateTime) Interval(org.opencds.cqf.cql.engine.runtime.Interval)

Example 2 with DateTime

use of org.opencds.cqf.cql.engine.runtime.DateTime in project cqf-ruler by DBCG.

the class PrefetchDataProviderStu3 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) {
    if (codePath == null && (codes != null || valueSet != null)) {
        throw new IllegalArgumentException("A code path must be provided when filtering on codes or a valueset.");
    }
    if (dataType == null) {
        throw new IllegalArgumentException("A data type (i.e. Procedure, Valueset, etc...) must be specified for clinical data retrieval");
    }
    // not be in the pre-fetch bundle, or might required a lookup by Id
    if (context.equals("Patient") && contextPath == null) {
        return Collections.emptyList();
    }
    List<Object> resourcesOfType = prefetchResources.get(dataType);
    if (resourcesOfType == null) {
        return Collections.emptyList();
    }
    // no resources or no filtering -> return list
    if (resourcesOfType.isEmpty() || (dateRange == null && codePath == null)) {
        return resourcesOfType;
    }
    List<Object> returnList = new ArrayList<>();
    for (Object resource : resourcesOfType) {
        boolean includeResource = true;
        if (dateRange != null) {
            if (datePath != null) {
                if (dateHighPath != null || dateLowPath != null) {
                    throw new IllegalArgumentException("If the datePath is specified, the dateLowPath and dateHighPath attributes must not be present.");
                }
                Object dateObject = PrefetchDataProviderHelper.getStu3DateTime(this.resolver.resolvePath(resource, datePath));
                DateTime date = dateObject instanceof DateTime ? (DateTime) dateObject : null;
                Interval dateInterval = dateObject instanceof Interval ? (Interval) dateObject : null;
                String precision = PrefetchDataProviderHelper.getPrecision(Arrays.asList(dateRange, date));
                if (date != null && !(InEvaluator.in(date, dateRange, precision))) {
                    includeResource = false;
                } else // TODO - add precision to includes evaluator
                if (dateInterval != null && !(IncludesEvaluator.includes(dateRange, dateInterval, precision))) {
                    includeResource = false;
                }
            } else {
                if (dateHighPath == null && dateLowPath == null) {
                    throw new IllegalArgumentException("If the datePath is not given, either the lowDatePath or highDatePath must be provided.");
                }
                DateTime lowDate = dateLowPath == null ? null : (DateTime) PrefetchDataProviderHelper.getStu3DateTime(this.resolver.resolvePath(resource, dateLowPath));
                DateTime highDate = dateHighPath == null ? null : (DateTime) PrefetchDataProviderHelper.getStu3DateTime(this.resolver.resolvePath(resource, dateHighPath));
                String precision = PrefetchDataProviderHelper.getPrecision(Arrays.asList(dateRange, lowDate, highDate));
                Interval interval = new Interval(lowDate, true, highDate, true);
                // TODO - add precision to includes evaluator
                if (!IncludesEvaluator.includes(dateRange, interval, precision)) {
                    includeResource = false;
                }
            }
        }
        if (codePath != null && !codePath.equals("") && includeResource) {
            if (valueSet != null && terminologyProvider != null) {
                if (valueSet.startsWith("urn:oid:")) {
                    valueSet = valueSet.replace("urn:oid:", "");
                }
                ValueSetInfo valueSetInfo = new ValueSetInfo().withId(valueSet);
                codes = terminologyProvider.expand(valueSetInfo);
            }
            if (codes != null) {
                Object codeObject = PrefetchDataProviderHelper.getStu3Code(this.resolver.resolvePath(resource, codePath));
                includeResource = PrefetchDataProviderHelper.checkCodeMembership(codes, codeObject, this.codeUtil);
            }
        }
        if (includeResource) {
            returnList.add(resource);
        }
    }
    return returnList;
}
Also used : ArrayList(java.util.ArrayList) ValueSetInfo(org.opencds.cqf.cql.engine.terminology.ValueSetInfo) DateTime(org.opencds.cqf.cql.engine.runtime.DateTime) Interval(org.opencds.cqf.cql.engine.runtime.Interval)

Example 3 with DateTime

use of org.opencds.cqf.cql.engine.runtime.DateTime in project cqf-ruler by DBCG.

the class PrefetchDataProviderDstu2 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) {
    if (codePath == null && (codes != null || valueSet != null)) {
        throw new IllegalArgumentException("A code path must be provided when filtering on codes or a valueset.");
    }
    if (dataType == null) {
        throw new IllegalArgumentException("A data type (i.e. Procedure, Valueset, etc...) must be specified for clinical data retrieval");
    }
    // not be in the pre-fetch bundle, or might required a lookup by Id
    if (context.equals("Patient") && contextPath == null) {
        return Collections.emptyList();
    }
    List<Object> resourcesOfType = prefetchResources.get(dataType);
    if (resourcesOfType == null) {
        return Collections.emptyList();
    }
    // no resources or no filtering -> return list
    if (resourcesOfType.isEmpty() || (dateRange == null && codePath == null)) {
        return resourcesOfType;
    }
    List<Object> returnList = new ArrayList<>();
    for (Object resource : resourcesOfType) {
        boolean includeResource = true;
        if (dateRange != null) {
            if (datePath != null) {
                if (dateHighPath != null || dateLowPath != null) {
                    throw new IllegalArgumentException("If the datePath is specified, the dateLowPath and dateHighPath attributes must not be present.");
                }
                Object dateObject = PrefetchDataProviderHelper.getDstu2DateTime(this.resolver.resolvePath(resource, datePath));
                DateTime date = dateObject instanceof DateTime ? (DateTime) dateObject : null;
                Interval dateInterval = dateObject instanceof Interval ? (Interval) dateObject : null;
                String precision = PrefetchDataProviderHelper.getPrecision(Arrays.asList(dateRange, date));
                if (date != null && !(InEvaluator.in(date, dateRange, precision))) {
                    includeResource = false;
                } else // TODO - add precision to includes evaluator
                if (dateInterval != null && !(IncludesEvaluator.includes(dateRange, dateInterval, precision))) {
                    includeResource = false;
                }
            } else {
                if (dateHighPath == null && dateLowPath == null) {
                    throw new IllegalArgumentException("If the datePath is not given, either the lowDatePath or highDatePath must be provided.");
                }
                DateTime lowDate = dateLowPath == null ? null : (DateTime) PrefetchDataProviderHelper.getDstu2DateTime(this.resolver.resolvePath(resource, dateLowPath));
                DateTime highDate = dateHighPath == null ? null : (DateTime) PrefetchDataProviderHelper.getDstu2DateTime(this.resolver.resolvePath(resource, dateHighPath));
                String precision = PrefetchDataProviderHelper.getPrecision(Arrays.asList(dateRange, lowDate, highDate));
                Interval interval = new Interval(lowDate, true, highDate, true);
                // TODO - add precision to includes evaluator
                if (!IncludesEvaluator.includes(dateRange, interval, precision)) {
                    includeResource = false;
                }
            }
        }
        if (codePath != null && !codePath.equals("") && includeResource) {
            if (valueSet != null && terminologyProvider != null) {
                if (valueSet.startsWith("urn:oid:")) {
                    valueSet = valueSet.replace("urn:oid:", "");
                }
                ValueSetInfo valueSetInfo = new ValueSetInfo().withId(valueSet);
                codes = terminologyProvider.expand(valueSetInfo);
            }
            if (codes != null) {
                Object codeObject = PrefetchDataProviderHelper.getDstu2Code(this.resolver.resolvePath(resource, codePath));
                includeResource = PrefetchDataProviderHelper.checkCodeMembership(codes, codeObject, this.codeUtil);
            }
        }
        if (includeResource) {
            returnList.add(resource);
        }
    }
    return returnList;
}
Also used : ArrayList(java.util.ArrayList) ValueSetInfo(org.opencds.cqf.cql.engine.terminology.ValueSetInfo) DateTime(org.opencds.cqf.cql.engine.runtime.DateTime) Interval(org.opencds.cqf.cql.engine.runtime.Interval)

Example 4 with DateTime

use of org.opencds.cqf.cql.engine.runtime.DateTime in project cqf-ruler by DBCG.

the class ActivityDefinitionApplyProvider method resolveActivityDefinition.

// For library use
public Resource resolveActivityDefinition(ActivityDefinition activityDefinition, String patientId, String practitionerId, String organizationId, RequestDetails theRequest) throws FHIRException {
    Resource result = newResource(activityDefinition.getKind().toCode());
    switch(result.fhirType()) {
        case "ServiceRequest":
            result = resolveServiceRequest(activityDefinition, patientId, practitionerId, organizationId);
            break;
        case "MedicationRequest":
            result = resolveMedicationRequest(activityDefinition, patientId);
            break;
        case "SupplyRequest":
            result = resolveSupplyRequest(activityDefinition, practitionerId, organizationId);
            break;
        case "Procedure":
            result = resolveProcedure(activityDefinition, patientId);
            break;
        case "DiagnosticReport":
            result = resolveDiagnosticReport(activityDefinition, patientId);
            break;
        case "Communication":
            result = resolveCommunication(activityDefinition, patientId);
            break;
        case "CommunicationRequest":
            result = resolveCommunicationRequest(activityDefinition, patientId);
            break;
        case "Task":
            result = resolveTask(activityDefinition, patientId);
            break;
    }
    for (ActivityDefinition.ActivityDefinitionDynamicValueComponent dynamicValue : activityDefinition.getDynamicValue()) {
        // ActivityDefinition apply operation does not fail.
        try {
            if (dynamicValue.getExpression() != null) {
                // Special case for setting a Patient reference
                if ("Patient".equals(dynamicValue.getExpression().getExpression())) {
                    this.modelResolver.setValue(result, dynamicValue.getPath(), new Reference(patientId));
                } else {
                    /*
						 * TODO: Passing the activityDefinition as context here because that's what will
						 * have the libraries, but perhaps the "context" here should be the result
						 * resource?
						 */
                    Object value = expressionEvaluation.evaluateInContext(activityDefinition, dynamicValue.getExpression().getExpression(), patientId, theRequest);
                    if (value != null) {
                        logger.debug("dynamicValue value: {}", value);
                        if (value instanceof Boolean) {
                            value = new BooleanType((Boolean) value);
                        } else if (value instanceof DateTime) {
                            value = Date.from(((DateTime) value).getDateTime().toInstant());
                        } else if (value instanceof String) {
                            value = new StringType((String) value);
                        }
                        this.modelResolver.setValue(result, dynamicValue.getPath(), value);
                    } else {
                        logger.warn("WARNING: ActivityDefinition has null value for path: {}", dynamicValue.getPath());
                    }
                }
            }
        } catch (Exception e) {
            logger.error("ERROR: ActivityDefinition dynamicValue {} could not be applied and threw exception {}", dynamicValue.getPath(), e.toString());
            logger.error(e.toString());
        }
    }
    return result;
}
Also used : StringType(org.hl7.fhir.r4.model.StringType) Reference(org.hl7.fhir.r4.model.Reference) Resource(org.hl7.fhir.r4.model.Resource) BooleanType(org.hl7.fhir.r4.model.BooleanType) DateTime(org.opencds.cqf.cql.engine.runtime.DateTime) InternalErrorException(ca.uhn.fhir.rest.server.exceptions.InternalErrorException) FHIRException(org.hl7.fhir.exceptions.FHIRException) ActivityDefinition(org.hl7.fhir.r4.model.ActivityDefinition)

Example 5 with DateTime

use of org.opencds.cqf.cql.engine.runtime.DateTime in project quality-measure-and-cohort-service by Alvearie.

the class MeasurementPeriodHelperTest method testPeriodEndAdditionalPrecision__returnsEndOfDay.

@Test
public void testPeriodEndAdditionalPrecision__returnsEndOfDay() {
    String input = "2014-07-20T14:45:45.9";
    DateTime expected = new DateTime("2014-07-20T23:59:59.999", ZoneOffset.UTC);
    assertTrue(expected.equal(MeasurementPeriodHelper.getPeriodEnd(input)));
}
Also used : DateTime(org.opencds.cqf.cql.engine.runtime.DateTime) Test(org.junit.Test)

Aggregations

DateTime (org.opencds.cqf.cql.engine.runtime.DateTime)23 Test (org.junit.Test)16 OffsetDateTime (java.time.OffsetDateTime)6 Interval (org.opencds.cqf.cql.engine.runtime.Interval)6 LocalDateTime (java.time.LocalDateTime)3 ArrayList (java.util.ArrayList)3 ValueSetInfo (org.opencds.cqf.cql.engine.terminology.ValueSetInfo)3 ZoneOffset (java.time.ZoneOffset)2 IBaseDatatype (org.hl7.fhir.instance.model.api.IBaseDatatype)2 DateTimeType (org.hl7.fhir.r4.model.DateTimeType)2 Period (org.hl7.fhir.r4.model.Period)2 InternalErrorException (ca.uhn.fhir.rest.server.exceptions.InternalErrorException)1 CompositeCqlDataProvider (com.ibm.cohort.cql.data.CompositeCqlDataProvider)1 CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)1 CqlEvaluationResult (com.ibm.cohort.cql.evaluation.CqlEvaluationResult)1 CqlEvaluator (com.ibm.cohort.cql.evaluation.CqlEvaluator)1 DateParameter (com.ibm.cohort.cql.evaluation.parameters.DateParameter)1 DatetimeParameter (com.ibm.cohort.cql.evaluation.parameters.DatetimeParameter)1 ClasspathCqlLibraryProvider (com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider)1 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)1