001package org.hl7.fhir.dstu3.terminologies; 002 003import java.util.List; 004 005import org.hl7.fhir.dstu3.model.BooleanType; 006import org.hl7.fhir.dstu3.model.CodeSystem; 007import org.hl7.fhir.dstu3.model.CodeSystem.ConceptDefinitionComponent; 008import org.hl7.fhir.dstu3.model.CodeSystem.ConceptPropertyComponent; 009import org.hl7.fhir.dstu3.model.CodeSystem.PropertyComponent; 010import org.hl7.fhir.dstu3.model.CodeSystem.PropertyType; 011import org.hl7.fhir.dstu3.utils.ToolingExtensions; 012import org.hl7.fhir.dstu3.model.DateTimeType; 013import org.hl7.fhir.dstu3.model.Identifier; 014import org.hl7.fhir.dstu3.model.Meta; 015import org.hl7.fhir.dstu3.model.UriType; 016import org.hl7.fhir.exceptions.FHIRException; 017import org.hl7.fhir.exceptions.FHIRFormatError; 018import org.hl7.fhir.utilities.Utilities; 019 020public class CodeSystemUtilities { 021 022 public static boolean isDeprecated(CodeSystem cs, ConceptDefinitionComponent def) { 023 for (ConceptPropertyComponent p : def.getProperty()) { 024 if (p.getCode().equals("deprecated") && p.hasValue() && p.getValue() instanceof BooleanType) 025 return ((BooleanType) p.getValue()).getValue(); 026 if (p.getCode().equals("deprecationDate") && p.hasValue() && p.getValue() instanceof DateTimeType) 027 return ((DateTimeType) p.getValue()).before(new DateTimeType()); 028 } 029 return false; 030 } 031 032 public static boolean isNotSelectable(CodeSystem cs, ConceptDefinitionComponent def) { 033 for (ConceptPropertyComponent p : def.getProperty()) { 034 if (p.getCode().equals("notSelectable") && p.hasValue() && p.getValue() instanceof BooleanType) 035 return ((BooleanType) p.getValue()).getValue(); 036 } 037 return false; 038 } 039 040 public static void setNotSelectable(CodeSystem cs, ConceptDefinitionComponent concept) throws FHIRFormatError { 041 defineNotSelectableProperty(cs); 042 concept.addProperty().setCode("notSelectable").setValue(new BooleanType(true)); 043 } 044 045 public static void setInactive(CodeSystem cs, ConceptDefinitionComponent concept) throws FHIRFormatError { 046 defineInactiveProperty(cs); 047 concept.addProperty().setCode("inactive").setValue(new BooleanType(true)); 048 } 049 050 public static void setDeprecated(CodeSystem cs, ConceptDefinitionComponent concept, DateTimeType date) throws FHIRFormatError { 051 defineDeprecatedProperty(cs); 052 concept.addProperty().setCode("deprecationDate").setValue(date); 053 } 054 055 public static void defineNotSelectableProperty(CodeSystem cs) { 056 defineCodeSystemProperty(cs, "notSelectable", "Indicates that the code is abstract - only intended to be used as a selector for other concepts", PropertyType.BOOLEAN); 057 } 058 059 public static void defineInactiveProperty(CodeSystem cs) { 060 defineCodeSystemProperty(cs, "inactive", "True if the concept is not considered active - e.g. not a valid concept any more", PropertyType.BOOLEAN); 061 } 062 063 public static void defineDeprecatedProperty(CodeSystem cs) { 064 defineCodeSystemProperty(cs, "deprecationDate", "The date at which a concept was deprecated. Concepts that are deprecated but not inactive can still be used, but their use is discouraged", PropertyType.DATETIME); 065 } 066 067 public static void defineCodeSystemProperty(CodeSystem cs, String code, String description, PropertyType type) { 068 for (PropertyComponent p : cs.getProperty()) { 069 if (p.getCode().equals(code)) 070 return; 071 } 072 cs.addProperty().setCode(code).setDescription(description).setType(type).setUri("http://hl7.org/fhir/concept-properties#"+code); 073 } 074 075 public static String getCodeDefinition(CodeSystem cs, String code) { 076 return getCodeDefinition(cs.getConcept(), code); 077 } 078 079 private static String getCodeDefinition(List<ConceptDefinitionComponent> list, String code) { 080 for (ConceptDefinitionComponent c : list) { 081 if (c.getCode().equals(code)) 082 return c.getDefinition(); 083 String s = getCodeDefinition(c.getConcept(), code); 084 if (s != null) 085 return s; 086 } 087 return null; 088 } 089 090 public static CodeSystem makeShareable(CodeSystem cs) { 091 if (!cs.hasMeta()) 092 cs.setMeta(new Meta()); 093 for (UriType t : cs.getMeta().getProfile()) 094 if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablecodesystem")) 095 return cs; 096 cs.getMeta().getProfile().add(new UriType("http://hl7.org/fhir/StructureDefinition/shareablecodesystem")); 097 return cs; 098 } 099 100 public static void setOID(CodeSystem cs, String oid) { 101 if (!oid.startsWith("urn:oid:")) 102 oid = "urn:oid:" + oid; 103 if (!cs.hasIdentifier()) 104 cs.setIdentifier(new Identifier().setSystem("urn:ietf:rfc:3986").setValue(oid)); 105 else if ("urn:ietf:rfc:3986".equals(cs.getIdentifier().getSystem()) && cs.getIdentifier().hasValue() && cs.getIdentifier().getValue().startsWith("urn:oid:")) 106 cs.getIdentifier().setValue(oid); 107 else 108 throw new Error("unable to set OID on code system"); 109 110 } 111 112 public static boolean hasOID(CodeSystem cs) { 113 return getOID(cs) != null; 114 } 115 116 public static String getOID(CodeSystem cs) { 117 if (cs.hasIdentifier() && "urn:ietf:rfc:3986".equals(cs.getIdentifier().getSystem()) && cs.getIdentifier().hasValue() && cs.getIdentifier().getValue().startsWith("urn:oid:")) 118 return cs.getIdentifier().getValue().substring(8); 119 return null; 120 } 121 122 public static boolean isInactive(CodeSystem cs, ConceptDefinitionComponent def) throws FHIRException { 123 for (ConceptPropertyComponent p : def.getProperty()) { 124 if (p.getCode().equals("status") && p.hasValueStringType()) 125 return "inactive".equals(p.getValueStringType()); 126 } 127 return false; 128 } 129 130 public static boolean isInactive(CodeSystem cs, String code) throws FHIRException { 131 ConceptDefinitionComponent def = findCode(cs.getConcept(), code); 132 if (def == null) 133 return true; 134 return isInactive(cs, def); 135 } 136 137 private static ConceptDefinitionComponent findCode(List<ConceptDefinitionComponent> list, String code) { 138 for (ConceptDefinitionComponent c : list) { 139 if (c.getCode().equals(code)) 140 return c; 141 ConceptDefinitionComponent s = findCode(c.getConcept(), code); 142 if (s != null) 143 return s; 144 } 145 return null; 146 } 147 148 public static void markStatus(CodeSystem cs, String wg, String status, String fmm) { 149 if (wg != null) { 150 if (!ToolingExtensions.hasExtension(cs, ToolingExtensions.EXT_WORKGROUP) || 151 (Utilities.existsInList(ToolingExtensions.readStringExtension(cs, ToolingExtensions.EXT_WORKGROUP), "fhir", "vocab") && !Utilities.existsInList(wg, "fhir", "vocab"))) { 152 ToolingExtensions.setCodeExtension(cs, ToolingExtensions.EXT_WORKGROUP, wg); 153 } 154 } 155 if (status != null) { 156 String ss = ToolingExtensions.readStringExtension(cs, ToolingExtensions.EXT_BALLOT_STATUS); 157 if (Utilities.noString(ss) || ssval(ss) < ssval(status)) 158 ToolingExtensions.setStringExtension(cs, ToolingExtensions.EXT_BALLOT_STATUS, status); 159 } 160 if (fmm != null) { 161 String sfmm = ToolingExtensions.readStringExtension(cs, ToolingExtensions.EXT_FMM_LEVEL); 162 if (Utilities.noString(sfmm) || Integer.parseInt(sfmm) < Integer.parseInt(fmm)) 163 ToolingExtensions.setIntegerExtension(cs, ToolingExtensions.EXT_FMM_LEVEL, Integer.parseInt(fmm)); 164 } 165 } 166 167 private static int ssval(String status) { 168 if ("Draft".equals("status")) 169 return 1; 170 if ("Informative".equals("status")) 171 return 2; 172 if ("External".equals("status")) 173 return 3; 174 if ("Trial Use".equals("status")) 175 return 3; 176 if ("Normative".equals("status")) 177 return 4; 178 return -1; 179 } 180 181}