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());
}
}
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));
}
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);
}
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);
}
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();
}
Aggregations