Search in sources :

Example 1 with Element

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();
}
Also used : IsmTransition(com.nedap.archie.rm.composition.IsmTransition) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) java.util(java.util) CanonicalJson(org.ehrbase.serialisation.jsonencoding.CanonicalJson) Composition(com.nedap.archie.rm.composition.Composition) ArchieRMInfoLookup(com.nedap.archie.rminfo.ArchieRMInfoLookup) WebTemplateInput(org.ehrbase.webtemplate.model.WebTemplateInput) RMTypeInfo(com.nedap.archie.rminfo.RMTypeInfo) DefaultValues(org.ehrbase.serialisation.walker.defaultvalues.DefaultValues) DvInterval(com.nedap.archie.rm.datavalues.quantity.DvInterval) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Element(com.nedap.archie.openehrtestrm.Element) WebTemplate(org.ehrbase.webtemplate.model.WebTemplate) EventContext(com.nedap.archie.rm.composition.EventContext) RMObject(com.nedap.archie.rm.RMObject) RmConstants(org.ehrbase.util.rmconstants.RmConstants) Locatable(com.nedap.archie.rm.archetyped.Locatable) WebTemplateNode(org.ehrbase.webtemplate.model.WebTemplateNode) Triple(org.apache.commons.lang3.tuple.Triple) WebTemplateNode(org.ehrbase.webtemplate.model.WebTemplateNode) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) Triple(org.apache.commons.lang3.tuple.Triple) RMObject(com.nedap.archie.rm.RMObject)

Aggregations

Element (com.nedap.archie.openehrtestrm.Element)1 RMObject (com.nedap.archie.rm.RMObject)1 Locatable (com.nedap.archie.rm.archetyped.Locatable)1 Composition (com.nedap.archie.rm.composition.Composition)1 EventContext (com.nedap.archie.rm.composition.EventContext)1 IsmTransition (com.nedap.archie.rm.composition.IsmTransition)1 DvInterval (com.nedap.archie.rm.datavalues.quantity.DvInterval)1 ArchieRMInfoLookup (com.nedap.archie.rminfo.ArchieRMInfoLookup)1 RMTypeInfo (com.nedap.archie.rminfo.RMTypeInfo)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 ImmutableTriple (org.apache.commons.lang3.tuple.ImmutableTriple)1 Triple (org.apache.commons.lang3.tuple.Triple)1 CanonicalJson (org.ehrbase.serialisation.jsonencoding.CanonicalJson)1 DefaultValues (org.ehrbase.serialisation.walker.defaultvalues.DefaultValues)1 RmConstants (org.ehrbase.util.rmconstants.RmConstants)1 WebTemplate (org.ehrbase.webtemplate.model.WebTemplate)1 WebTemplateInput (org.ehrbase.webtemplate.model.WebTemplateInput)1 WebTemplateNode (org.ehrbase.webtemplate.model.WebTemplateNode)1