diff --git a/src/main/java/de/uksh/medic/etl/OpenEhrObds.java b/src/main/java/de/uksh/medic/etl/OpenEhrObds.java index 29e8601..511d94b 100644 --- a/src/main/java/de/uksh/medic/etl/OpenEhrObds.java +++ b/src/main/java/de/uksh/medic/etl/OpenEhrObds.java @@ -26,6 +26,7 @@ import java.util.*; import java.util.Map.Entry; import java.util.stream.Collectors; +import javax.xml.bind.JAXBException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPathExpressionException; import org.tinylog.Logger; @@ -34,6 +35,7 @@ public final class OpenEhrObds { private static final Map> FHIR_ATTRIBUTES = new HashMap<>(); + private static Integer i = 0; private OpenEhrObds() { } @@ -92,6 +94,12 @@ public static void walkXmlTree(Set> xmlSet, int depth, if (depth > Settings.getDepthLimit()) { return; } + + Boolean split = Settings.getMapping().containsKey(path) + && Settings.getMapping().get(path).getSplit(); + + Map theMap = resMap; + if (Settings.getMapping().containsKey(path) && Settings.getMapping().get(path).getSource() != null) { Mapping m = Settings.getMapping().get(path); @@ -102,52 +110,33 @@ public static void walkXmlTree(Set> xmlSet, int depth, mapped.entrySet().forEach(e -> queryFhirTs(m, e)); Map result = formatMap((Map) mapped); - buildOpenEhrComposition(result); + result.putAll(resMap); + theMap = result; - resMap.putAll(result); + if (split) { + buildOpenEhrComposition(result, i++); + } } - xmlSet.forEach(entry -> { + for (Map.Entry entry : xmlSet) { String newPath = path + "/" + entry.getKey(); int newDepth = depth + 1; switch (entry.getValue()) { case @SuppressWarnings("rawtypes") Map h -> { - if (Settings.getMapping().containsKey(newPath) - && Settings.getMapping().get(newPath).getSplit()) { - Map m = new LinkedHashMap<>(); - System.out.println(entry.getKey()); - walkXmlTree(h.entrySet(), newDepth, newPath, m); - resMap.put(newPath, List.of(m)); - } else { - walkXmlTree(h.entrySet(), newDepth, newPath, resMap); - } + walkXmlTree(h.entrySet(), newDepth, newPath, theMap); } case @SuppressWarnings("rawtypes") List a -> { - - if (Settings.getMapping().containsKey(newPath) - && Settings.getMapping().get(newPath).getSplit()) { - List> l = new ArrayList<>(); - a.forEach(b -> { - Map m = new LinkedHashMap<>(); - l.add(m); - System.out.println(entry.getKey()); - walkXmlTree(((Map) b).entrySet(), newDepth, newPath, m); - }); - resMap.put(newPath, l); - } else { - a.forEach(b -> { - System.out.println(entry.getKey()); - walkXmlTree(((Map) b).entrySet(), newDepth, newPath, resMap); - }); + for (Object b : a) { + System.out.println(entry.getKey()); + walkXmlTree(((Map) b).entrySet(), newDepth, newPath, theMap); } } - default -> { } } - }); + } } @@ -157,7 +146,7 @@ private static void listConv(Map input) { return; } switch (e.getValue()) { - case List l -> { + case @SuppressWarnings("rawtypes") List l -> { } default -> { List l = new ArrayList<>(); @@ -240,11 +229,14 @@ private static Map convertMdr(Set> xml return null; } - private static void buildOpenEhrComposition(Map data) { + @SuppressWarnings("unchecked") + private static void buildOpenEhrComposition(Map data, int i) { EHRParser ep = new EHRParser(); - try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.json"))) { + try (BufferedWriter writer = new BufferedWriter( + new FileWriter(i + "_" + ((List) data.get("ehr_id")).get(0) + ".json"))) { writer.write(ep.build(data)); - } catch (XPathExpressionException | IOException | ParserConfigurationException | SAXException e) { + } catch (XPathExpressionException | IOException | ParserConfigurationException | SAXException + | JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); }