Skip to content

Commit

Permalink
query fhir ts based on mdr output
Browse files Browse the repository at this point in the history
  • Loading branch information
nr23730 committed Feb 21, 2024
1 parent f64756a commit 963ccda
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 2 deletions.
40 changes: 40 additions & 0 deletions src/main/java/de/uksh/medic/etl/OpenEhrObds.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import de.uksh.medic.etl.jobs.FhirResolver;
import de.uksh.medic.etl.jobs.mdr.centraxx.CxxMdrAttributes;
import de.uksh.medic.etl.jobs.mdr.centraxx.CxxMdrConvert;
import de.uksh.medic.etl.jobs.mdr.centraxx.CxxMdrItemSet;
import de.uksh.medic.etl.jobs.mdr.centraxx.CxxMdrLogin;
import de.uksh.medic.etl.model.FhirAttributes;
import de.uksh.medic.etl.model.mdr.centraxx.CxxItemSet;
import de.uksh.medic.etl.model.mdr.centraxx.RelationConvert;
import de.uksh.medic.etl.settings.ConfigurationLoader;
import de.uksh.medic.etl.settings.CxxMdrSettings;
Expand All @@ -15,13 +19,16 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.tinylog.Logger;

public final class OpenEhrObds {

private static final Map<String, Map<String, FhirAttributes>> ATTRIBUTE_CACHE = new HashMap<>();

private OpenEhrObds() {
}

Expand All @@ -40,6 +47,22 @@ public static void main(String[] args) throws IOException {
CxxMdrLogin.login(mdrSettings);
}

Settings.getMapping().values().forEach(m -> {
CxxItemSet is = CxxMdrItemSet.get(Settings.getCxxmdr(), m.getTarget());
is.getItems().forEach(it -> {
try {
if (!ATTRIBUTE_CACHE.containsKey(m.getTarget())) {
ATTRIBUTE_CACHE.put(m.getTarget(), new HashMap<>());
}
ATTRIBUTE_CACHE.getOrDefault(m.getTarget(), new HashMap<>()).put(it.getId(),
CxxMdrAttributes.getAttributes(Settings.getCxxmdr(), m.getTarget(), it.getId()));
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
});

XmlMapper xmlMapper = new XmlMapper();

// ToDo: Replace with Kafka consumer
Expand Down Expand Up @@ -69,6 +92,11 @@ public static void walkXmlTree(Set<Map.Entry<String, Object>> xmlSet, int depth,

try {
RelationConvert res = CxxMdrConvert.convert(Settings.getCxxmdr(), conv);

res.getValues().entrySet().forEach(e -> {
queryFhirTs(m, e);
});

System.out.println(res);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
Expand All @@ -92,4 +120,16 @@ public static void walkXmlTree(Set<Map.Entry<String, Object>> xmlSet, int depth,
}
}

private static void queryFhirTs(Mapping m, Map.Entry<String, Object> e) {
FhirAttributes fa = ATTRIBUTE_CACHE.get(m.getTarget()).getOrDefault(e.getKey(), null);
if (fa != null) {
if (fa.getVersion() != null) {
e.setValue(FhirResolver.lookUp(fa.getSystem(), fa.getVersion(), (String) e.getValue()));
} else if (fa.getConceptMap() != null) {
e.setValue(FhirResolver.conceptMap(fa.getConceptMap(), fa.getId(), fa.getSource(),
fa.getTarget(), (String) e.getValue()).getCode());
}
}
}

}
4 changes: 2 additions & 2 deletions src/main/java/de/uksh/medic/etl/jobs/FhirResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public static Coding conceptMap(URI conceptMapUri, String conceptMapId, URI sour
return new Coding();
}

public static String lookUp(URI source, String version, String code) {
public static String lookUp(URI system, String version, String code) {
Parameters params = new Parameters();
params.addParameter("system", new UriType(source));
params.addParameter("system", new UriType(system));
params.addParameter("code", code);
params.addParameter("version", version);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package de.uksh.medic.etl.jobs.mdr.centraxx;

import de.uksh.medic.etl.model.FhirAttributes;
import de.uksh.medic.etl.model.mdr.centraxx.CxxAttributeValue;
import de.uksh.medic.etl.model.mdr.centraxx.CxxList;
import de.uksh.medic.etl.settings.CxxMdrSettings;
import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import org.tinylog.Logger;

/**
* Class to fetch attributes from a Kairos CentraXX MDR.
*/
public final class CxxMdrAttributes {

private CxxMdrAttributes() {
}

/**
* Retrieves a FhirAttributes object for a specified MDR attribute.
*
* @param mdr Configuration for MDR.
* @param mdrProfile profile / form / ItemSet where the item is defined
* @param key key of the requested attribute
* @return FhirAttributes object
* @throws URISyntaxException
*/
public static FhirAttributes getAttributes(CxxMdrSettings mdr, String mdrProfile, String key)
throws URISyntaxException {

if (mdr.isTokenExpired()) {
CxxMdrLogin.login(mdr);
}

MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.set("code", mdrProfile);
form.set("domainCode", "fhir");
form.set("itemCode", key);

RestTemplate rt = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(mdr.getUrl() + "/rest/v1/itemsets/attributes/item");
builder.queryParams(form);
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString());
headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString());
headers.add("Authorization", "Bearer " + mdr.getToken());
try {
ResponseEntity<CxxList> response = rt.exchange(builder.build().encode().toUri(), HttpMethod.GET,
new HttpEntity<>(headers), CxxList.class);
CxxList l = response.getBody();
if (l != null && l.getContent() != null) {
FhirAttributes ch = new FhirAttributes();
for (CxxAttributeValue av : l.getContent()) {
switch (av.getAttribute()) {
case SYSTEM -> ch.setSystem(new URI(av.getValue()));
case SOURCE -> ch.setSource(new URI(av.getValue()));
case TARGET -> ch.setTarget(new URI(av.getValue()));
case ID -> ch.setId(av.getValue());
case CONCEPTMAP -> ch.setConceptMap(new URI(av.getValue()));
case CODE -> ch.setCode(av.getValue());
case VERSION -> ch.setVersion(av.getValue());
default -> {
}
}
}
return ch;
}
return null;

} catch (final HttpClientErrorException e) {
Logger.error("Object " + form.get("itemCode") + " not found in MDR!");
return null;
}
}

}
74 changes: 74 additions & 0 deletions src/main/java/de/uksh/medic/etl/model/FhirAttributes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package de.uksh.medic.etl.model;

import java.net.URI;

public class FhirAttributes {

private URI system;
private URI source;
private URI target;
private URI conceptMap;
private String id;
private String code;
private String version;

public FhirAttributes() {
}

public URI getSystem() {
return system;
}

public void setSystem(URI system) {
this.system = system;
}

public URI getSource() {
return source;
}

public void setSource(URI source) {
this.source = source;
}

public URI getTarget() {
return target;
}

public void setTarget(URI target) {
this.target = target;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public URI getConceptMap() {
return conceptMap;
}

public void setConceptMap(URI conceptMap) {
this.conceptMap = conceptMap;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

}
66 changes: 66 additions & 0 deletions src/main/java/de/uksh/medic/etl/model/mdr/MdrAttributes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package de.uksh.medic.etl.model.mdr;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

/**
* Enum for Attributes queried from MDR.
*/
public enum MdrAttributes {
/**
* system attribute in MDR.
*/
SYSTEM("system"),
/**
* source attribute in MDR.
*/
SOURCE("source"),
/**
* target attribute in MDR.
*/
TARGET("target"),
/**
* ID attribute in MDR.
*/
ID("id"),
/**
* conceptMap attribute in MDR.
*/
CONCEPTMAP("conceptMap"),
/**
* code attribute in MDR.
*/
CODE("code"),
/**
* version attribute in MDR.
*/
VERSION("version");

private final String label;

MdrAttributes(String label) {
this.label = label;
}

@Override
@JsonValue
public String toString() {
return this.label;
}

/**
* Look Enum up from String.
* @param s string to be looked up
* @return corresponding Enum
*/
@JsonCreator
public static MdrAttributes fromString(String s) {
for (MdrAttributes a : MdrAttributes.values()) {
if (a.label.equalsIgnoreCase(s)) {
return a;
}
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package de.uksh.medic.etl.model.mdr.centraxx;

import de.uksh.medic.etl.model.mdr.MdrAttributes;
import java.util.List;

/**
* Model for AttributeValue in Kairos CentraXX MDR.
*/
public class CxxAttributeValue {

private String domain;
private MdrAttributes attribute;
private String value;
private List<CxxLinks> links;

public String getDomain() {
return domain;
}

public MdrAttributes getAttribute() {
return attribute;
}

public String getValue() {
return value;
}

public List<CxxLinks> getLinks() {
return links;
}

}
21 changes: 21 additions & 0 deletions src/main/java/de/uksh/medic/etl/model/mdr/centraxx/CxxList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.uksh.medic.etl.model.mdr.centraxx;

import java.util.List;

/**
* Model for List in Kairos CentraXX MDR.
*/
public class CxxList {

private List<CxxAttributeValue> content;
private List<CxxLinks> links;

public List<CxxAttributeValue> getContent() {
return content;
}

public List<CxxLinks> getLinks() {
return links;
}

}

0 comments on commit 963ccda

Please sign in to comment.