use of org.hl7.fhir.utilities.validation.ValidationMessage.Source in project health-patterns by LinuxForHealth.
the class TerminologyService method translateResource.
/**
* Translates the given single FHIR resource represented as a {@link JsonNode}.
*
* @param resource the FHIR resource to translate
* @returns true if there was something to translate, false otherwise
* @throws DeIdentifierException if there is an error in the de-identification REST API or parsing the JSON
* @throws IllegalArgumentException if the given JSON does not have a 'resource' object
*/
private boolean translateResource(JsonNode resource) {
boolean translatedSomething = false;
String resourceType = getResourceType(resource);
boolean isTranslatable = StringUtils.equalsAny(resourceType, TRANSLATABLE_FHIR_TYPES);
if (!isTranslatable) {
return translatedSomething;
}
ArrayNode extensions = (ArrayNode) resource.get(EXTENSION_OBJECT);
if (extensions == null) {
return translatedSomething;
}
for (int i = 0; i < extensions.size(); i++) {
JsonNode extension = extensions.get(i);
JsonNode urlJson = extension.get(URL_OBJECT);
JsonNode valueCodeJson = extension.get(VALUE_CODE_OBJECT);
if (urlJson == null || valueCodeJson == null) {
// In order to do a translation we need both the url and the valueCode
continue;
}
// The resource's extension URL is the URL for the StructureDefinition, so we resolve a ValueSet if known
String structureDefinitionURL = urlJson.asText();
String valueSetURL = valueSetForStructureDefinition.get(structureDefinitionURL);
// and if known we check the FHIR Server's known ConceptMaps to see if there is a corresponding one
// http://4603f72b-us-south.lb.appdomain.cloud/fhir-server/api/v4/ConceptMap?_format=json&source-uri=http://hl7.org/fhir/us/core/ValueSet/birthsex
Bundle bundle = fhirClient.search().forResource(ConceptMap.class).where(ConceptMap.SOURCE_URI.hasId(valueSetURL)).returnBundle(Bundle.class).execute();
String conceptMapId;
if (!bundle.getEntry().isEmpty()) {
Resource conceptMap = bundle.getEntry().get(0).getResource();
if (bundle.getEntry().size() > 1) {
System.err.println("Found multiple ConceptMaps that will map " + valueSetURL + " for this StructureDefinition, will use the first one " + conceptMap.getId());
} else {
System.out.println("Found ConceptMap for " + valueSetURL + ": " + conceptMap.getId() + " !!");
}
conceptMapId = conceptMap.getIdElement().getIdPart();
} else {
System.out.println("Did not find ConceptMap for " + valueSetURL + "!!");
continue;
}
// "POST ${FHIR_URL}/${conceptMapID}/$translate?code=${code}&system=${valueSet}&_format=json
String valueCode = valueCodeJson.asText();
String url = String.format("%s/ConceptMap/%s/$translate?code=%s&system=%s&_format=json", fhirClient.getServerBase(), conceptMapId, valueCode, valueSetURL);
Parameters translationResponse = fhirClient.fetchResourceFromUrl(Parameters.class, url);
// This is what comes back from the server
// {
// "resourceType": "Parameters",
// "parameter": [
// {
// "name": "result",
// "valueBoolean": true
// },
// {
// "name": "match",
// "part": [
// {
// "name": "equivalence",
// "valueCode": "equivalent"
// },
// {
// "name": "concept",
// "valueCoding": {
// "system": "http://ibm.com/fhir/cdm/ValueSet/sex-assigned-at-birth",
// "code": "male",
// "display": "Male"
// }
// }
// ]
// }
// ]
// }
Coding translatedCode = null;
List<ParametersParameterComponent> parameters = translationResponse.getParameter();
for (ParametersParameterComponent parameter : parameters) {
if (parameter.getName().equals(MATCH_VALUE)) {
List<ParametersParameterComponent> parts = parameter.getPart();
for (ParametersParameterComponent part : parts) {
if (part.getName().equals(CONCEPT_VALUE)) {
try {
translatedCode = (Coding) part.getValue();
} catch (ClassCastException e) {
String jsonResponse = fhirClient.getFhirContext().newJsonParser().encodeResourceToString(translationResponse);
System.err.println("Found a ConceptMap that will map " + valueSetURL + " for this StructureDefinition, but the FHIR server returned an unknown $translate response (expected a 'valueCoding' part): " + jsonResponse);
}
}
}
}
}
if (translatedCode == null) {
String jsonResponse = fhirClient.getFhirContext().newJsonParser().encodeResourceToString(translationResponse);
System.err.println("Found a ConceptMap that will map " + valueSetURL + " for this StructureDefinition, but the FHIR server returned an unknown $translate response: " + jsonResponse);
continue;
}
System.out.printf("Found ConceptMap %s which translates (valueCode, system) = (%s, %s) for StructureDefinition %s to (valueCode, system) = (%s, %s) %n", conceptMapId, valueCode, valueSetURL, structureDefinitionURL, translatedCode.getCode(), translatedCode.getSystem());
String translatedStructuredData = valueSetForStructureDefinition.get(translatedCode.getSystem());
if (translatedStructuredData == null) {
System.err.printf("Cannot find the mapping from ValueSet '%s' to its corresponding StructureData for this translation, make sure the corresponding mappings configuration file has it.%n", translatedCode.getSystem());
continue;
}
((ObjectNode) extension).set(URL_OBJECT, JsonNodeFactory.instance.textNode(translatedStructuredData));
((ObjectNode) extension).set(VALUE_CODE_OBJECT, JsonNodeFactory.instance.textNode(translatedCode.getCode()));
translatedSomething = true;
}
return translatedSomething;
}
use of org.hl7.fhir.utilities.validation.ValidationMessage.Source in project BridgeServer2 by Sage-Bionetworks.
the class CRCController method createPatient.
Patient createPatient(Account account) {
Patient patient = new Patient();
patient.setActive(true);
patient.setId(account.getId());
Identifier identifier = new Identifier();
identifier.setValue(account.getId());
identifier.setSystem(USER_ID_VALUE_NS);
patient.addIdentifier(identifier);
Coding coding = new Coding();
coding.setSystem("source");
coding.setCode("sage");
Meta meta = new Meta();
meta.setTag(ImmutableList.of(coding));
patient.setMeta(meta);
HumanName name = new HumanName();
if (isNotBlank(account.getFirstName())) {
name.addGiven(account.getFirstName());
}
if (isNotBlank(account.getLastName())) {
name.setFamily(account.getLastName());
}
patient.addName(name);
Map<String, String> atts = account.getAttributes();
if (isNotBlank(atts.get("gender"))) {
if ("female".equalsIgnoreCase(atts.get("gender"))) {
patient.setGender(AdministrativeGender.FEMALE);
} else if ("male".equalsIgnoreCase(atts.get("gender"))) {
patient.setGender(AdministrativeGender.MALE);
} else {
patient.setGender(AdministrativeGender.OTHER);
}
} else {
patient.setGender(AdministrativeGender.UNKNOWN);
}
if (isNotBlank(atts.get("dob"))) {
LocalDate localDate = LocalDate.parse(atts.get("dob"));
patient.setBirthDate(localDate.toDate());
}
Address address = new Address();
if (isNotBlank(atts.get("address1"))) {
address.addLine(atts.get("address1"));
}
if (isNotBlank(atts.get("address2"))) {
address.addLine(atts.get("address2"));
}
if (isNotBlank(atts.get("city"))) {
address.setCity(atts.get("city"));
}
if (isNotBlank(atts.get("state"))) {
address.setState(atts.get("state"));
} else {
address.setState("NY");
}
if (isNotBlank(atts.get("zip_code"))) {
address.setPostalCode(atts.get("zip_code"));
}
patient.addAddress(address);
if (isNotBlank(atts.get("home_phone"))) {
ContactPoint contact = new ContactPoint();
contact.setSystem(ContactPointSystem.PHONE);
contact.setValue(atts.get("home_phone"));
patient.addTelecom(contact);
}
if (account.getPhone() != null && TRUE.equals(account.getPhoneVerified())) {
ContactPoint contact = new ContactPoint();
contact.setSystem(ContactPointSystem.SMS);
contact.setValue(account.getPhone().getNumber());
patient.addTelecom(contact);
}
if (account.getEmail() != null && TRUE.equals(account.getEmailVerified())) {
ContactPoint contact = new ContactPoint();
contact.setSystem(ContactPointSystem.EMAIL);
contact.setValue(account.getEmail());
patient.addTelecom(contact);
}
Reference ref = new Reference("CUZUCK");
ref.setDisplay("COVID Recovery Corps");
ContactComponent contact = new ContactComponent();
contact.setOrganization(ref);
patient.addContact(contact);
return patient;
}
use of org.hl7.fhir.utilities.validation.ValidationMessage.Source in project cqf-ruler by DBCG.
the class R4CarePlanToCdsCard method convert.
private static List<CdsCard> convert(RequestGroup requestGroup) {
List<CdsCard> cards = new ArrayList<>();
// links
List<CdsCard.Links> links = new ArrayList<>();
if (requestGroup.hasExtension()) {
for (Extension extension : requestGroup.getExtension()) {
CdsCard.Links link = new CdsCard.Links();
if (extension.getValue() instanceof Attachment) {
Attachment attachment = (Attachment) extension.getValue();
if (attachment.hasUrl()) {
link.setUrl(attachment.getUrl());
}
if (attachment.hasTitle()) {
link.setLabel(attachment.getTitle());
}
if (attachment.hasExtension()) {
link.setType(attachment.getExtensionFirstRep().getValue().primitiveValue());
}
} else {
throw new RuntimeException("Invalid link extension type: " + extension.getValue().fhirType());
}
links.add(link);
}
}
if (requestGroup.hasAction()) {
for (RequestGroup.RequestGroupActionComponent action : requestGroup.getAction()) {
IParser jsonParser = FhirContext.forCached(FhirVersionEnum.R4).newJsonParser().setPrettyPrint(true);
CdsCard card = new CdsCard(jsonParser);
// basic
if (action.hasTitle()) {
card.setSummary(action.getTitle());
}
if (action.hasDescription()) {
card.setDetail(action.getDescription());
}
if (action.hasExtension()) {
card.setIndicator(action.getExtensionFirstRep().getValue().toString());
}
// source
if (action.hasDocumentation()) {
// Assuming first related artifact has everything
RelatedArtifact documentation = action.getDocumentationFirstRep();
CdsCard.Source source = new CdsCard.Source();
if (documentation.hasDisplay()) {
source.setLabel(documentation.getDisplay());
}
if (documentation.hasUrl()) {
source.setUrl(documentation.getUrl());
}
if (documentation.hasDocument() && documentation.getDocument().hasUrl()) {
source.setIcon(documentation.getDocument().getUrl());
}
card.setSource(source);
}
if (action.hasSelectionBehavior()) {
card.setSelectionBehavior(action.getSelectionBehavior().toCode());
}
// suggestions
// TODO - uuid
boolean hasSuggestions = false;
CdsCard.Suggestions suggestions = new CdsCard.Suggestions();
CdsCard.Suggestions.Action actions = new CdsCard.Suggestions.Action();
if (action.hasPrefix()) {
suggestions.setLabel(action.getPrefix());
hasSuggestions = true;
if (action.hasDescription()) {
actions.setDescription(action.getDescription());
}
if (action.hasType() && !action.getType().getCodingFirstRep().getCode().equals("fire-event")) {
String code = action.getType().getCodingFirstRep().getCode();
actions.setType(CdsCard.Suggestions.Action.ActionType.valueOf(code.equals("remove") ? "delete" : code));
}
if (action.hasResource()) {
if (actions.getType().name().equalsIgnoreCase("create")) {
action.getResourceTarget().setId((String) null);
}
actions.setResource(action.getResourceTarget());
}
}
if (hasSuggestions) {
suggestions.addAction(actions);
card.addSuggestion(suggestions);
}
if (!links.isEmpty()) {
card.setLinks(links);
}
cards.add(card);
}
}
return cards;
}
use of org.hl7.fhir.utilities.validation.ValidationMessage.Source 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.hl7.fhir.utilities.validation.ValidationMessage.Source in project cqf-ruler by DBCG.
the class ExpressionEvaluation method setupContext.
private Context setupContext(DomainResource instance, String cql, String patientId, Boolean aliasedExpression, RequestDetails theRequest) {
JpaFhirDal jpaFhirDal = jpaFhirDalFactory.create(theRequest);
List<CanonicalType> libraries = getLibraryReferences(instance, theRequest);
String fhirVersion = this.fhirContext.getVersion().getVersion().getFhirVersionString();
// Remove LocalLibrary from cache first...
VersionedIdentifier localLibraryIdentifier = new VersionedIdentifier().withId("LocalLibrary");
globalLibraryCache.remove(localLibraryIdentifier);
// temporary LibraryLoader to resolve library dependencies when building
// includes
LibraryLoader tempLibraryLoader = libraryLoaderFactory.create(new ArrayList<LibraryContentProvider>(Arrays.asList(jpaLibraryContentProviderFactory.create(theRequest))));
String source = "";
if (aliasedExpression) {
if (libraries.size() != 1) {
throw new RuntimeException("If an aliased expression is provided, there must be exactly one primary Library");
}
VersionedIdentifier vi = getVersionedIdentifierFromCanonical(libraries.get(0));
// Still not the best way to build include, but at least checks dal for an
// existing library
// Check if id works for LibraryRetrieval
org.cqframework.cql.elm.execution.Library executionLibrary = null;
try {
executionLibrary = tempLibraryLoader.load(vi);
} catch (Exception e) {
// log error
}
if (executionLibrary == null) {
Library library = (Library) jpaFhirDal.read(new IdType("Library", Canonicals.getIdPart(libraries.get(0))));
vi.setId(library.getName());
if (library.getVersion() != null) {
vi.setVersion(library.getVersion());
}
}
source = String.format("library LocalLibrary using FHIR version '" + fhirVersion + "' include FHIRHelpers version '" + fhirVersion + "' called FHIRHelpers %s parameter %s %s parameter \"%%context\" %s define Expression: %s", buildIncludes(tempLibraryLoader, jpaFhirDal, libraries, theRequest), instance.fhirType(), instance.fhirType(), instance.fhirType(), vi.getId() + ".\"" + cql + "\"");
} else {
source = String.format("library LocalLibrary using FHIR version '" + fhirVersion + "' include FHIRHelpers version '" + fhirVersion + "' called FHIRHelpers %s parameter %s %s parameter \"%%context\" %s define Expression: %s", buildIncludes(tempLibraryLoader, jpaFhirDal, libraries, theRequest), instance.fhirType(), instance.fhirType(), instance.fhirType(), cql);
}
LibraryLoader libraryLoader = libraryLoaderFactory.create(new ArrayList<LibraryContentProvider>(Arrays.asList(jpaLibraryContentProviderFactory.create(theRequest), new InMemoryLibraryContentProvider(Arrays.asList(source)))));
// resolve execution context
return setupContext(instance, patientId, libraryLoader, theRequest);
}
Aggregations