Skip to content

Commit

Permalink
better handling for multiple occurences
Browse files Browse the repository at this point in the history
  • Loading branch information
nr23730 committed Mar 8, 2024
1 parent af28433 commit 5a9c11c
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 80 deletions.
21 changes: 19 additions & 2 deletions src/main/java/de/uksh/medic/etl/OpenEhrObds.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> m = new LinkedHashMap<>();
walkXmlTree(xmlMapper.readValue(f, new TypeReference<LinkedHashMap<String, Object>>() {
Expand Down Expand Up @@ -247,8 +247,25 @@ private static void splitMap(Object value, List<String> key, Map<String, Object>
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<Object>) out.get(key.getFirst())).addAll((List<Object>) value);
}
if (value instanceof List && out.get(key.getFirst()) instanceof Map) {
((List<Map<String, Object>>) value)
.forEach(m -> m.putAll((Map<String, Object>) out.get(key.getFirst())));
out.put(key.getFirst(), value);
}
if (value instanceof Map && out.get(key.getFirst()) instanceof Map) {
((Map<String, Object>) out.get(key.getFirst())).putAll((Map<String, Object>) value);
}
if (value instanceof Map && out.get(key.getFirst()) instanceof List) {
((List<Map<String, Object>>) out.get(key.getFirst()))
.forEach(l -> l.putAll((Map<String, Object>) value));
out.put(key.getFirst(), value);
}
}
}

Expand Down
221 changes: 143 additions & 78 deletions src/main/java/de/uksh/medic/etl/openehrmapper/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,32 @@ public void gen_ADMIN_ENTRY(String path, String name, Object jsonmap, Map<String
String paramName = getArcheTypeId(path);
String oap = path + "/attributes[rm_attribute_name=\"data\"]";
Boolean oa = (Boolean) XP.evaluate(oap, opt, XPathConstants.BOOLEAN);
String label = getLabel(getNodeId(path), paramName);

AdminEntry adminEntry = new AdminEntry();
adminEntry.setArchetypeDetails(new Archetyped(new ArchetypeID(paramName), "1.1.0"));
adminEntry.setArchetypeNodeId(paramName);
adminEntry.setNameAsString(getLabel(getNodeId(path), paramName));
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());

if (map.containsKey(paramName)) {
ItemTree il = new ItemTree();
processAttributeChildren(oap, paramName, il, (Map<String, Object>) map.get(paramName));
adminEntry.setData(il);
List<Map<String, Object>> l;
if (map.containsKey(paramName) && map.get(paramName) instanceof List) {
l = (List<Map<String, Object>>) map.get(paramName);
} else {
l = List.of((Map<String, Object>) 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<ContentItem>) jsonmap).add(adminEntry);
}
}
});
}

@SuppressWarnings("unchecked")
Expand All @@ -151,23 +160,32 @@ public void gen_OBSERVATION(String path, String name, Object jsonmap, Map<String
String paramName = getArcheTypeId(path);
String oap = path + "/attributes[rm_attribute_name=\"data\"]";
Boolean oa = (Boolean) XP.evaluate(oap, opt, XPathConstants.BOOLEAN);
String label = getLabel(getNodeId(path), paramName);

List<Map<String, Object>> l;
if (map.containsKey(paramName) && map.get(paramName) instanceof List) {
l = (List<Map<String, Object>>) map.get(paramName);
} else {
l = List.of((Map<String, Object>) 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<ItemStructure> history = new History<ItemStructure>();
processAttributeChildren(oap, paramName, history, (Map<String, Object>) map.get(paramName));
processAttributeChildren(oap, paramName, history, le);
observation.setData(history);
if (oa) {
((List<ContentItem>) jsonmap).add(observation);
}
}
});
}

@SuppressWarnings("unchecked")
Expand All @@ -176,23 +194,32 @@ public void gen_EVALUATION(String path, String name, Object jsonmap, Map<String,
String paramName = getArcheTypeId(path);
String oap = path + "/attributes[rm_attribute_name=\"data\"]";
Boolean oa = (Boolean) XP.evaluate(oap, opt, XPathConstants.BOOLEAN);
String label = getLabel(getNodeId(path), paramName);

List<Map<String, Object>> l;
if (map.containsKey(paramName) && map.get(paramName) instanceof List) {
l = (List<Map<String, Object>>) map.get(paramName);
} else {
l = List.of((Map<String, Object>) 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<String, Object>) map.get(paramName));
processAttributeChildren(oap, paramName, data, le);
evaluation.setData(data);
if (oa) {
((List<ContentItem>) jsonmap).add(evaluation);
}
}
});

}

Expand All @@ -204,27 +231,36 @@ public void gen_INSTRUCTION(String path, String name, Object jsonmap, Map<String
String oapProtocol = path + "/attributes[rm_attribute_name=\"protocol\"]";
Boolean oaActivities = (Boolean) XP.evaluate(oapActivities, opt, XPathConstants.BOOLEAN);
Boolean oaProtocol = (Boolean) XP.evaluate(oapProtocol, opt, XPathConstants.BOOLEAN);
String label = getLabel(getNodeId(path), paramName);

List<Map<String, Object>> l;
if (map.containsKey(paramName) && map.get(paramName) instanceof List) {
l = (List<Map<String, Object>>) map.get(paramName);
} else {
l = List.of((Map<String, Object>) 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<Activity> activities = new ArrayList<>();
ItemTree protocol = new ItemTree();
processAttributeChildren(oapActivities, paramName, activities, (Map<String, Object>) map.get(paramName));
processAttributeChildren(oapProtocol, paramName, protocol, (Map<String, Object>) map.get(paramName));
processAttributeChildren(oapActivities, paramName, activities, le);
processAttributeChildren(oapProtocol, paramName, protocol, le);
instruction.setActivities(activities);
instruction.setProtocol(protocol);
if (oaActivities || oaProtocol) {
((ArrayList<ContentItem>) jsonmap).add(instruction);
}
}
});
}

@SuppressWarnings("unchecked")
Expand All @@ -233,20 +269,29 @@ public void gen_ACTIVITY(String path, String name, Object jsonmap, Map<String, O
String nodeId = getNodeId(path);
String oap = path + "/attributes[rm_attribute_name=\"description\"]";
Boolean oa = (Boolean) XP.evaluate(oap, opt, XPathConstants.BOOLEAN);
String label = getLabel(nodeId, name);

List<Map<String, Object>> l;
if (map.containsKey(nodeId) && map.get(nodeId) instanceof List) {
l = (List<Map<String, Object>>) map.get(nodeId);
} else {
l = List.of((Map<String, Object>) 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<String, Object>) map.get(nodeId));
processAttributeChildren(oap, name, itemTree, le);
activity.setDescription(itemTree);
if (oa) {
((ArrayList<Activity>) jsonmap).add(activity);
}
}
});
}

@SuppressWarnings("unchecked")
Expand All @@ -257,32 +302,40 @@ public void gen_ACTION(String path, String name, Object jsonmap, Map<String, Obj
String oapProtocol = path + "/attributes[rm_attribute_name=\"protocol\"]";
Boolean oaDescription = (Boolean) XP.evaluate(oapDescription, opt, XPathConstants.BOOLEAN);
Boolean oaProtocol = (Boolean) XP.evaluate(oapProtocol, opt, XPathConstants.BOOLEAN);
String label = getLabel(getNodeId(path), paramName);

List<Map<String, Object>> l;
if (map.containsKey(paramName) && map.get(paramName) instanceof List) {
l = (List<Map<String, Object>>) map.get(paramName);
} else {
l = List.of((Map<String, Object>) 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<String, List<String>>) 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<String>) (le).get("time")).getFirst()));
ItemTree description = new ItemTree();
ItemTree protocol = new ItemTree();
processAttributeChildren(oapDescription, paramName, description, (Map<String, Object>) map.get(paramName));
processAttributeChildren(oapProtocol, paramName, protocol, (Map<String, Object>) map.get(paramName));
processAttributeChildren(oapDescription, paramName, description, le);
processAttributeChildren(oapProtocol, paramName, protocol, le);
action.setDescription(description);
action.setProtocol(protocol);

if (oaDescription || oaProtocol) {
((ArrayList<ContentItem>) jsonmap).add(action);
}
}
});

}

// Item Structure
Expand Down Expand Up @@ -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<Item> items = new ArrayList<Item>();
processAttributeChildren(newPath, paramName, items, (Map<String, Object>) map.get(code));
cluster.setItems(items);
((ArrayList<Object>) jsonmap).add(cluster);

List<Map<String, Object>> l;
if (map.containsKey(paramName) && map.get(paramName) instanceof List) {
l = (List<Map<String, Object>>) map.get(paramName);
} else {
l = List.of((Map<String, Object>) map.get(paramName));
}

l.forEach(le -> {

Cluster cluster = new Cluster();
cluster.setArchetypeNodeId(aNodeId);
cluster.setNameAsString(label);
ArrayList<Item> items = new ArrayList<Item>();
processAttributeChildren(newPath, paramName, items, le);
cluster.setItems(items);
((ArrayList<Object>) jsonmap).add(cluster);

});
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -397,7 +462,7 @@ public void gen_HISTORY(String path, String name, Object jsonmap, Map<String, Ob

history.setNameAsString(label);

history.setOrigin(new DvDateTime((String) ((List<String>) map.get("events_time")).get(0)));
history.setOrigin(new DvDateTime((String) ((List<String>) map.get("events_time")).getFirst()));

processAttributeChildren(newPath, nodeId, history, map);
}
Expand All @@ -413,7 +478,7 @@ public void gen_EVENT(String path, String name, Object jsonmap, Map<String, Obje

events.setNameAsString(label);

events.setTime(new DvDateTime((String) ((List<String>) map.get("events_time")).get(0)));
events.setTime(new DvDateTime((String) ((List<String>) map.get("events_time")).getFirst()));
ItemTree itemTree = new ItemTree();
processAttributeChildren(newPath, nodeId, itemTree, map);
events.setData(itemTree);
Expand Down

0 comments on commit 5a9c11c

Please sign in to comment.