Search in sources :

Example 1 with FlatPathDto

use of org.ehrbase.webtemplate.path.flat.FlatPathDto in project openEHR_SDK by ehrbase.

the class FlatJsonUnmarshaller method unmarshal.

/**
 * Unmarshal flat Json to Composition
 *
 * @param flat the flat Json
 * @param introspect the introspect belonging to the template
 * @return
 */
public Composition unmarshal(String flat, WebTemplate introspect) {
    Set<String> consumedPath;
    Map<String, String> currentValues;
    consumedPath = new HashSet<>();
    try {
        currentValues = new HashMap<>();
        for (Iterator<Map.Entry<String, JsonNode>> it = OBJECT_MAPPER.readTree(flat).fields(); it.hasNext(); ) {
            Map.Entry<String, JsonNode> e = it.next();
            currentValues.put(e.getKey(), e.getValue().toString());
        }
        Composition generate = WebTemplateSkeletonBuilder.build(introspect, false);
        StdToCompositionWalker walker = new StdToCompositionWalker();
        DefaultValues defaultValues = new DefaultValues(currentValues);
        // put default for the defaults
        if (!defaultValues.containsDefaultValue(DefaultValuePath.TIME)) {
            defaultValues.addDefaultValue(DefaultValuePath.TIME, OffsetDateTime.now());
        }
        if (!defaultValues.containsDefaultValue(DefaultValuePath.SETTING)) {
            defaultValues.addDefaultValue(DefaultValuePath.SETTING, Setting.OTHER_CARE);
        }
        String templateId = generate.getArchetypeDetails().getTemplateId().getValue();
        walker.walk(generate, currentValues.entrySet().stream().collect(Collectors.toMap(e1 -> new FlatPathDto(e1.getKey()), Map.Entry::getValue)), introspect, defaultValues, templateId);
        consumedPath = walker.getConsumedPaths();
        if (!CollectionUtils.isEmpty(getUnconsumed(consumedPath, currentValues))) {
            throw new UnmarshalException(String.format("Could not consume Parts %s", getUnconsumed(consumedPath, currentValues)));
        }
        return generate;
    } catch (JsonProcessingException e) {
        throw new UnmarshalException(e.getMessage(), e);
    }
}
Also used : Composition(com.nedap.archie.rm.composition.Composition) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto) JsonNode(com.fasterxml.jackson.databind.JsonNode) UnmarshalException(org.ehrbase.serialisation.exception.UnmarshalException) DefaultValues(org.ehrbase.serialisation.walker.defaultvalues.DefaultValues) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with FlatPathDto

use of org.ehrbase.webtemplate.path.flat.FlatPathDto in project openEHR_SDK by ehrbase.

the class StdToCompositionWalker method isMatchingNode.

private boolean isMatchingNode(Map<FlatPathDto, String> subValues, Context<Map<FlatPathDto, String>> context, WebTemplateNode child, FlatPathDto currentFlatPath) {
    if (child.getRmType().equals("POINT_EVENT")) {
        return subValues.entrySet().stream().allMatch((e -> !e.getKey().getLast().getName().equals("width")));
    } else if (child.getRmType().equals("INTERVAL_EVENT")) {
        return subValues.entrySet().stream().anyMatch((e -> e.getKey().getLast().getName().equals("width")));
    } else if (visitChildren(child)) {
        for (WebTemplateNode n : child.getChildren()) {
            context.getNodeDeque().push(n);
            String path = context.getFlatHelper().buildNamePath(context, true);
            context.getNodeDeque().remove();
            subValues = subValues.entrySet().stream().filter(e -> !e.getKey().startsWith(path)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        }
        return subValues.isEmpty();
    } else if (child.getRmType().equals(DV_CODED_TEXT)) {
        return subValues.keySet().stream().anyMatch(e -> "code".equals(e.getLast().getAttributeName()) && currentFlatPath.getLast().getName().equals(e.getLast().getName()));
    } else if (child.getRmType().equals(DV_TEXT)) {
        return subValues.keySet().stream().allMatch((e -> !("code".equals(e.getLast().getAttributeName()) && currentFlatPath.getLast().getName().equals(e.getLast().getName()))));
    } else {
        // End Nodes which are Choice always have unique flat paths
        return true;
    }
}
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) Entry(com.nedap.archie.rm.composition.Entry) WebTemplateNode(org.ehrbase.webtemplate.model.WebTemplateNode)

Example 3 with FlatPathDto

use of org.ehrbase.webtemplate.path.flat.FlatPathDto in project openEHR_SDK by ehrbase.

the class StdToCompositionWalker method extract.

@Override
protected Map<FlatPathDto, String> extract(Context<Map<FlatPathDto, String>> context, WebTemplateNode child, boolean isChoice, Integer count) {
    context.getNodeDeque().push(child);
    Integer oldCount = null;
    if (count != null) {
        oldCount = context.getCountMap().get(new NodeId(child));
        context.getCountMap().put(new NodeId(child), count);
    }
    String path;
    path = buildNamePathWithElementHandling(context);
    context.getNodeDeque().remove();
    context.getCountMap().remove(new NodeId(child));
    if (oldCount != null) {
        context.getCountMap().put(new NodeId(child), oldCount);
    }
    Map<FlatPathDto, String> subValues = context.getObjectDeque().peek().entrySet().stream().filter(e -> e.getKey().startsWith(path)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    if (isChoice && !isMatchingNode(subValues, context, child, new FlatPathDto(path))) {
        subValues = Collections.emptyMap();
    }
    if (!subValues.isEmpty()) {
        return subValues;
    } else {
        return null;
    }
}
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) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto) NodeId(org.ehrbase.serialisation.walker.NodeId) Map(java.util.Map)

Example 4 with FlatPathDto

use of org.ehrbase.webtemplate.path.flat.FlatPathDto in project openEHR_SDK by ehrbase.

the class StdToCompositionWalker method calculateSize.

@Override
protected int calculateSize(Context<Map<FlatPathDto, String>> context, WebTemplateNode childNode) {
    Integer oldCount = context.getCountMap().get(new NodeId(childNode));
    String namePath = context.getFlatHelper().buildNamePath(context, true);
    String finalNamePath = namePath;
    Integer count = context.getObjectDeque().peek().keySet().stream().filter(s -> s.startsWith(finalNamePath)).map(s -> FlatPathDto.removeStart(s, new FlatPathDto(finalNamePath))).filter(n -> n != null && n.getName().equals(childNode.getId())).map(n -> Optional.ofNullable(n.getCount()).orElse(0)).sorted().reduce((first, second) -> second).map(i -> i + 1).orElse(0);
    if (oldCount != null) {
        context.getCountMap().put(new NodeId(childNode), oldCount);
    }
    return count;
}
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) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto) NodeId(org.ehrbase.serialisation.walker.NodeId)

Example 5 with FlatPathDto

use of org.ehrbase.webtemplate.path.flat.FlatPathDto in project openEHR_SDK by ehrbase.

the class StdToCompositionWalker method preHandle.

@Override
protected void preHandle(Context<Map<FlatPathDto, String>> context) {
    // Handle if at an End-Node
    if (!isRaw(context) && !visitChildren(context.getNodeDeque().peek()) && !context.getFlatHelper().skip(context)) {
        if (context.getRmObjectDeque().peek().getClass().isAssignableFrom(DvCodedText.class) && context.getObjectDeque().peek().keySet().stream().anyMatch(k -> "other".equals(k.getLast().getAttributeName()))) {
            replaceRmObject(context, new DvText());
        }
        RMUnmarshaller rmUnmarshaller = findRMUnmarshaller(context.getRmObjectDeque().peek().getClass());
        String namePath = buildNamePathWithElementHandling(context);
        rmUnmarshaller.handle(namePath, context.getRmObjectDeque().peek(), context.getObjectDeque().peek(), context, consumedPaths);
    }
}
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) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) DefaultRMUnmarshaller(org.ehrbase.serialisation.flatencoding.std.umarshal.rmunmarshaller.DefaultRMUnmarshaller) RMUnmarshaller(org.ehrbase.serialisation.flatencoding.std.umarshal.rmunmarshaller.RMUnmarshaller) DvText(com.nedap.archie.rm.datavalues.DvText)

Aggregations

FlatPathDto (org.ehrbase.webtemplate.path.flat.FlatPathDto)30 Map (java.util.Map)14 DvCodedText (com.nedap.archie.rm.datavalues.DvCodedText)12 Context (org.ehrbase.serialisation.walker.Context)12 CodePhrase (com.nedap.archie.rm.datatypes.CodePhrase)11 Set (java.util.Set)11 DefaultValues (org.ehrbase.serialisation.walker.defaultvalues.DefaultValues)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)9 DvText (com.nedap.archie.rm.datavalues.DvText)9 TerminologyId (com.nedap.archie.rm.support.identification.TerminologyId)9 Collectors (java.util.stream.Collectors)9 StringUtils (org.apache.commons.lang3.StringUtils)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 UnmarshalException (org.ehrbase.serialisation.exception.UnmarshalException)7 RMObject (com.nedap.archie.rm.RMObject)6 Composition (com.nedap.archie.rm.composition.Composition)6 Entry (com.nedap.archie.rm.composition.Entry)6 PartyRelated (com.nedap.archie.rm.generic.PartyRelated)6 List (java.util.List)6 Optional (java.util.Optional)6