Skip to content

Commit

Permalink
start working on conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveregger committed Sep 10, 2024
1 parent 8f06ac5 commit e35b68b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@
"vmArgs": "-Dspring.config.additional-location=file:../../matchbox-int-tests/src/test/resources/application-validate-r4-ch-epr-fhir.yaml",
"cwd": "${workspaceFolder}/matchbox-server"
},
{
"type": "java",
"name": "Launch Matchbox-Server (int-tests ch-elm)",
"request": "launch",
"mainClass": "ca.uhn.fhir.jpa.starter.Application",
"projectName": "matchbox-server",
"vmArgs": "-Dspring.config.additional-location=file:../../matchbox-int-tests/src/test/resources/application-validate-r4-ch-elm.yaml",
"cwd": "${workspaceFolder}/matchbox-server"
},
{
"type": "java",
"name": "Launch Matchbox-Server (int-tests ch)",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.ahdis.matchbox.engine;

import static org.apache.commons.lang3.StringUtils.substring;

/*
* #%L
* Matchbox Engine
Expand Down Expand Up @@ -61,6 +63,7 @@
import org.hl7.fhir.r5.fhirpath.FHIRPathEngine;
import org.hl7.fhir.utilities.ByteProvider;
import org.hl7.fhir.utilities.FhirPublication;
import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.json.model.JsonObject;
import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager;
import org.hl7.fhir.utilities.npm.NpmPackage;
Expand Down Expand Up @@ -378,6 +381,16 @@ public String transform(String input, boolean inputJson, String mapUri, boolean
public org.hl7.fhir.r5.elementmodel.Element transform(ByteProvider source, FhirFormat cntType, String mapUri)
throws FHIRException, IOException {
SimpleWorkerContext context = this.getContext();

// usual case is that source and target are in the same FHIR version as in the context, however it could be that either source or target are in a different FHIR version
// if this is the case we do lazy loading of the additional FHIR version into the context

StructureMap map = context.fetchResource(StructureMap.class, mapUri);
String fhirVersion = getFhirVersion(getCanonicalFromStructureMap(map, StructureMap.StructureMapModelMode.SOURCE));
if (!fhirVersion.equals(this.getVersion())) {
log.info("Loading additional FHIR version for Source into context" + fhirVersion);
context.loadFromPackage("hl7.fhir.r5.core", fhirVersion);
}
org.hl7.fhir.r5.elementmodel.Element src = Manager.parseSingle(context, new ByteArrayInputStream(source.getBytes()),
cntType);
return transform(src, mapUri);
Expand Down Expand Up @@ -405,12 +418,41 @@ public org.hl7.fhir.r5.elementmodel.Element transform(org.hl7.fhir.r5.elementmod
}
log.info("Using map " + map.getUrl() + (map.getVersion()!=null ? "|" + map.getVersion() + " " : "" )
+ (map.getDateElement() != null && !map.getDateElement().isEmpty() ? "(" + map.getDateElement().asStringValue() + ")" : ""));

org.hl7.fhir.r5.elementmodel.Element resource = getTargetResourceFromStructureMap(map);
scu.transform(null, src, map, resource);
resource.populatePaths(null);
return resource;
}

/**
* returns the explication FHIR version of it the FHIR resource contains the version inside the url
* http://hl7.org/fhir/3.0/StructureDefinition/Account
* @param url
* @return
*/
private String getFhirVersion(String url) {
return VersionUtilities.getMajMin(url);
}

/**
* gets the canonical for either source or target, we assume currently that the fhir source or target is the default canonical for the source and target, this might not be true
* however this approach is used in the getTargetResourceFromStructureMap below
* @param map
* @param mode
* @return
*/
private String getCanonicalFromStructureMap(StructureMap map, StructureMap.StructureMapModelMode mode) {
String targetTypeUrl = null;
for (StructureMap.StructureMapStructureComponent component : map.getStructure()) {
if (component.getMode() == mode) {
targetTypeUrl = component.getUrl();
break;
}
}
return targetTypeUrl;
}

private org.hl7.fhir.r5.elementmodel.Element getTargetResourceFromStructureMap(StructureMap map) {
String targetTypeUrl = null;
SimpleWorkerContext context = this.getContext();
Expand Down

0 comments on commit e35b68b

Please sign in to comment.