use of com.nedap.archie.openehrtestrm.Element in project openEHR_SDK by ehrbase.
the class Walker method handle.
private void handle(Context<T> context) {
preHandle(context);
WebTemplateNode currentNode = context.getNodeDeque().peek();
if (visitChildren(currentNode)) {
if (ACTION.equals(currentNode.getRmType())) {
List<WebTemplateNode> ismTransitionList = currentNode.getChildren().stream().filter(n -> ISM_TRANSITION.equals(n.getRmType())).collect(Collectors.toList());
if (!ismTransitionList.isEmpty()) {
currentNode.getChildren().removeAll(ismTransitionList);
currentNode.getChildren().add(ismTransitionList.get(0));
}
}
currentNode.getChildren().forEach(this::handleDVText);
Map<String, List<WebTemplateNode>> choices = currentNode.getChoicesInChildren();
List<WebTemplateNode> children = new ArrayList<>(currentNode.getChildren());
if (children.stream().anyMatch(n -> n.getRmType().equals("EVENT"))) {
WebTemplateNode event = children.stream().filter(n -> n.getRmType().equals("EVENT")).findAny().orElseThrow();
EventHelper eventHelper = new EventHelper(event).invoke();
WebTemplateNode pointEvent = eventHelper.getPointEvent();
WebTemplateNode intervalEvent = eventHelper.getIntervalEvent();
choices.put(intervalEvent.getAqlPath(), List.of(intervalEvent, pointEvent));
children.add(intervalEvent);
children.add(pointEvent);
children.remove(event);
}
Map<String, List<WebTemplateNode>> map = new LinkedHashMap<>();
for (WebTemplateNode webTemplateNode : children) {
map.computeIfAbsent(webTemplateNode.getAqlPath(), k -> new ArrayList<>()).add(webTemplateNode);
}
Map<String, List<WebTemplateNode>> result = new LinkedHashMap<>();
for (Map.Entry<String, List<WebTemplateNode>> stringListEntry : map.entrySet()) {
if (result.put(stringListEntry.getKey(), stringListEntry.getValue()) != null) {
throw new IllegalStateException("Duplicate key");
}
}
Collection<List<WebTemplateNode>> childChoices = result.values();
for (List<WebTemplateNode> choice : childChoices) {
if (choice.stream().noneMatch(WebTemplateNode::isMulti) || currentNode.getRmType().equals(ELEMENT)) {
for (WebTemplateNode childNode : choice) {
ImmutablePair<T, RMObject> pair = extractPair(context, currentNode, choices, childNode, null);
T childObject = pair.getLeft();
RMObject child = pair.getRight();
if (child != null && childObject != null) {
context.getNodeDeque().push(childNode);
context.getObjectDeque().push(childObject);
context.getRmObjectDeque().push(child);
handle(context);
}
}
} else {
int size = calculateSize(context, choice.get(0));
Map<Integer, Triple<T, RMObject, WebTemplateNode>> pairs = new HashMap<>();
for (int i = 0; i < size; i++) {
for (WebTemplateNode childNode : choice) {
ImmutablePair<T, RMObject> pair = extractPair(context, currentNode, choices, childNode, i);
if (pair.getLeft() != null && pair.getRight() != null) {
pairs.put(i, new ImmutableTriple<>(pair.getLeft(), pair.getRight(), childNode));
}
}
}
pairs.forEach((i, p) -> {
RMObject currentChild = null;
T childObject = null;
childObject = p.getLeft();
currentChild = p.getMiddle();
WebTemplateNode childNode = p.getRight();
if (currentChild != null && childObject != null) {
context.getNodeDeque().push(childNode);
context.getObjectDeque().push(childObject);
context.getRmObjectDeque().push(currentChild);
context.getCountMap().put(new NodeId(childNode), i);
handle(context);
}
});
}
}
}
postHandle(context);
insertDefaults(context);
context.getRmObjectDeque().remove();
context.getNodeDeque().remove();
context.getObjectDeque().remove();
}
Aggregations