Search in sources :

Example 36 with RMObject

use of com.nedap.archie.rm.RMObject in project openEHR_SDK by ehrbase.

the class StdToCompositionWalker method handleRaw.

private void handleRaw(Context<Map<FlatPathDto, String>> context) {
    ObjectMapper om = JacksonUtil.getObjectMapper();
    try {
        Map.Entry<FlatPathDto, String> current = context.getObjectDeque().peek().entrySet().stream().findAny().orElseThrow();
        RMObject newRmObject = new CanonicalJson().unmarshal(om.readValue(current.getValue(), String.class).replace("\"@class\"", "\"_type\""), RMObject.class);
        // Replace old skeleton
        replaceRmObject(context, newRmObject);
        consumedPaths.add(current.getKey().format());
    } catch (JsonProcessingException e) {
        throw new UnmarshalException(e.getMessage());
    }
}
Also used : CanonicalJson(org.ehrbase.serialisation.jsonencoding.CanonicalJson) UnmarshalException(org.ehrbase.serialisation.exception.UnmarshalException) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RMObject(com.nedap.archie.rm.RMObject)

Example 37 with RMObject

use of com.nedap.archie.rm.RMObject in project openEHR_SDK by ehrbase.

the class StdToCompositionWalker method postHandle.

@Override
protected void postHandle(Context<Map<FlatPathDto, String>> context) {
    super.postHandle(context);
    if (isRaw(context)) {
        handleRaw(context);
    }
    WebTemplateNode currentNode = context.getNodeDeque().peek();
    currentNode.getChildren().forEach(childNode -> {
        // Check for Raw in optional (skipped Nodes)
        if (context.getFlatHelper().skip(childNode, currentNode)) {
            context.getNodeDeque().push(childNode);
            context.getRmObjectDeque().push(new RMObject() {
            });
            String path = context.getFlatHelper().buildNamePath(context, true);
            Map<FlatPathDto, String> subValues = context.getObjectDeque().peek().entrySet().stream().filter(e -> e.getKey().startsWith(path + "/_" + childNode.getId())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
            context.getObjectDeque().push(subValues);
            if (isRaw(context)) {
                handleRaw(context);
            }
            context.getNodeDeque().poll();
            context.getRmObjectDeque().poll();
            context.getObjectDeque().poll();
        }
    });
    List<? extends UnmarshalPostprocessor<? extends RMObject>> postprocessor = findUnmarshalPostprocessors(context.getRmObjectDeque().peek().getClass());
    String namePath = buildNamePathWithElementHandling(context);
    if (Entry.class.isAssignableFrom(context.getRmObjectDeque().peek().getClass())) {
        if (((Entry) context.getRmObjectDeque().peek()).getSubject() instanceof PartyRelated) {
            Optional.ofNullable(context.getNodeDeque().peek()).flatMap(c -> c.findChildById("subject")).flatMap(c -> c.findChildById("relationship")).stream().map(WebTemplateNode::getInputs).flatMap(List::stream).filter(i -> "code".equals(i.getSuffix())).map(WebTemplateInput::getList).map(l -> l.size() == 1 ? l.get(0) : null).filter(Objects::nonNull).findAny().ifPresent(v -> ((PartyRelated) ((Entry) context.getRmObjectDeque().peek()).getSubject()).setRelationship(new DvCodedText(v.getLabel(), new CodePhrase(new TerminologyId("openehr"), v.getValue()))));
        }
    }
    postprocessor.forEach(p -> ((UnmarshalPostprocessor) p).process(namePath, context.getRmObjectDeque().peek(), context.getObjectDeque().peek(), consumedPaths, context));
}
Also used : Composition(com.nedap.archie.rm.composition.Composition) TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) ToCompositionWalker(org.ehrbase.serialisation.walker.ToCompositionWalker) DefaultRMUnmarshaller(org.ehrbase.serialisation.flatencoding.std.umarshal.rmunmarshaller.DefaultRMUnmarshaller) Element(com.nedap.archie.rm.datastructures.Element) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) WebTemplateSkeletonBuilder(org.ehrbase.building.webtemplateskeletnbuilder.WebTemplateSkeletonBuilder) HashSet(java.util.HashSet) Map(java.util.Map) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) Entry(com.nedap.archie.rm.composition.Entry) UnmarshalPostprocessor(org.ehrbase.serialisation.flatencoding.std.umarshal.postprocessor.UnmarshalPostprocessor) DV_TEXT(org.ehrbase.util.rmconstants.RmConstants.DV_TEXT) Context(org.ehrbase.serialisation.walker.Context) ReflectionHelper(org.ehrbase.util.reflection.ReflectionHelper) ELEMENT(org.ehrbase.util.rmconstants.RmConstants.ELEMENT) CanonicalJson(org.ehrbase.serialisation.jsonencoding.CanonicalJson) DvText(com.nedap.archie.rm.datavalues.DvText) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) WebTemplateInput(org.ehrbase.webtemplate.model.WebTemplateInput) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DefaultValues(org.ehrbase.serialisation.walker.defaultvalues.DefaultValues) DV_CODED_TEXT(org.ehrbase.util.rmconstants.RmConstants.DV_CODED_TEXT) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) UnmarshalException(org.ehrbase.serialisation.exception.UnmarshalException) JacksonUtil(org.ehrbase.serialisation.jsonencoding.JacksonUtil) Objects(java.util.Objects) WebTemplate(org.ehrbase.webtemplate.model.WebTemplate) List(java.util.List) WebTemplateUtils(org.ehrbase.webtemplate.util.WebTemplateUtils) RMObject(com.nedap.archie.rm.RMObject) Optional(java.util.Optional) RMUnmarshaller(org.ehrbase.serialisation.flatencoding.std.umarshal.rmunmarshaller.RMUnmarshaller) NodeId(org.ehrbase.serialisation.walker.NodeId) WebTemplateNode(org.ehrbase.webtemplate.model.WebTemplateNode) PartyRelated(com.nedap.archie.rm.generic.PartyRelated) Collections(java.util.Collections) Filter(org.ehrbase.webtemplate.filter.Filter) TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) WebTemplateNode(org.ehrbase.webtemplate.model.WebTemplateNode) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto) WebTemplateInput(org.ehrbase.webtemplate.model.WebTemplateInput) PartyRelated(com.nedap.archie.rm.generic.PartyRelated) Entry(com.nedap.archie.rm.composition.Entry) Objects(java.util.Objects) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) RMObject(com.nedap.archie.rm.RMObject)

Example 38 with RMObject

use of com.nedap.archie.rm.RMObject in project openEHR_SDK by ehrbase.

the class StdToCompositionWalker method replaceRmObject.

private void replaceRmObject(Context<Map<FlatPathDto, String>> context, RMObject newRmObject) {
    RMObject oldRM = context.getRmObjectDeque().poll();
    RMObject parentRM = context.getRmObjectDeque().peek();
    WebTemplateNode currentNode = context.getNodeDeque().poll();
    WebTemplateNode parentNode = context.getNodeDeque().peek();
    // since flat skips Elements we might need to keep it
    if (oldRM instanceof Element && !(newRmObject instanceof Element)) {
        WebTemplateNode valueNode = currentNode.getChildren().stream().filter(n -> n.getId().contains("value")).findAny().orElseThrow();
        WebTemplateSkeletonBuilder.insert(currentNode, oldRM, valueNode, newRmObject);
        context.getRmObjectDeque().push(oldRM);
    } else {
        WebTemplateSkeletonBuilder.remove(parentNode, parentRM, currentNode, oldRM);
        WebTemplateSkeletonBuilder.insert(parentNode, parentRM, currentNode, newRmObject);
        context.getRmObjectDeque().push(newRmObject);
    }
    context.getNodeDeque().push(currentNode);
}
Also used : WebTemplateNode(org.ehrbase.webtemplate.model.WebTemplateNode) Element(com.nedap.archie.rm.datastructures.Element) RMObject(com.nedap.archie.rm.RMObject)

Example 39 with RMObject

use of com.nedap.archie.rm.RMObject in project openEHR_SDK by ehrbase.

the class CompositionSerializer method dbEncode.

// @Override
public String dbEncode(RMObject rmObject) {
    Map<String, Object> objectMap;
    if (rmObject instanceof Composition) {
        objectMap = traverse((Composition) rmObject);
    } else if (rmObject instanceof Item) {
        objectMap = traverse((Item) rmObject, TAG_ITEMS);
    } else if (rmObject instanceof ItemStructure) {
        objectMap = traverse((ItemStructure) rmObject, TAG_ITEMS);
        if (objectMap != null && !objectMap.containsKey(TAG_ARCHETYPE_NODE_ID) && ((ItemStructure) rmObject).getArchetypeNodeId() != null) {
            objectMap.put(CompositionSerializer.TAG_ARCHETYPE_NODE_ID, ((ItemStructure) rmObject).getArchetypeNodeId());
        }
        if (objectMap != null && !objectMap.containsKey(CompositionSerializer.TAG_NAME) && ((ItemStructure) rmObject).getName() != null) {
            objectMap.put(CompositionSerializer.TAG_NAME, ((ItemStructure) rmObject).getName());
        }
    } else {
        throw new MarshalException(String.format("Class %s not supported ", rmObject.getClass()), null);
    }
    GsonBuilder builder = EncodeUtilArchie.getGsonBuilderInstance();
    Gson gson = builder.setPrettyPrinting().create();
    return gson.toJson(objectMap);
}
Also used : MarshalException(org.ehrbase.serialisation.exception.MarshalException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) RMObject(com.nedap.archie.rm.RMObject)

Example 40 with RMObject

use of com.nedap.archie.rm.RMObject 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

RMObject (com.nedap.archie.rm.RMObject)57 Test (org.junit.Test)19 Composition (com.nedap.archie.rm.composition.Composition)10 CanonicalJson (org.ehrbase.serialisation.jsonencoding.CanonicalJson)9 QueryResponseData (org.ehrbase.response.openehr.QueryResponseData)8 Locatable (com.nedap.archie.rm.archetyped.Locatable)7 List (java.util.List)7 WebTemplateNode (org.ehrbase.webtemplate.model.WebTemplateNode)6 Collectors (java.util.stream.Collectors)5 TemplateId (com.nedap.archie.rm.archetyped.TemplateId)4 IOException (java.io.IOException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Map (java.util.Map)4 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)4 TestDataTemplateProvider (org.ehrbase.client.templateprovider.TestDataTemplateProvider)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Archetyped (com.nedap.archie.rm.archetyped.Archetyped)3 Version (com.nedap.archie.rm.changecontrol.Version)3 CodePhrase (com.nedap.archie.rm.datatypes.CodePhrase)3