Search in sources :

Example 1 with RequestGroupActionBuilder

use of org.opencds.cqf.ruler.cr.dstu3.builder.RequestGroupActionBuilder in project cqf-ruler by DBCG.

the class Session method resolveActions.

private void resolveActions(List<PlanDefinition.PlanDefinitionActionComponent> actions, Context context, String patientId, RequestGroupBuilder requestGroupBuilder, List<RequestGroup.RequestGroupActionComponent> actionComponents, RequestDetails theRequest) {
    for (PlanDefinition.PlanDefinitionActionComponent action : actions) {
        boolean conditionsMet = true;
        for (PlanDefinition.PlanDefinitionActionConditionComponent condition : action.getCondition()) {
            if (condition.getKind() == PlanDefinition.ActionConditionKind.APPLICABILITY) {
                if (!condition.hasExpression()) {
                    continue;
                }
                if (condition.hasExpression() && !condition.getExpression().hasExpression()) {
                    continue;
                }
                Object result = context.resolveExpressionRef(condition.getExpression().getExpression()).getExpression().evaluate(context);
                if (!(result instanceof Boolean)) {
                    continue;
                }
                if (!(Boolean) result) {
                    conditionsMet = false;
                }
            }
            if (conditionsMet) {
                RequestGroupActionBuilder actionBuilder = new RequestGroupActionBuilder();
                if (action.hasTitle()) {
                    actionBuilder.buildTitle(action.getTitle());
                }
                if (action.hasDescription()) {
                    actionBuilder.buildDescripition(action.getDescription());
                }
                // source
                if (action.hasDocumentation()) {
                    RelatedArtifact artifact = action.getDocumentationFirstRep();
                    RelatedArtifactBuilder artifactBuilder = new RelatedArtifactBuilder();
                    if (artifact.hasDisplay()) {
                        artifactBuilder.buildDisplay(artifact.getDisplay());
                    }
                    if (artifact.hasUrl()) {
                        artifactBuilder.buildUrl(artifact.getUrl());
                    }
                    if (artifact.hasDocument() && artifact.getDocument().hasUrl()) {
                        AttachmentBuilder attachmentBuilder = new AttachmentBuilder();
                        attachmentBuilder.buildUrl(artifact.getDocument().getUrl());
                        artifactBuilder.buildDocument(attachmentBuilder.build());
                    }
                    actionBuilder.buildDocumentation(Collections.singletonList(artifactBuilder.build()));
                }
                // TODO - uuid
                if (action.hasPrefix()) {
                    actionBuilder.buildPrefix(action.getPrefix());
                }
                if (action.hasType()) {
                    actionBuilder.buildType(action.getType());
                }
                if (action.hasDefinition()) {
                    if (action.getDefinitionCanonicalType().getValue().contains("ActivityDefinition")) {
                        ActivityDefinition activityDefinition = this.activityDefinitionDao.read(new IdType("ActivityDefinition", action.getDefinitionCanonicalType().getId()));
                        if (activityDefinition.hasDescription()) {
                            actionBuilder.buildDescripition(activityDefinition.getDescription());
                        }
                        try {
                            this.activityDefinitionApplyProvider.apply(theRequest, new IdType(action.getDefinitionCanonicalType().getId()), patientId, null, null, null, null, null, null, null, null).setId(UUID.randomUUID().toString());
                        } catch (FHIRException e) {
                            throw new RuntimeException("Error applying ActivityDefinition " + e.getMessage());
                        }
                        Parameters inParams = new Parameters();
                        inParams.addParameter().setName("patient").setValue(new StringType(patientId));
                        Parameters outParams = this.fhirContext.newRestfulGenericClient(theRequest.getFhirServerBase()).operation().onInstance(new IdDt("ActivityDefinition", action.getDefinition().getId())).named("$apply").withParameters(inParams).useHttpGet().execute();
                        List<Parameters.ParametersParameterComponent> response = outParams.getParameter();
                        Resource resource = response.get(0).getResource().setId(UUID.randomUUID().toString());
                        actionBuilder.buildResourceTarget(resource);
                        actionBuilder.buildResource(new ReferenceBuilder().buildReference(resource.getId()).build());
                    }
                }
                // on here...
                if (action.hasDynamicValue()) {
                    for (PlanDefinition.PlanDefinitionActionDynamicValueComponent dynamicValue : action.getDynamicValue()) {
                        if (dynamicValue.hasPath() && dynamicValue.hasExpression()) {
                            if (dynamicValue.getPath().endsWith("title")) {
                                // summary
                                String title = (String) context.resolveExpressionRef(dynamicValue.getExpression().getExpression()).evaluate(context);
                                actionBuilder.buildTitle(title);
                            } else if (dynamicValue.getPath().endsWith("description")) {
                                // detail
                                String description = (String) context.resolveExpressionRef(dynamicValue.getExpression().getExpression()).evaluate(context);
                                actionBuilder.buildDescripition(description);
                            } else if (dynamicValue.getPath().endsWith("extension")) {
                                // indicator
                                String extension = (String) context.resolveExpressionRef(dynamicValue.getExpression().getExpression()).evaluate(context);
                                actionBuilder.buildExtension(extension);
                            }
                        }
                    }
                }
                if (!actionBuilder.build().isEmpty()) {
                    actionComponents.add(actionBuilder.build());
                }
                if (action.hasAction()) {
                    resolveActions(action.getAction(), context, patientId, requestGroupBuilder, actionComponents, theRequest);
                }
            }
        }
    }
    requestGroupBuilder.buildAction(new ArrayList<>(actionComponents));
}
Also used : Parameters(org.hl7.fhir.r4.model.Parameters) StringType(org.hl7.fhir.r4.model.StringType) Resource(org.hl7.fhir.r4.model.Resource) DomainResource(org.hl7.fhir.r4.model.DomainResource) IdDt(ca.uhn.fhir.model.primitive.IdDt) RelatedArtifact(org.hl7.fhir.r4.model.RelatedArtifact) FHIRException(org.hl7.fhir.exceptions.FHIRException) IdType(org.hl7.fhir.r4.model.IdType) RequestGroupActionBuilder(org.opencds.cqf.ruler.cr.r4.builder.RequestGroupActionBuilder) AttachmentBuilder(org.opencds.cqf.ruler.cr.r4.builder.AttachmentBuilder) ReferenceBuilder(org.opencds.cqf.ruler.cr.r4.builder.ReferenceBuilder) PlanDefinition(org.hl7.fhir.r4.model.PlanDefinition) RelatedArtifactBuilder(org.opencds.cqf.ruler.cr.r4.builder.RelatedArtifactBuilder) ActivityDefinition(org.hl7.fhir.r4.model.ActivityDefinition)

Example 2 with RequestGroupActionBuilder

use of org.opencds.cqf.ruler.cr.dstu3.builder.RequestGroupActionBuilder in project cqf-ruler by DBCG.

the class Session method resolveActions.

private CarePlan resolveActions(RequestDetails theRequest, Session session) {
    for (PlanDefinition.PlanDefinitionActionComponent action : session.getPlanDefinition().getAction()) {
        // TODO - Apply input/output dataRequirements?
        if (meetsConditions(theRequest, session, action)) {
            Resource result = resolveDefinition(theRequest, session, action);
            RequestGroupActionComponent currentActionTarget = null;
            if (result != null) {
                currentActionTarget = new RequestGroupActionBuilder().buildResource(new Reference("#" + result.getId())).build();
                session.getRequestGroupBuilder().buildContained(result).addAction(currentActionTarget);
            }
            resolveDynamicActions(theRequest, session, currentActionTarget, action);
        }
    }
    RequestGroup result = session.getRequestGroupBuilder().build();
    if (result.getId() == null) {
        result.setId(UUID.randomUUID().toString());
    }
    session.getCarePlanBuilder().buildContained(result).buildActivity(new CarePlanActivityBuilder().buildReference(new Reference("#" + result.getId())).build());
    return session.getCarePlan();
}
Also used : RequestGroupActionComponent(org.hl7.fhir.dstu3.model.RequestGroup.RequestGroupActionComponent) RequestGroupActionBuilder(org.opencds.cqf.ruler.cr.dstu3.builder.RequestGroupActionBuilder) Reference(org.hl7.fhir.dstu3.model.Reference) RequestGroup(org.hl7.fhir.dstu3.model.RequestGroup) DomainResource(org.hl7.fhir.dstu3.model.DomainResource) Resource(org.hl7.fhir.dstu3.model.Resource) PlanDefinition(org.hl7.fhir.dstu3.model.PlanDefinition) CarePlanActivityBuilder(org.opencds.cqf.ruler.cr.dstu3.builder.CarePlanActivityBuilder)

Example 3 with RequestGroupActionBuilder

use of org.opencds.cqf.ruler.cr.dstu3.builder.RequestGroupActionBuilder in project cqf-ruler by DBCG.

the class Session method resolveDefinition.

private Resource resolveDefinition(RequestDetails theRequest, Session session, PlanDefinition.PlanDefinitionActionComponent action) {
    Resource result = null;
    if (action.hasDefinition()) {
        logger.debug("Resolving definition: {}", action.getDefinition().getReference());
        Reference definition = action.getDefinition();
        if (definition.getId().contains(session.getPlanDefinition().fhirType())) {
            IdType id = new IdType(definition.getId());
            CarePlan plan;
            try {
                plan = applyPlanDefinition(theRequest, id, session.getPatientId(), session.getEncounterId(), session.getPractitionerId(), session.getOrganizationId(), session.getUserType(), session.getUserLanguage(), session.getUserTaskContext(), session.getSetting(), session.getSettingContext());
                if (plan.getId() == null) {
                    plan.setId(UUID.randomUUID().toString());
                }
                // Add an action to the request group which points to this CarePlan
                session.getRequestGroupBuilder().buildContained(plan).addAction(new RequestGroupActionBuilder().buildResource(new Reference("#" + plan.getId())).build());
                for (Reference r : plan.getDefinition()) {
                    session.getCarePlanBuilder().buildDefinition(r);
                }
                result = plan;
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("nested plan failed");
            }
        } else {
            try {
                if (action.getDefinition().getReferenceElement().getIdPart().startsWith("#")) {
                    result = this.activityDefinitionApplyProvider.resolveActivityDefinition((ActivityDefinition) resolveContained(session.getPlanDefinition(), action.getDefinition().getReferenceElement().getIdPart()), session.getPatientId(), session.getPractitionerId(), session.getOrganizationId(), theRequest);
                } else {
                    result = this.activityDefinitionApplyProvider.apply(theRequest, new IdType(action.getDefinition().getReferenceElement().getIdPart()), session.getPatientId(), session.getEncounterId(), session.getPractitionerId(), session.getOrganizationId(), null, session.getUserLanguage(), session.getUserTaskContext(), session.getSetting(), session.getSettingContext());
                }
                if (result.getId() == null) {
                    logger.warn("ActivityDefinition {} returned resource with no id, setting one", action.getDefinition().getReferenceElement().getIdPart());
                    result.setId(UUID.randomUUID().toString());
                }
                session.getRequestGroupBuilder().buildContained(result).addAction(new RequestGroupActionBuilder().buildResource(new Reference("#" + result.getId())).build());
            } catch (Exception e) {
                logger.error("ERROR: ActivityDefinition {} could not be applied and threw exception {}", action.getDefinition(), e.toString());
            }
        }
    }
    return result;
}
Also used : RequestGroupActionBuilder(org.opencds.cqf.ruler.cr.dstu3.builder.RequestGroupActionBuilder) CarePlan(org.hl7.fhir.dstu3.model.CarePlan) Reference(org.hl7.fhir.dstu3.model.Reference) DomainResource(org.hl7.fhir.dstu3.model.DomainResource) Resource(org.hl7.fhir.dstu3.model.Resource) IOException(java.io.IOException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) IdType(org.hl7.fhir.dstu3.model.IdType) ActivityDefinition(org.hl7.fhir.dstu3.model.ActivityDefinition)

Example 4 with RequestGroupActionBuilder

use of org.opencds.cqf.ruler.cr.dstu3.builder.RequestGroupActionBuilder in project cqf-ruler by DBCG.

the class Session method resolveActions.

private void resolveActions(RequestDetails theRequest, PlanDefinition planDefinition, List<PlanDefinition.PlanDefinitionActionComponent> actions, Context context, String patientId, RequestGroupBuilder requestGroupBuilder, List<RequestGroup.RequestGroupActionComponent> actionComponents) {
    for (PlanDefinition.PlanDefinitionActionComponent action : actions) {
        boolean conditionsMet = true;
        for (PlanDefinition.PlanDefinitionActionConditionComponent condition : action.getCondition()) {
            if (condition.getKind() == PlanDefinition.ActionConditionKind.APPLICABILITY) {
                if (!condition.hasExpression()) {
                    continue;
                }
                Boolean aliasedExpression = null;
                if (condition.hasLanguage()) {
                    String language = condition.getLanguage();
                    if (language.equals("text/cql.identifier") || language.equals("text/cql-identifier") || language.equals("text/cql.name") || language.equals("text/cql-name")) {
                        aliasedExpression = true;
                    } else {
                        aliasedExpression = false;
                    }
                } else {
                    aliasedExpression = false;
                }
                Object result = expressionEvaluation.evaluateInContext(planDefinition, condition.getExpression(), aliasedExpression, patientId, theRequest);
                if (!(result instanceof Boolean)) {
                    continue;
                }
                if (!(Boolean) result) {
                    conditionsMet = false;
                }
            }
            if (conditionsMet) {
                RequestGroupActionBuilder actionBuilder = new RequestGroupActionBuilder();
                if (action.hasTitle()) {
                    actionBuilder.buildTitle(action.getTitle());
                }
                if (action.hasDescription()) {
                    actionBuilder.buildDescripition(action.getDescription());
                }
                // source
                if (action.hasDocumentation()) {
                    RelatedArtifact artifact = action.getDocumentationFirstRep();
                    RelatedArtifactBuilder artifactBuilder = new RelatedArtifactBuilder();
                    if (artifact.hasDisplay()) {
                        artifactBuilder.buildDisplay(artifact.getDisplay());
                    }
                    if (artifact.hasUrl()) {
                        artifactBuilder.buildUrl(artifact.getUrl());
                    }
                    if (artifact.hasDocument() && artifact.getDocument().hasUrl()) {
                        AttachmentBuilder attachmentBuilder = new AttachmentBuilder();
                        attachmentBuilder.buildUrl(artifact.getDocument().getUrl());
                        artifactBuilder.buildDocument(attachmentBuilder.build());
                    }
                    actionBuilder.buildDocumentation(Collections.singletonList(artifactBuilder.build()));
                }
                // TODO - uuid
                if (action.hasLabel()) {
                    actionBuilder.buildLabel(action.getLabel());
                }
                if (action.hasType()) {
                    actionBuilder.buildType(action.getType());
                }
                if (action.hasDefinition()) {
                    if (action.getDefinition().getReferenceElement().getResourceType().equals("ActivityDefinition")) {
                        if (action.getDefinition().getResource() != null) {
                            ActivityDefinition activityDefinition = (ActivityDefinition) action.getDefinition().getResource();
                            ReferenceBuilder referenceBuilder = new ReferenceBuilder();
                            referenceBuilder.buildDisplay(activityDefinition.getDescription());
                            actionBuilder.buildResource(referenceBuilder.build());
                            if (activityDefinition.hasDescription()) {
                                actionBuilder.buildDescripition(activityDefinition.getDescription());
                            }
                        }
                        ActivityDefinition activityDefinition = this.activityDefinitionDao.read(action.getDefinition().getReferenceElement());
                        if (activityDefinition.hasDescription()) {
                            actionBuilder.buildDescripition(activityDefinition.getDescription());
                        }
                        try {
                            this.activityDefinitionApplyProvider.apply(theRequest, new IdType(action.getDefinition().getReferenceElement().getIdPart()), patientId, null, null, null, null, null, null, null, null).setId(UUID.randomUUID().toString());
                        } catch (FHIRException e) {
                            throw new RuntimeException("Error applying ActivityDefinition " + e.getMessage());
                        }
                        Parameters inParams = new Parameters();
                        inParams.addParameter().setName("patient").setValue(new StringType(patientId));
                        Parameters outParams = this.fhirContext.newRestfulGenericClient(theRequest.getFhirServerBase()).operation().onInstance(new IdDt("ActivityDefinition", action.getDefinition().getId())).named("$apply").withParameters(inParams).useHttpGet().execute();
                        List<Parameters.ParametersParameterComponent> response = outParams.getParameter();
                        Resource resource = response.get(0).getResource().setId(UUID.randomUUID().toString());
                        actionBuilder.buildResourceTarget(resource);
                        actionBuilder.buildResource(new ReferenceBuilder().buildReference(resource.getId()).build());
                    }
                }
                // on here...
                if (action.hasDynamicValue()) {
                    for (PlanDefinition.PlanDefinitionActionDynamicValueComponent dynamicValue : action.getDynamicValue()) {
                        if (dynamicValue.hasPath() && dynamicValue.hasExpression()) {
                            if (dynamicValue.getPath().endsWith("title")) {
                                // summary
                                String title = (String) context.resolveExpressionRef(dynamicValue.getExpression()).evaluate(context);
                                actionBuilder.buildTitle(title);
                            } else if (dynamicValue.getPath().endsWith("description")) {
                                // detail
                                String description = (String) context.resolveExpressionRef(dynamicValue.getExpression()).evaluate(context);
                                actionBuilder.buildDescripition(description);
                            } else if (dynamicValue.getPath().endsWith("extension")) {
                                // indicator
                                String extension = (String) context.resolveExpressionRef(dynamicValue.getExpression()).evaluate(context);
                                actionBuilder.buildExtension(extension);
                            }
                        }
                    }
                }
                if (!actionBuilder.build().isEmpty()) {
                    actionComponents.add(actionBuilder.build());
                }
                if (action.hasAction()) {
                    resolveActions(theRequest, planDefinition, action.getAction(), context, patientId, requestGroupBuilder, actionComponents);
                }
            }
        }
    }
    requestGroupBuilder.buildAction(new ArrayList<>(actionComponents));
}
Also used : Parameters(org.hl7.fhir.dstu3.model.Parameters) StringType(org.hl7.fhir.dstu3.model.StringType) DomainResource(org.hl7.fhir.dstu3.model.DomainResource) Resource(org.hl7.fhir.dstu3.model.Resource) IdDt(ca.uhn.fhir.model.primitive.IdDt) RelatedArtifact(org.hl7.fhir.dstu3.model.RelatedArtifact) FHIRException(org.hl7.fhir.exceptions.FHIRException) IdType(org.hl7.fhir.dstu3.model.IdType) RequestGroupActionBuilder(org.opencds.cqf.ruler.cr.dstu3.builder.RequestGroupActionBuilder) AttachmentBuilder(org.opencds.cqf.ruler.cr.dstu3.builder.AttachmentBuilder) ReferenceBuilder(org.opencds.cqf.ruler.cr.dstu3.builder.ReferenceBuilder) PlanDefinition(org.hl7.fhir.dstu3.model.PlanDefinition) RelatedArtifactBuilder(org.opencds.cqf.ruler.cr.dstu3.builder.RelatedArtifactBuilder) ActivityDefinition(org.hl7.fhir.dstu3.model.ActivityDefinition)

Example 5 with RequestGroupActionBuilder

use of org.opencds.cqf.ruler.cr.dstu3.builder.RequestGroupActionBuilder in project cqf-ruler by DBCG.

the class Session method resolveActions.

private CarePlan resolveActions(Session session, RequestDetails theRequest) {
    RequestGroup theRequestGroup = session.getRequestGroupBuilder().build();
    for (PlanDefinition.PlanDefinitionActionComponent action : session.getPlanDefinition().getAction()) {
        // TODO - Apply input/output dataRequirements?
        if (meetsConditions(session, action, theRequest)) {
            Resource result = resolveDefinition(session, action, theRequest);
            RequestGroupActionComponent currentActionTarget = null;
            if (result != null) {
                currentActionTarget = new RequestGroupActionBuilder().buildResource(new Reference("#" + result.getId())).build();
                session.getRequestGroupBuilder().buildContained(result).addAction(currentActionTarget);
            }
            resolveDynamicActions(session, action, currentActionTarget, theRequest);
        }
    }
    if (theRequestGroup.getId() == null) {
        theRequestGroup.setId(UUID.randomUUID().toString());
    }
    session.getCarePlanBuilder().buildContained(theRequestGroup).buildActivity(new CarePlanActivityBuilder().buildReference(new Reference("#" + theRequestGroup.getId())).build());
    return session.getCarePlan();
}
Also used : RequestGroupActionComponent(org.hl7.fhir.r4.model.RequestGroup.RequestGroupActionComponent) RequestGroupActionBuilder(org.opencds.cqf.ruler.cr.r4.builder.RequestGroupActionBuilder) RequestGroup(org.hl7.fhir.r4.model.RequestGroup) Reference(org.hl7.fhir.r4.model.Reference) Resource(org.hl7.fhir.r4.model.Resource) DomainResource(org.hl7.fhir.r4.model.DomainResource) PlanDefinition(org.hl7.fhir.r4.model.PlanDefinition) CarePlanActivityBuilder(org.opencds.cqf.ruler.cr.r4.builder.CarePlanActivityBuilder)

Aggregations

DomainResource (org.hl7.fhir.dstu3.model.DomainResource)3 Resource (org.hl7.fhir.dstu3.model.Resource)3 FHIRException (org.hl7.fhir.exceptions.FHIRException)3 RequestGroupActionBuilder (org.opencds.cqf.ruler.cr.dstu3.builder.RequestGroupActionBuilder)3 IdDt (ca.uhn.fhir.model.primitive.IdDt)2 ActivityDefinition (org.hl7.fhir.dstu3.model.ActivityDefinition)2 IdType (org.hl7.fhir.dstu3.model.IdType)2 PlanDefinition (org.hl7.fhir.dstu3.model.PlanDefinition)2 Reference (org.hl7.fhir.dstu3.model.Reference)2 DomainResource (org.hl7.fhir.r4.model.DomainResource)2 PlanDefinition (org.hl7.fhir.r4.model.PlanDefinition)2 Resource (org.hl7.fhir.r4.model.Resource)2 RequestGroupActionBuilder (org.opencds.cqf.ruler.cr.r4.builder.RequestGroupActionBuilder)2 IOException (java.io.IOException)1 CarePlan (org.hl7.fhir.dstu3.model.CarePlan)1 Parameters (org.hl7.fhir.dstu3.model.Parameters)1 RelatedArtifact (org.hl7.fhir.dstu3.model.RelatedArtifact)1 RequestGroup (org.hl7.fhir.dstu3.model.RequestGroup)1 RequestGroupActionComponent (org.hl7.fhir.dstu3.model.RequestGroup.RequestGroupActionComponent)1 StringType (org.hl7.fhir.dstu3.model.StringType)1