
001package org.hl7.fhir.r4.terminologies; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032import lombok.extern.slf4j.Slf4j; 033import org.hl7.fhir.exceptions.FHIRException; 034import org.hl7.fhir.r4.context.IWorkerContext; 035import org.hl7.fhir.r4.model.CanonicalType; 036import org.hl7.fhir.r4.model.CodeSystem; 037import org.hl7.fhir.r4.model.Enumerations.PublicationStatus; 038import org.hl7.fhir.r4.model.Identifier; 039import org.hl7.fhir.r4.model.Meta; 040import org.hl7.fhir.r4.model.UriType; 041import org.hl7.fhir.r4.model.ValueSet; 042import org.hl7.fhir.r4.model.ValueSet.ConceptSetComponent; 043import org.hl7.fhir.r4.utils.ToolingExtensions; 044import org.hl7.fhir.utilities.StandardsStatus; 045import org.hl7.fhir.utilities.Utilities; 046 047@Slf4j 048public class ValueSetUtilities { 049 050 public static ValueSet makeShareable(ValueSet vs) { 051 if (!vs.hasMeta()) 052 vs.setMeta(new Meta()); 053 for (UriType t : vs.getMeta().getProfile()) 054 if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablevalueset")) 055 return vs; 056 vs.getMeta().getProfile().add(new CanonicalType("http://hl7.org/fhir/StructureDefinition/shareablevalueset")); 057 return vs; 058 } 059 060 public static void checkShareable(ValueSet vs) { 061 if (!vs.hasMeta()) 062 throw new Error("ValueSet " + vs.getUrl() + " is not shareable"); 063 for (UriType t : vs.getMeta().getProfile()) { 064 if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablevalueset")) 065 return; 066 } 067 throw new Error("ValueSet " + vs.getUrl() + " is not shareable"); 068 } 069 070 public static boolean hasOID(ValueSet vs) { 071 return getOID(vs) != null; 072 } 073 074 public static String getOID(ValueSet vs) { 075 for (Identifier id : vs.getIdentifier()) { 076 if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:")) 077 return id.getValue().substring(8); 078 } 079 return null; 080 } 081 082 public static void setOID(ValueSet vs, String oid) { 083 if (!oid.startsWith("urn:oid:")) 084 oid = "urn:oid:" + oid; 085 for (Identifier id : vs.getIdentifier()) { 086 if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:")) { 087 id.setValue(oid); 088 return; 089 } 090 } 091 vs.addIdentifier().setSystem("urn:ietf:rfc:3986").setValue(oid); 092 } 093 094 public static void markStatus(ValueSet vs, String wg, StandardsStatus status, String pckage, String fmm, 095 IWorkerContext context, String normativeVersion) throws FHIRException { 096 if (vs.hasUserData("external.url")) 097 return; 098 099 if (wg != null) { 100 if (!ToolingExtensions.hasExtension(vs, ToolingExtensions.EXT_WORKGROUP) 101 || (!Utilities.existsInList(ToolingExtensions.readStringExtension(vs, ToolingExtensions.EXT_WORKGROUP), 102 "fhir", "vocab") && Utilities.existsInList(wg, "fhir", "vocab"))) { 103 ToolingExtensions.setCodeExtension(vs, ToolingExtensions.EXT_WORKGROUP, wg); 104 } 105 } 106 if (status != null) { 107 StandardsStatus ss = ToolingExtensions.getStandardsStatus(vs); 108 if (ss == null || ss.isLowerThan(status)) 109 ToolingExtensions.setStandardsStatus(vs, status, normativeVersion); 110 if (pckage != null) { 111 if (!vs.hasUserData("ballot.package")) 112 vs.setUserData("ballot.package", pckage); 113 else if (!pckage.equals(vs.getUserString("ballot.package"))) 114 if (!"infrastructure".equals(vs.getUserString("ballot.package"))) 115 log.warn("Value Set " + vs.getUrl() + ": ownership clash " + pckage + " vs " 116 + vs.getUserString("ballot.package")); 117 } 118 if (status == StandardsStatus.NORMATIVE) { 119 vs.setExperimental(false); 120 vs.setStatus(PublicationStatus.ACTIVE); 121 } 122 } 123 if (fmm != null) { 124 String sfmm = ToolingExtensions.readStringExtension(vs, ToolingExtensions.EXT_FMM_LEVEL); 125 if (Utilities.noString(sfmm) || Integer.parseInt(sfmm) < Integer.parseInt(fmm)) 126 ToolingExtensions.setIntegerExtension(vs, ToolingExtensions.EXT_FMM_LEVEL, Integer.parseInt(fmm)); 127 } 128 if (vs.hasUserData("cs")) 129 CodeSystemUtilities.markStatus((CodeSystem) vs.getUserData("cs"), wg, status, pckage, fmm, normativeVersion); 130 else if (status == StandardsStatus.NORMATIVE && context != null) { 131 for (ConceptSetComponent csc : vs.getCompose().getInclude()) { 132 if (csc.hasSystem()) { 133 CodeSystem cs = context.fetchCodeSystem(csc.getSystem()); 134 if (cs != null) { 135 CodeSystemUtilities.markStatus(cs, wg, status, pckage, fmm, normativeVersion); 136 } 137 } 138 } 139 } 140 } 141 142 private static int ssval(String status) { 143 if ("Draft".equals("status")) 144 return 1; 145 if ("Informative".equals("status")) 146 return 2; 147 if ("External".equals("status")) 148 return 3; 149 if ("Trial Use".equals("status")) 150 return 3; 151 if ("Normative".equals("status")) 152 return 4; 153 return -1; 154 } 155 156}