diff --git a/src/main/java/de/uksh/medic/etl/OpenEhrObds.java b/src/main/java/de/uksh/medic/etl/OpenEhrObds.java index 77e3fa8..f57d67d 100644 --- a/src/main/java/de/uksh/medic/etl/OpenEhrObds.java +++ b/src/main/java/de/uksh/medic/etl/OpenEhrObds.java @@ -125,7 +125,7 @@ public static void main(String[] args) throws IOException { // File f = new File("file_1705482004-clean.xml"); File f = new File("file_1705482019-clean.xml"); // File f = new File("file_1705482057-clean.xml"); - // File f = new File("st.xml"); + // File f = new File("syst.xml"); Map m = new LinkedHashMap<>(); walkXmlTree(xmlMapper.readValue(f, new TypeReference>() { @@ -247,8 +247,25 @@ private static void splitMap(Object value, List key, Map new LinkedHashMap<>()); out.put(k, m); splitMap(value, key, m); - } else if (key.size() == 1) { + } else if (key.size() == 1 && !out.containsKey(key.getFirst())) { out.put(key.removeFirst(), value); + } else if (key.size() == 1 && out.containsKey(key.getFirst())) { + if (value instanceof List && out.get(key.getFirst()) instanceof List) { + ((List) out.get(key.getFirst())).addAll((List) value); + } + if (value instanceof List && out.get(key.getFirst()) instanceof Map) { + ((List>) value) + .forEach(m -> m.putAll((Map) out.get(key.getFirst()))); + out.put(key.getFirst(), value); + } + if (value instanceof Map && out.get(key.getFirst()) instanceof Map) { + ((Map) out.get(key.getFirst())).putAll((Map) value); + } + if (value instanceof Map && out.get(key.getFirst()) instanceof List) { + ((List>) out.get(key.getFirst())) + .forEach(l -> l.putAll((Map) value)); + out.put(key.getFirst(), value); + } } } diff --git a/src/main/java/de/uksh/medic/etl/openehrmapper/Generator.java b/src/main/java/de/uksh/medic/etl/openehrmapper/Generator.java index 098dc99..f4c214b 100644 --- a/src/main/java/de/uksh/medic/etl/openehrmapper/Generator.java +++ b/src/main/java/de/uksh/medic/etl/openehrmapper/Generator.java @@ -126,23 +126,32 @@ public void gen_ADMIN_ENTRY(String path, String name, Object jsonmap, Map) map.get(paramName)); - adminEntry.setData(il); + List> l; + if (map.containsKey(paramName) && map.get(paramName) instanceof List) { + l = (List>) map.get(paramName); + } else { + l = List.of((Map) map.get(paramName)); + } + + l.forEach(le -> { + + AdminEntry adminEntry = new AdminEntry(); + adminEntry.setArchetypeDetails(new Archetyped(new ArchetypeID(paramName), "1.1.0")); + adminEntry.setArchetypeNodeId(paramName); + adminEntry.setNameAsString(label); + adminEntry.setLanguage(new CodePhrase(new TerminologyId("ISO_639-1"), "de")); + adminEntry.setEncoding(new CodePhrase(new TerminologyId("IANA_character-sets"), "UTF-8")); + adminEntry.setSubject(new PartySelf()); + + ItemTree itemTree = new ItemTree(); + processAttributeChildren(oap, paramName, itemTree, le); + adminEntry.setData(itemTree); if (oa) { ((ArrayList) jsonmap).add(adminEntry); } - } + }); } @SuppressWarnings("unchecked") @@ -151,23 +160,32 @@ public void gen_OBSERVATION(String path, String name, Object jsonmap, Map> l; + if (map.containsKey(paramName) && map.get(paramName) instanceof List) { + l = (List>) map.get(paramName); + } else { + l = List.of((Map) map.get(paramName)); + } + + l.forEach(le -> { - Observation observation = new Observation(); - observation.setArchetypeDetails(new Archetyped(new ArchetypeID(paramName), "1.1.0")); - observation.setArchetypeNodeId(paramName); - observation.setNameAsString(getLabel(getNodeId(path), paramName)); - observation.setLanguage(new CodePhrase(new TerminologyId("ISO_639-1"), "de")); - observation.setEncoding(new CodePhrase(new TerminologyId("IANA_character-sets"), "UTF-8")); - observation.setSubject(new PartySelf()); + Observation observation = new Observation(); + observation.setArchetypeDetails(new Archetyped(new ArchetypeID(paramName), "1.1.0")); + observation.setArchetypeNodeId(paramName); + observation.setNameAsString(label); + observation.setLanguage(new CodePhrase(new TerminologyId("ISO_639-1"), "de")); + observation.setEncoding(new CodePhrase(new TerminologyId("IANA_character-sets"), "UTF-8")); + observation.setSubject(new PartySelf()); - if (map.containsKey(paramName)) { History history = new History(); - processAttributeChildren(oap, paramName, history, (Map) map.get(paramName)); + processAttributeChildren(oap, paramName, history, le); observation.setData(history); if (oa) { ((List) jsonmap).add(observation); } - } + }); } @SuppressWarnings("unchecked") @@ -176,23 +194,32 @@ public void gen_EVALUATION(String path, String name, Object jsonmap, Map> l; + if (map.containsKey(paramName) && map.get(paramName) instanceof List) { + l = (List>) map.get(paramName); + } else { + l = List.of((Map) map.get(paramName)); + } + + l.forEach(le -> { - Evaluation evaluation = new Evaluation(); - evaluation.setArchetypeDetails(new Archetyped(new ArchetypeID(paramName), "1.1.0")); - evaluation.setArchetypeNodeId(paramName); - evaluation.setNameAsString(getLabel(getNodeId(path), paramName)); - evaluation.setLanguage(new CodePhrase(new TerminologyId("ISO_639-1"), "de")); - evaluation.setEncoding(new CodePhrase(new TerminologyId("IANA_character-sets"), "UTF-8")); - evaluation.setSubject(new PartySelf()); + Evaluation evaluation = new Evaluation(); + evaluation.setArchetypeDetails(new Archetyped(new ArchetypeID(paramName), "1.1.0")); + evaluation.setArchetypeNodeId(paramName); + evaluation.setNameAsString(label); + evaluation.setLanguage(new CodePhrase(new TerminologyId("ISO_639-1"), "de")); + evaluation.setEncoding(new CodePhrase(new TerminologyId("IANA_character-sets"), "UTF-8")); + evaluation.setSubject(new PartySelf()); - if (map.containsKey(paramName)) { ItemTree data = new ItemTree(); - processAttributeChildren(oap, paramName, data, (Map) map.get(paramName)); + processAttributeChildren(oap, paramName, data, le); evaluation.setData(data); if (oa) { ((List) jsonmap).add(evaluation); } - } + }); } @@ -204,27 +231,36 @@ public void gen_INSTRUCTION(String path, String name, Object jsonmap, Map> l; + if (map.containsKey(paramName) && map.get(paramName) instanceof List) { + l = (List>) map.get(paramName); + } else { + l = List.of((Map) map.get(paramName)); + } + + l.forEach(le -> { - Instruction instruction = new Instruction(); - instruction.setArchetypeDetails(new Archetyped(new ArchetypeID(paramName), "1.1.0")); - instruction.setArchetypeNodeId(paramName); - instruction.setNameAsString(getLabel(getNodeId(path), paramName)); - instruction.setLanguage(new CodePhrase(new TerminologyId("ISO_639-1"), "de")); - instruction.setEncoding(new CodePhrase(new TerminologyId("IANA_character-sets"), "UTF-8")); - instruction.setSubject(new PartySelf()); - instruction.setNarrative(new DvText("")); + Instruction instruction = new Instruction(); + instruction.setArchetypeDetails(new Archetyped(new ArchetypeID(paramName), "1.1.0")); + instruction.setArchetypeNodeId(paramName); + instruction.setNameAsString(label); + instruction.setLanguage(new CodePhrase(new TerminologyId("ISO_639-1"), "de")); + instruction.setEncoding(new CodePhrase(new TerminologyId("IANA_character-sets"), "UTF-8")); + instruction.setSubject(new PartySelf()); + instruction.setNarrative(new DvText("")); - if (map.containsKey(paramName)) { List activities = new ArrayList<>(); ItemTree protocol = new ItemTree(); - processAttributeChildren(oapActivities, paramName, activities, (Map) map.get(paramName)); - processAttributeChildren(oapProtocol, paramName, protocol, (Map) map.get(paramName)); + processAttributeChildren(oapActivities, paramName, activities, le); + processAttributeChildren(oapProtocol, paramName, protocol, le); instruction.setActivities(activities); instruction.setProtocol(protocol); if (oaActivities || oaProtocol) { ((ArrayList) jsonmap).add(instruction); } - } + }); } @SuppressWarnings("unchecked") @@ -233,20 +269,29 @@ public void gen_ACTIVITY(String path, String name, Object jsonmap, Map> l; + if (map.containsKey(nodeId) && map.get(nodeId) instanceof List) { + l = (List>) map.get(nodeId); + } else { + l = List.of((Map) map.get(nodeId)); + } - Activity activity = new Activity(); - activity.setActionArchetypeId("openEHR-EHR-INSTRUCTION.service_request.v1"); - activity.setArchetypeNodeId(nodeId); - activity.setNameAsString(getLabel(nodeId, name)); + l.forEach(le -> { + + Activity activity = new Activity(); + activity.setActionArchetypeId("openEHR-EHR-INSTRUCTION.service_request.v1"); + activity.setArchetypeNodeId(nodeId); + activity.setNameAsString(label); - if (map.containsKey(nodeId)) { ItemTree itemTree = new ItemTree(); - processAttributeChildren(oap, name, itemTree, (Map) map.get(nodeId)); + processAttributeChildren(oap, name, itemTree, le); activity.setDescription(itemTree); if (oa) { ((ArrayList) jsonmap).add(activity); } - } + }); } @SuppressWarnings("unchecked") @@ -257,32 +302,40 @@ public void gen_ACTION(String path, String name, Object jsonmap, Map> l; + if (map.containsKey(paramName) && map.get(paramName) instanceof List) { + l = (List>) map.get(paramName); + } else { + l = List.of((Map) map.get(paramName)); + } - Action action = new Action(); - action.setArchetypeDetails(new Archetyped(new ArchetypeID(paramName), "1.1.0")); - action.setArchetypeNodeId(paramName); - action.setNameAsString(getLabel(getNodeId(path), paramName)); - action.setLanguage(new CodePhrase(new TerminologyId("ISO_639-1"), "de")); - action.setEncoding(new CodePhrase(new TerminologyId("IANA_character-sets"), "UTF-8")); - action.setSubject(new PartySelf()); - IsmTransition ism = new IsmTransition(); - ism.setCurrentState( - new DvCodedText("completed", new CodePhrase(new TerminologyId("openehr"), "532", "completed"))); - action.setIsmTransition(ism); - - if (map.containsKey(paramName)) { - action.setTime(new DvDateTime(((Map>) map.get(paramName)).get("time").get(0))); + l.forEach(le -> { + Action action = new Action(); + action.setArchetypeDetails(new Archetyped(new ArchetypeID(paramName), "1.1.0")); + action.setArchetypeNodeId(paramName); + action.setNameAsString(label); + action.setLanguage(new CodePhrase(new TerminologyId("ISO_639-1"), "de")); + action.setEncoding(new CodePhrase(new TerminologyId("IANA_character-sets"), "UTF-8")); + action.setSubject(new PartySelf()); + IsmTransition ism = new IsmTransition(); + ism.setCurrentState( + new DvCodedText("completed", new CodePhrase(new TerminologyId("openehr"), "532", "completed"))); + action.setIsmTransition(ism); + action.setTime(new DvDateTime(((List) (le).get("time")).getFirst())); ItemTree description = new ItemTree(); ItemTree protocol = new ItemTree(); - processAttributeChildren(oapDescription, paramName, description, (Map) map.get(paramName)); - processAttributeChildren(oapProtocol, paramName, protocol, (Map) map.get(paramName)); + processAttributeChildren(oapDescription, paramName, description, le); + processAttributeChildren(oapProtocol, paramName, protocol, le); action.setDescription(description); action.setProtocol(protocol); if (oaDescription || oaProtocol) { ((ArrayList) jsonmap).add(action); } - } + }); + } // Item Structure @@ -338,13 +391,25 @@ public void gen_CLUSTER(String path, String name, Object jsonmap, return; } String label = getLabel(nodeId, paramName); - Cluster cluster = new Cluster(); - cluster.setArchetypeNodeId(aNodeId); - cluster.setNameAsString(label); - ArrayList items = new ArrayList(); - processAttributeChildren(newPath, paramName, items, (Map) map.get(code)); - cluster.setItems(items); - ((ArrayList) jsonmap).add(cluster); + + List> l; + if (map.containsKey(paramName) && map.get(paramName) instanceof List) { + l = (List>) map.get(paramName); + } else { + l = List.of((Map) map.get(paramName)); + } + + l.forEach(le -> { + + Cluster cluster = new Cluster(); + cluster.setArchetypeNodeId(aNodeId); + cluster.setNameAsString(label); + ArrayList items = new ArrayList(); + processAttributeChildren(newPath, paramName, items, le); + cluster.setItems(items); + ((ArrayList) jsonmap).add(cluster); + + }); } @SuppressWarnings("unchecked") @@ -397,7 +462,7 @@ public void gen_HISTORY(String path, String name, Object jsonmap, Map) map.get("events_time")).get(0))); + history.setOrigin(new DvDateTime((String) ((List) map.get("events_time")).getFirst())); processAttributeChildren(newPath, nodeId, history, map); } @@ -413,7 +478,7 @@ public void gen_EVENT(String path, String name, Object jsonmap, Map) map.get("events_time")).get(0))); + events.setTime(new DvDateTime((String) ((List) map.get("events_time")).getFirst())); ItemTree itemTree = new ItemTree(); processAttributeChildren(newPath, nodeId, itemTree, map); events.setData(itemTree);