001package org.hl7.fhir.r5.terminologies; 002 003import java.util.ArrayList; 004import java.util.List; 005 006import org.hl7.fhir.r5.model.CanonicalType; 007import org.hl7.fhir.r5.model.CodeSystem; 008import org.hl7.fhir.r5.model.Coding; 009import org.hl7.fhir.r5.model.ConceptMap; 010import org.hl7.fhir.r5.model.ConceptMap.ConceptMapGroupComponent; 011import org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent; 012import org.hl7.fhir.r5.model.ConceptMap.TargetElementComponent; 013import org.hl7.fhir.r5.model.Identifier; 014import org.hl7.fhir.r5.model.Meta; 015import org.hl7.fhir.r5.model.UriType; 016import org.hl7.fhir.r5.model.ValueSet; 017 018public class ConceptMapUtilities { 019 020 public static boolean hasOID(ConceptMap cm) { 021 return getOID(cm) != null; 022 } 023 024 public static String getOID(ConceptMap cm) { 025 for (Identifier id : cm.getIdentifier()) { 026 if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:")) 027 return id.getValue().substring(8); 028 } 029 return null; 030 } 031 032 public static void setOID(ConceptMap cm, String oid) { 033 if (!oid.startsWith("urn:oid:")) 034 oid = "urn:oid:" + oid; 035 for (Identifier id : cm.getIdentifier()) { 036 if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:")) { 037 id.setValue(oid); 038 return; 039 } 040 } 041 cm.addIdentifier().setSystem("urn:ietf:rfc:3986").setValue(oid); 042 } 043 044 public static boolean hasMappingForSource(ConceptMap cm, String system, String version, String code) { 045 for (ConceptMapGroupComponent grp : cm.getGroup()) { 046 if (system.equals(grp.getSource())) { // to do: version 047 for (SourceElementComponent e : grp.getElement()) { 048 if (code.equals(e.getCode())) { 049 return true; // doesn't matter if it's actually unmapped 050 } 051 } 052 } 053 } 054 return false; 055 } 056 057 public static List<Coding> listTargets(ConceptMap cm, List<String> systems) { 058 List<Coding> list = new ArrayList<>(); 059 for (ConceptMapGroupComponent grp : cm.getGroup()) { 060 if (systems.isEmpty() || systems.contains(grp.getSource())) { // to do: version 061 for (SourceElementComponent e : grp.getElement()) { 062 for (TargetElementComponent t : e.getTarget()) { 063 if (t.hasCode()) { 064 list.add(new Coding(grp.getTarget(), t.getCode(), t.getDisplay())); 065 } 066 } 067 } 068 } 069 } 070 return list; 071 } 072 073 074 public static ConceptMap makeShareable(ConceptMap cm) { 075 if (!cm.hasExperimental()) { 076 cm.setExperimental(false); 077 } 078 079 if (!cm.hasMeta()) 080 cm.setMeta(new Meta()); 081 for (UriType t : cm.getMeta().getProfile()) 082 if ("http://hl7.org/fhir/StructureDefinition/shareableconceptmap".equals(t.getValue())) 083 return cm; 084 cm.getMeta().getProfile().add(new CanonicalType("http://hl7.org/fhir/StructureDefinition/shareableconceptmap")); 085 return cm; 086 } 087 088}