Skip to content

Commit

Permalink
Update cql example
Browse files Browse the repository at this point in the history
  • Loading branch information
Panthevm committed Jan 15, 2025
1 parent 6d9f817 commit 9ec120d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ public class HelloController {
@Autowired
private CqlEvaluateService evaluateService;

public String cqlLibraryEvaluate(JsonNode body) {
String libraryName = body.path("request").path("route-params").path("libraryName").asText();
return evaluateService.evaluate(libraryName);
}

@PostMapping("/")
public String index(@RequestBody String body) {
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(body);
String libraryName = rootNode.path("request").path("route-params").path("library").asText();
String expressionName = rootNode.path("request").path("route-params").path("expressionName").asText();
return evaluateService.evaluate(libraryName, expressionName);
JsonNode jsonBody = objectMapper.readTree(body);
String operationID = jsonBody.path("operation").path("id").asText();
switch(operationID) {
case "cql-library-evaluate":
return cqlLibraryEvaluate(jsonBody);
default:
return "Operation " + operationID + " not founded in App";
}

} catch (Exception e) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.cql.app.example.services;

import org.opencds.cqf.cql.engine.execution.ExpressionResult;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IPrimitiveType;


import java.util.Map;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.rest.client.api.IGenericClient;
Expand All @@ -21,6 +28,11 @@
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

import java.io.IOException;
import java.io.InputStream;

Expand Down Expand Up @@ -76,15 +88,72 @@ public CqlEvaluateService(
this.compiler = new CqlCompiler(libraryManager);
}

public String evaluate(String libraryName, String expressionName) {
private boolean isValidJson(String str) {
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.readTree(str);
return true;
} catch (IOException e) {
return false;
}
}

public String buildResponse (Map<String, ExpressionResult> result) {
try {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode response = objectMapper.createObjectNode();
ArrayNode parameters = objectMapper.createArrayNode();
response.set("resourceType", objectMapper.getNodeFactory().textNode("Parameters"));

for (var entry : result.entrySet()) {
var expressionName = entry.getKey();
var expressionValue = entry.getValue().value();
for (var value : (Iterable)expressionValue) {
ObjectNode parameter = objectMapper.createObjectNode();
var valueStr = FhirContext.forR4().newJsonParser().encodeToString((IBase)value);
parameter.set("name", objectMapper.getNodeFactory().textNode(expressionName));
if (value instanceof IBaseResource) {
parameter.set("valueResource", objectMapper.readTree(valueStr));
} else if (value instanceof IPrimitiveType) {
var chars = ((IPrimitiveType)value).fhirType().toCharArray();
chars[0] = Character.toUpperCase(chars[0]);
String formattedKey = "value" + new String(chars);
if (value instanceof IPrimitiveType<?>) {
String stringValue = ((IPrimitiveType<?>) value).getValueAsString();
if (isValidJson(stringValue)) {
parameter.set(formattedKey, objectMapper.readTree(stringValue));
} else {
parameter.set(formattedKey, objectMapper.getNodeFactory().textNode(stringValue));
}
}
} else if (value instanceof IBase) {
var chars = ((IBase)value).fhirType().toCharArray();
chars[0] = Character.toUpperCase(chars[0]);
parameter.set("value" + new String(chars), objectMapper.readTree(valueStr));
}
parameters.add(parameter);
}

}
response.set("parameters", parameters);

return objectMapper.writeValueAsString(response);
} catch (IOException e) {
throw new RuntimeException(e);
}

}


public String evaluate(String libraryName) {
var exampleCqlFile = new ClassPathResource(libraryName + ".cql");
try {
var library = compiler.run(exampleCqlFile.getFile());
} catch (IOException e) {
throw new RuntimeException(e);
}
logger.info("Evaluating {} in file: {} ", expressionName, libraryName);
logger.info("Evaluating file: {} ", libraryName);
var result = engine.evaluate(libraryName);
return result.forExpression(expressionName).value().toString();
return buildResponse(result.expressionResults);
}
}
2 changes: 1 addition & 1 deletion cql_app_example/src/main/resources/example.cql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ include FHIRHelpers version '4.0.1'
define "MalePatients":
[Patient] P
where P.gender.value = 'male'
return P.name.family
return P.name[0]

0 comments on commit 9ec120d

Please sign in to comment.