diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/CodeSystemUtilities.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/CodeSystemUtilities.java index 5870f52e6..523538ad4 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/CodeSystemUtilities.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/CodeSystemUtilities.java @@ -53,10 +53,13 @@ import org.hl7.fhir.r5.model.CodeType; import org.hl7.fhir.r5.model.Coding; import org.hl7.fhir.r5.model.DataType; import org.hl7.fhir.r5.model.DateTimeType; +import org.hl7.fhir.r5.model.DecimalType; import org.hl7.fhir.r5.model.Enumerations.PublicationStatus; import org.hl7.fhir.r5.terminologies.CodeSystemUtilities.ConceptDefinitionComponentSorter; import org.hl7.fhir.r5.model.Identifier; +import org.hl7.fhir.r5.model.IntegerType; import org.hl7.fhir.r5.model.Meta; +import org.hl7.fhir.r5.model.StringType; import org.hl7.fhir.r5.model.UriType; import org.hl7.fhir.r5.utils.ToolingExtensions; import org.hl7.fhir.utilities.StandardsStatus; @@ -68,7 +71,7 @@ public class CodeSystemUtilities { @Override public int compare(ConceptDefinitionComponent o1, ConceptDefinitionComponent o2) { - return o1.getCode().compareTo(o2.getCode()); + return o1.getCode().compareToIgnoreCase(o2.getCode()); } } @@ -177,6 +180,58 @@ public class CodeSystemUtilities { concept.addProperty().setCode("notSelectable").setValue(new BooleanType(true)); } + public static void setProperty(CodeSystem cs, ConceptDefinitionComponent concept, String code, DataType value) throws FHIRFormatError { + defineProperty(cs, code, propertyTypeForValue(value)); + ConceptPropertyComponent p = getProperty(concept, code); + if (p != null) + p.setValue(value); + else + concept.addProperty().setCode(code).setValue(value); + } + + + private static PropertyType propertyTypeForValue(DataType value) { + if (value instanceof BooleanType) { + return PropertyType.BOOLEAN; + } + if (value instanceof CodeType) { + return PropertyType.CODE; + } + if (value instanceof Coding) { + return PropertyType.CODING; + } + if (value instanceof DateTimeType) { + return PropertyType.DATETIME; + } + if (value instanceof DecimalType) { + return PropertyType.DECIMAL; + } + if (value instanceof IntegerType) { + return PropertyType.INTEGER; + } + if (value instanceof StringType) { + return PropertyType.STRING; + } + throw new Error("Unknown property type "+value.getClass().getName()); + } + + private static void defineProperty(CodeSystem cs, String code, PropertyType pt) { + String url = "http://hl7.org/fhir/concept-properties#"+code; + for (PropertyComponent p : cs.getProperty()) { + if (p.getCode().equals(code)) { + if (!p.getUri().equals(url)) { + throw new Error("URI mismatch for code "+code+" url = "+p.getUri()+" vs "+url); + } + if (!p.getType().equals(pt)) { + throw new Error("Type mismatch for code "+code+" type = "+p.getType()+" vs "+pt); + } + return; + } + } + cs.addProperty().setCode(code).setUri(url).setType(pt).setUri(url); + + } + public static void defineNotSelectableProperty(CodeSystem cs) { defineCodeSystemProperty(cs, "notSelectable", "Indicates that the code is abstract - only intended to be used as a selector for other concepts", PropertyType.BOOLEAN); } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/ValueSetUtilities.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/ValueSetUtilities.java index ac06329ee..1fc5ca3dd 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/ValueSetUtilities.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/ValueSetUtilities.java @@ -1,5 +1,7 @@ package org.hl7.fhir.r5.terminologies; +import java.util.Collections; +import java.util.Comparator; import java.util.List; /* @@ -47,9 +49,11 @@ import org.hl7.fhir.r5.model.UriType; import org.hl7.fhir.r5.model.ValueSet; import org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent; import org.hl7.fhir.r5.model.CodeType; +import org.hl7.fhir.r5.model.ValueSet.ConceptReferenceComponent; import org.hl7.fhir.r5.model.ValueSet.ConceptSetComponent; import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionContainsComponent; import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionPropertyComponent; +import org.hl7.fhir.r5.terminologies.CodeSystemUtilities.ConceptDefinitionComponentSorter; import org.hl7.fhir.r5.terminologies.CodeSystemUtilities.ConceptStatus; import org.hl7.fhir.r5.utils.ToolingExtensions; import org.hl7.fhir.utilities.StandardsStatus; @@ -255,4 +259,18 @@ public class ValueSetUtilities { } + public static class ConceptReferenceComponentSorter implements Comparator { + + @Override + public int compare(ConceptReferenceComponent o1, ConceptReferenceComponent o2) { + return o1.getCode().compareToIgnoreCase(o2.getCode()); + } + } + + + public static void sortInclude(ConceptSetComponent inc) { + Collections.sort(inc.getConcept(), new ConceptReferenceComponentSorter()); + } + + } \ No newline at end of file