From 4f85aa8e80e9367a42001a9c5ea144c763dfa118 Mon Sep 17 00:00:00 2001 From: EdwardvanRaak Date: Fri, 11 Feb 2022 17:54:06 +0100 Subject: [PATCH] address some review comments --- .../archie/rminfo/UpdatedValueHandler.java | 69 ++++++++++++------- 1 file changed, 46 insertions(+), 23 deletions(-) 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 9ce530686..6aaf0ef05 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 @@ -49,37 +49,60 @@ public static Map pathHasBeenUpdated(Object rmObject, Archetype 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); + // TODO Check for magnitude? + // TODO Check if this fix actually needs to occur + return fixForMagnitude(rmObject, (OperationalTemplate) archetype, pathOfParent); + } catch (Exception e) { + logger.warn("cannot fix DvQuantity", e); + } - OperationalTemplate template = (OperationalTemplate) archetype; + return new HashMap<>(); + } - CAttribute units = template.getDefinition().itemAtPath(pathOfParent + "/units"); - CAttribute precision = template.getDefinition().itemAtPath(pathOfParent + "/precision"); + private static Map fixForMagnitude(Object rmObject, OperationalTemplate template, String pathOfParent) { + Map result = new HashMap<>(); - 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 + RMPathQuery rmPathQuery = new RMPathQuery(pathOfParent); + DvQuantity quantity = rmPathQuery.find(ArchieRMInfoLookup.getInstance(), rmObject); - // Fix units - CString cString = (CString) units.getChildren().get(0); - String assumedUnitValue = cString.getAssumedValue(); - quantity.setUnits(assumedUnitValue); - result.put(pathOfParent + "/units", assumedUnitValue); + CAttribute units = template.getDefinition().itemAtPath(pathOfParent + "/units"); + CAttribute precision = template.getDefinition().itemAtPath(pathOfParent + "/precision"); - // Fix precision - CInteger cInteger = (CInteger) units.getChildren().get(0); - long assumedPrecisionValue = cInteger.getAssumedValue(); - quantity.setPrecision(assumedPrecisionValue); - result.put(pathOfParent + "/precision", assumedPrecisionValue); + if (units.getChildren().size() != 1) return result; // Only fix if there is 1 unit - return result; - } catch (Exception e) { - logger.warn("cannot fix DvQuantity", e); + // Fix units + CString cString = (CString) units.getChildren().get(0); + List cStringConstraint = cString.getConstraint(); + if(cStringConstraint != null && cStringConstraint.size() == 1) { + String constraint = cStringConstraint.get(0); + if(!CString.isRegexConstraint(constraint)) { + quantity.setUnits(constraint); + result.put(pathOfParent + "/units", constraint); + } } - return new HashMap<>(); + if (precision.getChildren().size() != 1) + return result; // Only fix if there is 1 precision + + // Fix precision + CInteger cInteger = (CInteger) precision.getChildren().get(0); + + List> cIntegerConstraint = cInteger.getConstraint(); + + if (cIntegerConstraint != null && cIntegerConstraint.size() == 1) { + Interval interval = cIntegerConstraint.get(0); + long value; + if (interval.isUpperUnbounded()) { + value = -1; + } else if (interval.isUpperIncluded() && interval.getUpper() != null) { + value = interval.getUpper(); + } else if (interval.getUpper() != null) { + value = interval.getUpper() -1 ; + } else throw new IllegalStateException("upper bound was not available"); + quantity.setPrecision(value); + result.put(pathOfParent + "/precision", value); + } + return result; } private static Map fixCodePhrase(Object rmObject, Archetype archetype, String pathOfParent) {