Skip to content

Commit

Permalink
flat fix offset handling (#333)
Browse files Browse the repository at this point in the history
* flat fix offset handling

* edit changelog

see CDR-38

* fix review
  • Loading branch information
stefanspiska authored Mar 7, 2022
1 parent a14ce1a commit 00aedbe
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Note: version releases in the 0.x.y range may introduce breaking changes.

- Flat : corrected handling of PARTY_PROXY ( see https://github.com/ehrbase/openEHR_SDK/pull/320)
- Flat : corrected handling of history origin and ISM_TRANSITION (see https://github.com/ehrbase/openEHR_SDK/pull/329)
- Flat : handle fixed offset in template (see https://github.com/ehrbase/openEHR_SDK/pull/333)
- Validation: fix validation of DV_TEXT with listopen (see https://github.com/ehrbase/openEHR_SDK/pull/335)
- Walker, Flat, DTO, Validation : enhance performance of path handling (
see https://github.com/ehrbase/openEHR_SDK/pull/325 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ protected void insertDefaults(Context<T> context) {
}

postprocessor.forEach(
p -> p.insert(context.getRmObjectDeque().peek(), context.getDefaultValues()));
p ->
p.insert(
context.getRmObjectDeque().peek(),
context.getDefaultValues(),
context.getNodeDeque().peek()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@
import com.nedap.archie.rm.composition.Action;
import com.nedap.archie.rm.composition.IsmTransition;
import com.nedap.archie.rm.datavalues.quantity.datetime.DvDateTime;
import org.ehrbase.serialisation.walker.FlatHelper;
import org.ehrbase.serialisation.walker.RMHelper;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValuePath;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValues;
import org.ehrbase.webtemplate.model.WebTemplateNode;

import java.time.temporal.TemporalAccessor;
import java.util.Objects;
import java.util.stream.Stream;

public class ActionValueInserter extends AbstractValueInserter<Action> {
@Override
public void insert(Action rmObject, DefaultValues defaultValues) {
public void insert(Action rmObject, DefaultValues defaultValues, WebTemplateNode node) {

if (RMHelper.isEmpty(rmObject.getTime())
&& (defaultValues.containsDefaultValue(DefaultValuePath.TIME)
Expand All @@ -49,7 +51,11 @@ public void insert(Action rmObject, DefaultValues defaultValues) {
if (rmObject.getIsmTransition() == null) {
rmObject.setIsmTransition(new IsmTransition());
}
new IsmTransitionValueInserter().insert(rmObject.getIsmTransition(), defaultValues);
new IsmTransitionValueInserter()
.insert(
rmObject.getIsmTransition(),
defaultValues,
FlatHelper.buildDummyChild("ism_transition", node));
if (RMHelper.isEmpty(rmObject.getIsmTransition())) {
rmObject.setIsmTransition(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
import org.ehrbase.serialisation.walker.RMHelper;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValuePath;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValues;
import org.ehrbase.webtemplate.model.WebTemplateNode;

public class ActivityValueInserter extends AbstractValueInserter<Activity> {

@Override
public void insert(Activity rmObject, DefaultValues defaultValues) {
public void insert(Activity rmObject, DefaultValues defaultValues, WebTemplateNode node) {

if (RMHelper.isEmpty(rmObject.getTiming())
&& defaultValues.containsDefaultValue(DefaultValuePath.ACTIVITY_TIMING)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import com.nedap.archie.rm.composition.EventContext;
import com.nedap.archie.rm.generic.PartyIdentified;
import com.nedap.archie.rm.generic.PartySelf;
import org.ehrbase.serialisation.walker.FlatHelper;
import org.ehrbase.serialisation.walker.RMHelper;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValuePath;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValues;
import org.ehrbase.webtemplate.model.WebTemplateNode;

public class CompositionValueInserter extends AbstractValueInserter<Composition> {
@Override
public void insert(Composition rmObject, DefaultValues defaultValues) {
public void insert(Composition rmObject, DefaultValues defaultValues, WebTemplateNode node) {

if (RMHelper.isEmpty(rmObject.getLanguage())
&& defaultValues.containsDefaultValue(DefaultValuePath.LANGUAGE)) {
Expand Down Expand Up @@ -76,7 +78,8 @@ public void insert(Composition rmObject, DefaultValues defaultValues) {
rmObject.setContext(new EventContext());
}

new EventContextValueInserter().insert(rmObject.getContext(), defaultValues);
new EventContextValueInserter()
.insert(rmObject.getContext(), defaultValues, FlatHelper.buildDummyChild("context", node));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import com.nedap.archie.rm.RMObject;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValues;
import org.ehrbase.util.reflection.ClassDependent;
import org.ehrbase.webtemplate.model.WebTemplateNode;

public interface DefaultValueInserter<T extends RMObject> extends ClassDependent<T> {

void insert(T rmObject, DefaultValues defaultValues);
void insert(T rmObject, DefaultValues defaultValues, WebTemplateNode node);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
import org.ehrbase.serialisation.walker.RMHelper;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValuePath;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValues;
import org.ehrbase.webtemplate.model.WebTemplateNode;

import java.util.Objects;

public class EntryDefaultValueInserter extends AbstractValueInserter<Entry> {
@Override
public void insert(Entry rmObject, DefaultValues defaultValues) {
public void insert(Entry rmObject, DefaultValues defaultValues, WebTemplateNode node) {

if (RMHelper.isEmpty(rmObject.getLanguage())
&& defaultValues.getDefaultValue(DefaultValuePath.LANGUAGE) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
import org.ehrbase.serialisation.walker.RMHelper;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValuePath;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValues;
import org.ehrbase.webtemplate.model.WebTemplateNode;

import java.util.Objects;

public class EventContextValueInserter extends AbstractValueInserter<EventContext> {
@Override
public void insert(EventContext rmObject, DefaultValues defaultValues) {
public void insert(EventContext rmObject, DefaultValues defaultValues, WebTemplateNode node) {

if (RMHelper.isEmpty(rmObject.getStartTime())
&& defaultValues.containsDefaultValue(DefaultValuePath.TIME)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
import org.ehrbase.serialisation.walker.RMHelper;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValuePath;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValues;
import org.ehrbase.webtemplate.model.WebTemplateNode;

public class InstructionValueInserter extends AbstractValueInserter<Instruction> {
@Override
public void insert(Instruction rmObject, DefaultValues defaultValues) {
public void insert(Instruction rmObject, DefaultValues defaultValues, WebTemplateNode node) {

if (RMHelper.isEmpty(rmObject.getNarrative())
&& defaultValues.containsDefaultValue(DefaultValuePath.INSTRUCTION_NARRATIVE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
import org.ehrbase.client.classgenerator.shareddefinition.MathFunction;
import org.ehrbase.serialisation.walker.RMHelper;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValues;
import org.ehrbase.webtemplate.model.WebTemplateNode;

public class IntervalEventValueInserter extends AbstractValueInserter<IntervalEvent> {
@Override
public void insert(IntervalEvent rmObject, DefaultValues defaultValues) {
public void insert(IntervalEvent rmObject, DefaultValues defaultValues, WebTemplateNode node) {

if (RMHelper.isEmpty(rmObject.getMathFunction())) {
rmObject.setMathFunction(MathFunction.ACTUAL.toCodedText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
import org.ehrbase.serialisation.walker.RMHelper;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValuePath;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValues;
import org.ehrbase.webtemplate.model.WebTemplateNode;

public class IsmTransitionValueInserter extends AbstractValueInserter<IsmTransition> {

@Override
public void insert(IsmTransition rmObject, DefaultValues defaultValues) {
public void insert(IsmTransition rmObject, DefaultValues defaultValues, WebTemplateNode node) {

if (RMHelper.isEmpty(rmObject.getCurrentState())
&& defaultValues.containsDefaultValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import com.nedap.archie.rm.archetyped.Locatable;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValuePath;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValues;
import org.ehrbase.webtemplate.model.WebTemplateNode;

import java.util.ArrayList;

public class LocatableDefaultValueInserter extends AbstractValueInserter<Locatable> {
@Override
public void insert(Locatable rmObject, DefaultValues defaultValues) {
public void insert(Locatable rmObject, DefaultValues defaultValues, WebTemplateNode node) {

if (defaultValues.containsDefaultValue(DefaultValuePath.LINKS)) {
if (rmObject.getLinks() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,57 @@
import com.nedap.archie.rm.datastructures.Event;
import com.nedap.archie.rm.datastructures.History;
import com.nedap.archie.rm.datavalues.quantity.datetime.DvDateTime;
import org.ehrbase.serialisation.walker.FlatHelper;
import org.ehrbase.serialisation.walker.RMHelper;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValuePath;
import org.ehrbase.serialisation.walker.defaultvalues.DefaultValues;
import org.ehrbase.webtemplate.model.WebTemplateInput;
import org.ehrbase.webtemplate.model.WebTemplateInterval;
import org.ehrbase.webtemplate.model.WebTemplateNode;
import org.ehrbase.webtemplate.model.WebTemplateValidation;
import org.threeten.extra.PeriodDuration;

import java.time.Duration;
import java.time.Period;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAmount;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ObservationValueInserter extends AbstractValueInserter<Observation> {
@Override
public void insert(Observation rmObject, DefaultValues defaultValues) {

if (rmObject.getData() != null) {
insert(rmObject.getData(), defaultValues);

if (rmObject.getData().getEvents() != null) {
public void insert(Observation rmObject, DefaultValues defaultValues, WebTemplateNode node) {

rmObject.getData().getEvents().forEach(e -> insert(e, defaultValues));
}
if (rmObject.getData() != null) {
insert(
rmObject.getData(),
defaultValues,
node
.findMatching(
n -> Objects.equals(n.getNodeId(), rmObject.getData().getArchetypeNodeId()))
.stream()
.findFirst()
.orElse(FlatHelper.buildDummyChild("history", node)));
}

if (rmObject.getState() != null) {
insert(rmObject.getState(), defaultValues);
if (rmObject.getState().getEvents() != null) {
rmObject.getState().getEvents().forEach(e -> insert(e, defaultValues));
}
insert(
rmObject.getState(),
defaultValues,
node
.findMatching(
n -> Objects.equals(n.getNodeId(), rmObject.getState().getArchetypeNodeId()))
.stream()
.findFirst()
.orElse(FlatHelper.buildDummyChild("history", node)));
}
}

private void insert(History<?> rmObject, DefaultValues defaultValues) {
private void insert(History<?> rmObject, DefaultValues defaultValues, WebTemplateNode node) {

if (RMHelper.isEmpty(rmObject.getOrigin())
&& (defaultValues.containsDefaultValue(DefaultValuePath.TIME)
Expand All @@ -65,16 +86,88 @@ private void insert(History<?> rmObject, DefaultValues defaultValues) {
.orElseThrow();
rmObject.setOrigin(new DvDateTime(defaultTemporalAccessor));
}

if (rmObject.getEvents() != null && rmObject.getOrigin() != null) {

rmObject
.getEvents()
.forEach(
e ->
insert(
e,
rmObject.getOrigin().getValue(),
node
.findMatching(n -> Objects.equals(n.getNodeId(), e.getArchetypeNodeId()))
.stream()
.findFirst()
.orElse(FlatHelper.buildDummyChild("event", node))));
}
}

private void insert(Event<?> rmObject, DefaultValues defaultValues) {
if (RMHelper.isEmpty(rmObject.getTime())
&& defaultValues.containsDefaultValue(DefaultValuePath.TIME)) {
private void insert(Event<?> rmObject, TemporalAccessor origin, WebTemplateNode node) {
if (RMHelper.isEmpty(rmObject.getTime())) {

rmObject.setTime(new DvDateTime(defaultValues.getDefaultValue(DefaultValuePath.TIME)));
TemporalAccessor defaultValue = origin;
if (defaultValue instanceof Temporal) {
defaultValue =
((Temporal) defaultValue).plus(buildOffset(node.findChildById("offset").orElse(null)));
}
rmObject.setTime(new DvDateTime(defaultValue));
}
}

private TemporalAmount buildOffset(WebTemplateNode offsetNode) {

PeriodDuration offset = PeriodDuration.ZERO;

List<TemporalAmount> temporalAmounts =
Optional.ofNullable(offsetNode).map(WebTemplateNode::getInputs).stream()
.flatMap(List::stream)
.filter(
i ->
Optional.of(i)
.map(WebTemplateInput::getValidation)
.map(WebTemplateValidation::getRange)
.map(WebTemplateInterval::getMin)
.map(Object::getClass)
.filter(Integer.class::isAssignableFrom)
.isPresent())
.map(this::build)
.collect(Collectors.toList());

for (TemporalAmount amount : temporalAmounts) {

offset = offset.plus(amount);
}
return offset;
}

private TemporalAmount build(WebTemplateInput input) {

String suffix = input.getSuffix();
WebTemplateInterval<Integer> validation = input.getValidation().getRange();

switch (suffix) {
case "year":
return Period.ofYears(validation.getMin());
case "month":
return Period.ofMonths(validation.getMin());
case "day":
return Period.ofDays(validation.getMin());
case "week":
return Period.ofWeeks(validation.getMin());
case "hour":
return Duration.ofHours(validation.getMin());
case "minute":
return Duration.ofMinutes(validation.getMin());
case "second":
return Duration.ofSeconds(validation.getMin());
default:
throw new IllegalArgumentException("Unsupported suffix: " + suffix);
}

}

@Override
public Class<Observation> getAssociatedClass() {
return Observation.class;
Expand Down
Loading

0 comments on commit 00aedbe

Please sign in to comment.