use of org.ehrbase.serialisation.walker.defaultvalues.DefaultValues 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);
}
}
use of org.ehrbase.serialisation.walker.defaultvalues.DefaultValues in project openEHR_SDK by ehrbase.
the class EntryPostprocessor method process.
/**
* {@inheritDoc}
*/
@Override
public void process(String term, Entry rmObject, Map<FlatPathDto, String> values, Set<String> consumedPaths, Context<Map<FlatPathDto, String>> context) {
consumedPaths.add(term + PATH_DIVIDER + "encoding|code");
consumedPaths.add(term + PATH_DIVIDER + "encoding|terminology");
Map<FlatPathDto, String> subjectValues = FlatHelper.filter(values, term + "/subject", false);
if (!subjectValues.isEmpty()) {
if (rmObject.getSubject() == null) {
// If it was PartyRelated it would be set by now do to the relationship and if it was
// PartySelf subjectValues would be empty
rmObject.setSubject(new PartyIdentified());
}
callUnmarshal(term, "subject", rmObject.getSubject(), values, consumedPaths, context, context.getNodeDeque().peek().findChildById("subject").orElse(buildDummyChild("subject", context.getNodeDeque().peek())));
}
PartyProxy subject = rmObject.getSubject();
if (subject == null || (subject instanceof PartyIdentified && ((PartyIdentified) subject).getName() == null && CollectionUtils.isEmpty(((PartyIdentified) subject).getIdentifiers()) && subject.getExternalRef() == null && (!(subject instanceof PartyRelated) || ((PartyRelated) subject).getRelationship() == null || StringUtils.isEmpty(((PartyRelated) subject).getRelationship().getValue())))) {
rmObject.setSubject(new PartySelf());
}
Map<FlatPathDto, String> providerList = values.entrySet().stream().filter(e -> e.getKey().startsWith(term + PATH_DIVIDER + "_provider")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
if (!MapUtils.isEmpty(providerList)) {
if (!(rmObject.getProvider() instanceof PartyIdentified)) {
rmObject.setProvider(new PartyIdentified());
}
PartyIdentifiedRMUnmarshaller partyIdentifiedRMUnmarshaller = new PartyIdentifiedRMUnmarshaller();
partyIdentifiedRMUnmarshaller.handle(term + PATH_DIVIDER + "_provider", (PartyIdentified) rmObject.getProvider(), providerList, null, consumedPaths);
}
Map<Integer, Map<String, String>> other = extractMultiValued(term, "_other_participation", values);
other.values().stream().map(Map::entrySet).map(s -> s.stream().collect(Collectors.toMap(e -> "ctx/" + DefaultValuePath.PARTICIPATION.getPath() + "_" + e.getKey().replace("identifiers_", "identifiers|"), e -> StringUtils.wrap(e.getValue(), '"'))).entrySet()).map(DefaultValues::buildParticipation).forEach(rmObject::addOtherParticipant);
consumeAllMatching(term + PATH_DIVIDER + "_other_participation", values, consumedPaths, false);
Map<FlatPathDto, String> workflowIdValues = filter(values, term + "/_work_flow_id", false);
if (!workflowIdValues.isEmpty()) {
ObjectRef<GenericId> ref = new ObjectRef<>();
ref.setId(new GenericId());
rmObject.setWorkflowId(ref);
setValue(term + "/_work_flow_id", "id", workflowIdValues, s -> ref.getId().setValue(s), String.class, consumedPaths);
setValue(term + "/_work_flow_id", "id_scheme", workflowIdValues, s -> ref.getId().setScheme(s), String.class, consumedPaths);
setValue(term + "/_work_flow_id", "namespace", workflowIdValues, ref::setNamespace, String.class, consumedPaths);
setValue(term + "/_work_flow_id", "type", workflowIdValues, ref::setType, String.class, consumedPaths);
}
}
use of org.ehrbase.serialisation.walker.defaultvalues.DefaultValues in project openEHR_SDK by ehrbase.
the class LocatableUnmarshalPostprocessor method process.
/**
* {@inheritDoc} Unmarshalls {@link Composition#setUid}
*/
@Override
public void process(String term, Locatable rmObject, Map<FlatPathDto, String> values, Set<String> consumedPaths, Context<Map<FlatPathDto, String>> context) {
if (RmConstants.ELEMENT.equals(context.getNodeDeque().peek().getRmType()) || !context.getFlatHelper().skip(context)) {
setValue(term + PATH_DIVIDER + "_uid", null, values, s -> rmObject.setUid(new HierObjectId(s)), String.class, consumedPaths);
Map<Integer, Map<String, String>> links = extractMultiValued(term, "_link", values);
if (rmObject.getLinks() == null) {
rmObject.setLinks(new ArrayList<>());
}
rmObject.getLinks().addAll(links.values().stream().map(DefaultValues::createLink).collect(Collectors.toList()));
consumeAllMatching(term + PATH_DIVIDER + "_link", values, consumedPaths, false);
Map<FlatPathDto, String> feederAuditValues = FlatHelper.filter(values, term + "/_feeder_audit", false);
if (!feederAuditValues.isEmpty()) {
rmObject.setFeederAudit(new FeederAudit());
handleRmAttribute(term, rmObject.getFeederAudit(), feederAuditValues, consumedPaths, context, "feeder_audit");
}
Map<FlatPathDto, String> nameValues = FlatHelper.filter(values, term + "/_name", false);
if (!nameValues.isEmpty()) {
final DvText name;
boolean isDvCodedText = nameValues.keySet().stream().anyMatch(e -> "code".equals(e.getLast().getAttributeName()) && "_name".equals(e.getLast().getName()));
if (isDvCodedText) {
name = new DvCodedText();
} else {
name = new DvText();
}
rmObject.setName(name);
handleRmAttribute(term, rmObject.getName(), nameValues, consumedPaths, context, "name");
}
}
}
use of org.ehrbase.serialisation.walker.defaultvalues.DefaultValues in project openEHR_SDK by ehrbase.
the class PartyIdentifiedRMUnmarshaller method handle.
@Override
public void handle(String currentTerm, PartyIdentified rmObject, Map<FlatPathDto, String> currentValues, Context<Map<FlatPathDto, String>> context, Set<String> consumedPaths) {
setValue(currentTerm, "name", currentValues, rmObject::setName, String.class, consumedPaths);
if (rmObject.getName() == null) {
// betters implementation uses /name or /_name instead of |name for subject
setValue(currentTerm + "/name", null, currentValues, rmObject::setName, String.class, consumedPaths);
if (rmObject.getName() == null) {
setValue(currentTerm + "/_name", null, currentValues, rmObject::setName, String.class, consumedPaths);
}
}
rmObject.setExternalRef(new PartyRef());
rmObject.getExternalRef().setType("PARTY");
rmObject.getExternalRef().setId(new GenericId());
setValue(currentTerm, "id", currentValues, rmObject.getExternalRef().getId()::setValue, String.class, consumedPaths);
setValue(currentTerm, "id_scheme", currentValues, ((GenericId) rmObject.getExternalRef().getId())::setScheme, String.class, consumedPaths);
setValue(currentTerm, "id_namespace", currentValues, rmObject.getExternalRef()::setNamespace, String.class, consumedPaths);
// remove if not set
if (rmObject.getExternalRef().getId() == null || StringUtils.isBlank(rmObject.getExternalRef().getId().getValue())) {
rmObject.setExternalRef(null);
}
Map<Integer, Map<String, String>> identifiers = extractMultiValued(currentTerm, "_identifier", currentValues);
rmObject.setIdentifiers(identifiers.values().stream().map(DefaultValues::toDvIdentifier).collect(Collectors.toList()));
consumeAllMatching(currentTerm + PATH_DIVIDER + "_identifier", currentValues, consumedPaths, false);
}
use of org.ehrbase.serialisation.walker.defaultvalues.DefaultValues in project openEHR_SDK by ehrbase.
the class DefaultRestCompositionEndpointIT method testSaveCompositionWithDefaultEntity.
@Test
public void testSaveCompositionWithDefaultEntity() throws URISyntaxException {
openEhrClient = setupDefaultRestClientWithDefaultProvider(o -> {
DefaultValues defaultValues = new DefaultValues();
defaultValues.addDefaultValue(DefaultValuePath.END_TIME, OffsetDateTime.of(2019, 05, 03, 22, 00, 00, 00, ZoneOffset.UTC));
Participation participation = new Participation(new PartyIdentified(null, "Dr. Peter", null), new DvText("Performer"), ParticipationMode.PHYSICALLY_PRESENT.toCodedText(), null);
defaultValues.addDefaultValue(DefaultValuePath.PARTICIPATION, new ArrayList<>(Collections.singleton(participation)));
return defaultValues;
});
ehr = openEhrClient.ehrEndpoint().createEhr();
EhrbaseBloodPressureSimpleDeV0Composition bloodPressureSimpleDeV0 = TestData.buildEhrbaseBloodPressureSimpleDeV0WithEmptyFields();
// Not set during creation
assertThat(bloodPressureSimpleDeV0.getEndTimeValue()).isNull();
assertThat(bloodPressureSimpleDeV0.getParticipations()).isEmpty();
// during saving default values will be set
bloodPressureSimpleDeV0 = openEhrClient.compositionEndpoint(ehr).mergeCompositionEntity(bloodPressureSimpleDeV0);
assertThat(bloodPressureSimpleDeV0.getEndTimeValue()).isEqualTo(OffsetDateTime.of(2019, 05, 03, 22, 00, 00, 00, ZoneOffset.UTC));
assertThat(bloodPressureSimpleDeV0.getParticipations()).extracting(p -> ((PartyIdentified) p.getPerformer()).getName(), p -> p.getMode().getValue()).containsExactlyInAnyOrder(new Tuple("Dr. Peter", "physically present"));
}
Aggregations