001package org.hl7.fhir.utilities; 002 003import java.io.BufferedReader; 004import java.io.IOException; 005import java.io.InputStream; 006import java.io.InputStreamReader; 007import java.util.HashMap; 008import java.util.Map; 009 010import org.hl7.fhir.exceptions.FHIRException; 011 012/* 013 Copyright (c) 2011+, HL7, Inc. 014 All rights reserved. 015 016 Redistribution and use in source and binary forms, with or without modification, 017 are permitted provided that the following conditions are met: 018 019 * Redistributions of source code must retain the above copyright notice, this 020 list of conditions and the following disclaimer. 021 * Redistributions in binary form must reproduce the above copyright notice, 022 this list of conditions and the following disclaimer in the documentation 023 and/or other materials provided with the distribution. 024 * Neither the name of HL7 nor the names of its contributors may be used to 025 endorse or promote products derived from this software without specific 026 prior written permission. 027 028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 029 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 030 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 031 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 032 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 033 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 034 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 035 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 036 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 037 POSSIBILITY OF SUCH DAMAGE. 038 039 */ 040 041 042 043public class OIDUtilities { 044 045 // Simple class to hold OID information 046 public static class OIDInfo { 047 private String oid; 048 private String name; 049 private String codeSystem; 050 private Integer type; 051 052 // Getters and setters 053 public String getOid() { return oid; } 054 public void setOid(String oid) { this.oid = oid; } 055 056 public String getName() { return name; } 057 public void setName(String name) { this.name = name; } 058 059 public String getCodeSystem() { return codeSystem; } 060 public void setCodeSystem(String codeSystem) { this.codeSystem = codeSystem; } 061 062 public Integer getType() { return type; } 063 public void setType(Integer type) { this.type = type; } 064 065 @Override 066 public String toString() { 067 return "OID: " + oid + 068 ", Name: " + name + 069 ", CodeSystem: " + codeSystem + 070 ", Type: " + type; 071 } 072 } 073 074 private static final String OID_REGEX = "[0-2](\\.(0|[1-9][0-9]*))+"; 075 private final Map<String, OIDInfo> oidMap = new HashMap<>(); 076 077 078 public OIDUtilities() { 079 loadOIDsFromCSV(); 080 } 081 082 private void loadOIDsFromCSV() throws FHIRException { 083 try { 084 085 try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("oidregistry.csv"); 086 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { 087 088 String line; 089 boolean isHeaderProcessed = false; 090 091 while ((line = reader.readLine()) != null) { 092 if (!isHeaderProcessed) { 093 // Skip header line 094 isHeaderProcessed = true; 095 } else { 096 String[] parts = line.split(","); 097 if (parts.length >= 4) { 098 OIDInfo info = new OIDInfo(); 099 info.setOid(parts[0].trim()); 100 info.setType(Integer.parseInt(parts[1].trim())); 101 info.setName(parts[2].trim()); 102 info.setCodeSystem(parts[3].trim()); 103 104 // Add to map with OID as key 105 oidMap.put(info.getOid(), info); 106 } 107 } 108 } 109 } 110 } catch (Exception e) { 111 throw new FHIRException(e); 112 } 113 } 114 115 /* 116 2.16.840.1.113883.3.72.5.2 - NIST owns this 117 2.16.840.1.113883.4.6 - National Provider Identifier 118 2.16.840.1.113883.6.21 - UB92 119 2.16.840.1.113883.6.69 - NDC 120 */ 121 122 public static String getUriForOid(String r) { 123 if (r == null) { 124 return null; 125 } 126 if (r.equals("2.16.840.1.113883.6.96")) 127 return "http://snomed.info/sct"; 128 if (r.equals("2.16.840.1.113883.6.1")) 129 return "http://loinc.org"; 130 if (r.equals("2.16.840.1.113883.6.8")) 131 return "http://unitsofmeasure.org"; 132 if (r.equals("2.16.840.1.113883.6.3")) 133 return "http://hl7.org/fhir/sid/icd-10"; 134 if (r.equals("2.16.840.1.113883.6.42")) 135 return "http://hl7.org/fhir/sid/icd-9"; 136 if (r.equals("2.16.840.1.113883.6.104")) 137 return "http://hl7.org/fhir/sid/icd-9"; 138 if (r.equals("2.16.840.1.113883.6.103")) 139 return "http://hl7.org/fhir/sid/icd-9"; //todo: confirm this 140 if (r.equals("2.16.840.1.113883.6.73")) 141 return "http://www.whocc.no/atc"; 142 if (r.equals("2.16.840.1.113883.3.26.1.1")) 143 return "http://ncimeta.nci.nih.gov"; 144 if (r.equals("2.16.840.1.113883.3.26.1.1.1")) 145 return "http://ncimeta.nci.nih.gov"; 146 if (r.equals("2.16.840.1.113883.6.88")) 147 return "http://www.nlm.nih.gov/research/umls/rxnorm"; // todo: confirm this 148 149 if (r.equals("2.16.840.1.113883.6.59")) 150 return "http://hl7.org/fhir/sid/cvx"; 151 if (r.equals("2.16.840.1.113883.12.292")) 152 return "http://hl7.org/fhir/sid/cvx"; 153 154 if (r.equals("2.16.840.1.113883.6.12")) 155 return "http://www.ama-assn.org/go/cpt"; 156 157 return null; 158 } 159 160 public static String oidTail(String id) { 161 if (id == null || !id.contains(".")) 162 return id; 163 return id.substring(id.lastIndexOf(".") + 1); 164 } 165 166 public static String oidRoot(String id) { 167 if (id == null || !id.contains(".")) 168 return id; 169 return id.substring(0, id.indexOf(".")); 170 } 171 172 public static boolean isValidOID(String oid) { 173 return oid.matches(OID_REGEX); 174 } 175 176 public boolean isKnownOID(String url) { 177 if (url == null || !url.startsWith("urn:oid:") ) { 178 return false; 179 } else { 180 return oidMap.containsKey(url.substring(8)); 181 } 182 } 183}