diff --git a/openehr-rm/src/main/java/com/nedap/archie/rminfo/UpdatedValueHandler.java b/openehr-rm/src/main/java/com/nedap/archie/rminfo/UpdatedValueHandler.java index 792e275d5..9ce530686 100644 --- a/openehr-rm/src/main/java/com/nedap/archie/rminfo/UpdatedValueHandler.java +++ b/openehr-rm/src/main/java/com/nedap/archie/rminfo/UpdatedValueHandler.java @@ -4,6 +4,8 @@ import com.google.common.base.Strings; import com.nedap.archie.ArchieLanguageConfiguration; import com.nedap.archie.aom.*; +import com.nedap.archie.aom.primitives.CInteger; +import com.nedap.archie.aom.primitives.CString; import com.nedap.archie.aom.primitives.CTerminologyCode; import com.nedap.archie.aom.terminology.ArchetypeTerm; import com.nedap.archie.aom.terminology.ArchetypeTerminology; @@ -18,15 +20,18 @@ import com.nedap.archie.rm.datatypes.CodePhrase; import com.nedap.archie.rm.datavalues.DvCodedText; import com.nedap.archie.rm.datavalues.quantity.DvOrdinal; +import com.nedap.archie.rm.datavalues.quantity.DvQuantity; import com.nedap.archie.rm.support.identification.TerminologyId; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.xml.xpath.XPathExpressionException; import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.xml.xpath.XPathExpressionException; + public class UpdatedValueHandler { private static final Logger logger = LoggerFactory.getLogger(UpdatedValueHandler.class); @@ -35,6 +40,44 @@ public static Map pathHasBeenUpdated(Object rmObject, Archetype if(parent instanceof CodePhrase) { return fixCodePhrase(rmObject, archetype, pathOfParent); } + if (parent instanceof DvQuantity) { + return fixDvQuantity(rmObject, archetype, pathOfParent); + } + + return new HashMap<>(); + } + + private static Map fixDvQuantity(Object rmObject, Archetype archetype, String pathOfParent) { + try { + Map result = new HashMap<>(); + + RMPathQuery rmPathQuery = new RMPathQuery(pathOfParent); + DvQuantity quantity = rmPathQuery.find(ArchieRMInfoLookup.getInstance(), rmObject); + + OperationalTemplate template = (OperationalTemplate) archetype; + + CAttribute units = template.getDefinition().itemAtPath(pathOfParent + "/units"); + CAttribute precision = template.getDefinition().itemAtPath(pathOfParent + "/precision"); + + if (units.getChildren().size() != 1 || precision.getChildren().size() != 1) + return result; // Only fix if there is 1 unit and 1 precision, don't assume anything otherwise + + // Fix units + CString cString = (CString) units.getChildren().get(0); + String assumedUnitValue = cString.getAssumedValue(); + quantity.setUnits(assumedUnitValue); + result.put(pathOfParent + "/units", assumedUnitValue); + + // Fix precision + CInteger cInteger = (CInteger) units.getChildren().get(0); + long assumedPrecisionValue = cInteger.getAssumedValue(); + quantity.setPrecision(assumedPrecisionValue); + result.put(pathOfParent + "/precision", assumedPrecisionValue); + + return result; + } catch (Exception e) { + logger.warn("cannot fix DvQuantity", e); + } return new HashMap<>(); }