use of org.opencds.cqf.ruler.cr.r4.builder.RequestGroupBuilder in project cqf-ruler by DBCG.
the class Session method applyPlanDefinition.
@Operation(name = "$apply", idempotent = true, type = PlanDefinition.class)
public CarePlan applyPlanDefinition(RequestDetails theRequest, @IdParam IdType theId, @OperationParam(name = "patient") String patientId, @OperationParam(name = "encounter") String encounterId, @OperationParam(name = "practitioner") String practitionerId, @OperationParam(name = "organization") String organizationId, @OperationParam(name = "userType") String userType, @OperationParam(name = "userLanguage") String userLanguage, @OperationParam(name = "userTaskContext") String userTaskContext, @OperationParam(name = "setting") String setting, @OperationParam(name = "settingContext") String settingContext) throws IOException, FHIRException {
PlanDefinition planDefinition = this.planDefinitionDao.read(theId);
if (planDefinition == null) {
throw new IllegalArgumentException("Couldn't find PlanDefinition " + theId);
}
logger.info("Performing $apply operation on PlanDefinition/{}", theId);
CarePlanBuilder builder = new CarePlanBuilder();
builder.buildInstantiatesCanonical(planDefinition.getUrl()).buildSubject(new Reference(patientId)).buildStatus(CarePlan.CarePlanStatus.DRAFT);
if (encounterId != null)
builder.buildEncounter(new Reference(encounterId));
if (practitionerId != null)
builder.buildAuthor(new Reference(practitionerId));
if (organizationId != null)
builder.buildAuthor(new Reference(organizationId));
if (userLanguage != null)
builder.buildLanguage(userLanguage);
// Each Group of actions shares a RequestGroup
RequestGroupBuilder requestGroupBuilder = new RequestGroupBuilder().buildStatus().buildIntent();
Session session = new Session(planDefinition, builder, patientId, encounterId, practitionerId, organizationId, userType, userLanguage, userTaskContext, setting, settingContext, requestGroupBuilder);
return (CarePlan) ContainedHelper.liftContainedResourcesToParent(resolveActions(session, theRequest));
}
use of org.opencds.cqf.ruler.cr.r4.builder.RequestGroupBuilder 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));
}
use of org.opencds.cqf.ruler.cr.r4.builder.RequestGroupBuilder in project cqf-ruler by DBCG.
the class Session method resolveCdsHooksPlanDefinition.
// For library use
public CarePlan resolveCdsHooksPlanDefinition(RequestDetails theRequest, Context context, PlanDefinition planDefinition, String patientId) {
CarePlanBuilder carePlanBuilder = new CarePlanBuilder();
RequestGroupBuilder requestGroupBuilder = new RequestGroupBuilder().buildStatus().buildIntent();
// links
if (planDefinition.hasRelatedArtifact()) {
List<Extension> extensions = new ArrayList<>();
for (RelatedArtifact relatedArtifact : planDefinition.getRelatedArtifact()) {
AttachmentBuilder attachmentBuilder = new AttachmentBuilder();
ExtensionBuilder extensionBuilder = new ExtensionBuilder();
if (relatedArtifact.hasDisplay()) {
// label
attachmentBuilder.buildTitle(relatedArtifact.getDisplay());
}
if (relatedArtifact.hasUrl()) {
// url
attachmentBuilder.buildUrl(relatedArtifact.getUrl());
}
if (relatedArtifact.hasExtension()) {
// type
attachmentBuilder.buildExtension(relatedArtifact.getExtension());
}
extensionBuilder.buildUrl("http://example.org");
extensionBuilder.buildValue(attachmentBuilder.build());
extensions.add(extensionBuilder.build());
}
requestGroupBuilder.buildExtension(extensions);
}
resolveActions(theRequest, planDefinition, planDefinition.getAction(), context, patientId, requestGroupBuilder, new ArrayList<>());
CarePlanActivityBuilder carePlanActivityBuilder = new CarePlanActivityBuilder();
carePlanActivityBuilder.buildReferenceTarget(requestGroupBuilder.build());
carePlanBuilder.buildActivity(carePlanActivityBuilder.build());
return carePlanBuilder.build();
}
use of org.opencds.cqf.ruler.cr.r4.builder.RequestGroupBuilder in project cqf-ruler by DBCG.
the class Session method applyPlanDefinition.
@Operation(name = "$apply", idempotent = true, type = PlanDefinition.class)
public CarePlan applyPlanDefinition(RequestDetails theRequest, @IdParam IdType theId, @OperationParam(name = "patient") String patientId, @OperationParam(name = "encounter") String encounterId, @OperationParam(name = "practitioner") String practitionerId, @OperationParam(name = "organization") String organizationId, @OperationParam(name = "userType") String userType, @OperationParam(name = "userLanguage") String userLanguage, @OperationParam(name = "userTaskContext") String userTaskContext, @OperationParam(name = "setting") String setting, @OperationParam(name = "settingContext") String settingContext) throws IOException, FHIRException {
PlanDefinition planDefinition = this.planDefinitionDao.read(theId);
if (planDefinition == null) {
throw new IllegalArgumentException("Couldn't find PlanDefinition " + theId);
}
logger.info("Performing $apply operation on PlanDefinition/{}", theId);
CarePlanBuilder builder = new CarePlanBuilder();
builder.buildDefinition(new Reference(planDefinition.getIdElement().getIdPart())).buildSubject(new Reference(patientId)).buildStatus(CarePlan.CarePlanStatus.DRAFT);
if (encounterId != null)
builder.buildContext(new Reference(encounterId));
if (practitionerId != null)
builder.buildAuthor(new Reference(practitionerId));
if (organizationId != null)
builder.buildAuthor(new Reference(organizationId));
if (userLanguage != null)
builder.buildLanguage(userLanguage);
// Each Group of actions shares a RequestGroup
RequestGroupBuilder requestGroupBuilder = new RequestGroupBuilder().buildStatus().buildIntent();
Session session = new Session(planDefinition, builder, patientId, encounterId, practitionerId, organizationId, userType, userLanguage, userTaskContext, setting, settingContext, requestGroupBuilder);
return (CarePlan) ContainedHelper.liftContainedResourcesToParent(resolveActions(theRequest, session));
}
use of org.opencds.cqf.ruler.cr.r4.builder.RequestGroupBuilder in project cqf-ruler by DBCG.
the class Session method resolveCdsHooksPlanDefinition.
// For library use
public CarePlan resolveCdsHooksPlanDefinition(Context context, PlanDefinition planDefinition, String patientId, RequestDetails theRequest) {
CarePlanBuilder carePlanBuilder = new CarePlanBuilder();
RequestGroupBuilder requestGroupBuilder = new RequestGroupBuilder().buildStatus().buildIntent();
// links
if (planDefinition.hasRelatedArtifact()) {
List<Extension> extensions = new ArrayList<>();
for (RelatedArtifact relatedArtifact : planDefinition.getRelatedArtifact()) {
AttachmentBuilder attachmentBuilder = new AttachmentBuilder();
ExtensionBuilder extensionBuilder = new ExtensionBuilder();
if (relatedArtifact.hasDisplay()) {
// label
attachmentBuilder.buildTitle(relatedArtifact.getDisplay());
}
if (relatedArtifact.hasUrl()) {
// url
attachmentBuilder.buildUrl(relatedArtifact.getUrl());
}
if (relatedArtifact.hasExtension()) {
// type
attachmentBuilder.buildExtension(relatedArtifact.getExtension());
}
extensionBuilder.buildUrl("http://example.org");
extensionBuilder.buildValue(attachmentBuilder.build());
extensions.add(extensionBuilder.build());
}
requestGroupBuilder.buildExtension(extensions);
}
resolveActions(planDefinition.getAction(), context, patientId, requestGroupBuilder, new ArrayList<>(), theRequest);
CarePlanActivityBuilder carePlanActivityBuilder = new CarePlanActivityBuilder();
carePlanActivityBuilder.buildReferenceTarget(requestGroupBuilder.build());
carePlanBuilder.buildActivity(carePlanActivityBuilder.build());
return carePlanBuilder.build();
}
Aggregations