Skip to content

Commit

Permalink
add xml mapping using treewalker
Browse files Browse the repository at this point in the history
  • Loading branch information
nr23730 committed Feb 21, 2024
1 parent 12452cc commit de2b203
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
Expand Down
65 changes: 62 additions & 3 deletions src/main/java/de/uksh/medic/etl/OpenEhrObds.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package de.uksh.medic.etl;

import com.fasterxml.jackson.core.JsonProcessingException;
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.CxxMdrConvert;
import de.uksh.medic.etl.jobs.mdr.centraxx.CxxMdrLogin;
import de.uksh.medic.etl.model.mdr.centraxx.RelationConvert;
import de.uksh.medic.etl.settings.ConfigurationLoader;
import de.uksh.medic.etl.settings.CxxMdrSettings;
import de.uksh.medic.etl.settings.Mapping;
import de.uksh.medic.etl.settings.Settings;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.tinylog.Logger;

public final class OpenEhrObds {

private OpenEhrObds() {}
private OpenEhrObds() {
}

public static void main(String[] args) throws IOException {
InputStream settingsYaml = ClassLoader.getSystemClassLoader().getResourceAsStream("settings.yml");
Expand All @@ -23,14 +34,62 @@ public static void main(String[] args) throws IOException {
ConfigurationLoader configLoader = new ConfigurationLoader();
configLoader.loadConfiguration(settingsYaml, Settings.class);

//ToDo: Replace with Kafka consumer

FhirResolver.initalize();
CxxMdrSettings mdrSettings = Settings.getCxxmdr();
if (mdrSettings != null) {
CxxMdrLogin.login(mdrSettings);
}

XmlMapper xmlMapper = new XmlMapper();

// ToDo: Replace with Kafka consumer

File f = new File("file_1705482057-clean.xml");

walkXmlTree(xmlMapper.readValue(f, new TypeReference<LinkedHashMap<String, Object>>() {
}).entrySet(), 1, "");

Logger.info("OpenEhrObds started!");
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public static void walkXmlTree(Set<Map.Entry<String, Object>> xmlSet, int depth, String path) {
if (depth <= Settings.getDepthLimit()) {
if (Settings.getMapping().containsKey(path)) {
System.out.println("Mapping " + path);

RelationConvert conv = new RelationConvert();
Mapping m = Settings.getMapping().get(path);
conv.setSourceProfileCode(m.getSource());
conv.setTargetProfileCode(m.getTarget());
conv.setSourceProfileVersion(m.getSourceVersion());
conv.setTargetProfileVersion(m.getTargetVersion());
conv.setValues(xmlSet.stream()
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)));

try {
RelationConvert res = CxxMdrConvert.convert(Settings.getCxxmdr(), conv);
System.out.println(res);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

xmlSet.forEach(entry -> {
String newPath = path + "/" + entry.getKey();
int newDepth = depth + 1;

switch (entry.getValue()) {
case Map h -> {
walkXmlTree(h.entrySet(), newDepth, newPath);
}
case List a -> a.forEach(b -> walkXmlTree(((Map) b).entrySet(), newDepth, newPath));
default -> {
}
}
});
}
}

}

0 comments on commit de2b203

Please sign in to comment.