001package org.hl7.fhir.dstu3.terminologies; 002 003/*- 004 * #%L 005 * org.hl7.fhir.dstu3 006 * %% 007 * Copyright (C) 2014 - 2019 Health Level 7 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023 024import org.hl7.fhir.dstu3.model.CodeSystem; 025import org.hl7.fhir.dstu3.model.Identifier; 026import org.hl7.fhir.dstu3.model.Meta; 027import org.hl7.fhir.dstu3.model.UriType; 028import org.hl7.fhir.dstu3.model.ValueSet; 029import org.hl7.fhir.dstu3.utils.ToolingExtensions; 030import org.hl7.fhir.utilities.Utilities; 031 032public class ValueSetUtilities { 033 034 public static ValueSet makeShareable(ValueSet vs) { 035 if (!vs.hasMeta()) 036 vs.setMeta(new Meta()); 037 for (UriType t : vs.getMeta().getProfile()) 038 if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablevalueset")) 039 return vs; 040 vs.getMeta().getProfile().add(new UriType("http://hl7.org/fhir/StructureDefinition/shareablevalueset")); 041 return vs; 042 } 043 044 public static void checkShareable(ValueSet vs) { 045 if (!vs.hasMeta()) 046 throw new Error("ValueSet "+vs.getUrl()+" is not shareable"); 047 for (UriType t : vs.getMeta().getProfile()) { 048 if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablevalueset")) 049 return; 050 } 051 throw new Error("ValueSet "+vs.getUrl()+" is not shareable"); 052 } 053 054 public static boolean hasOID(ValueSet vs) { 055 return getOID(vs) != null; 056 } 057 058 public static String getOID(ValueSet vs) { 059 for (Identifier id : vs.getIdentifier()) { 060 if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:")) 061 return id.getValue().substring(8); 062 } 063 return null; 064 } 065 066 public static void setOID(ValueSet vs, String oid) { 067 if (!oid.startsWith("urn:oid:")) 068 oid = "urn:oid:" + oid; 069 for (Identifier id : vs.getIdentifier()) { 070 if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:")) { 071 id.setValue(oid); 072 return; 073 } 074 } 075 vs.addIdentifier().setSystem("urn:ietf:rfc:3986").setValue(oid); 076 } 077 078 public static void markStatus(ValueSet vs, String wg, String status, String fmm) { 079 if (wg != null) { 080 if (!ToolingExtensions.hasExtension(vs, ToolingExtensions.EXT_WORKGROUP) || 081 (Utilities.existsInList(ToolingExtensions.readStringExtension(vs, ToolingExtensions.EXT_WORKGROUP), "fhir", "vocab") && !Utilities.existsInList(wg, "fhir", "vocab"))) { 082 ToolingExtensions.setCodeExtension(vs, ToolingExtensions.EXT_WORKGROUP, wg); 083 } 084 } 085 if (status != null) { 086 String ss = ToolingExtensions.readStringExtension(vs, ToolingExtensions.EXT_BALLOT_STATUS); 087 if (Utilities.noString(ss) || ssval(ss) < ssval(status)) 088 ToolingExtensions.setStringExtension(vs, ToolingExtensions.EXT_BALLOT_STATUS, status); 089 } 090 if (fmm != null) { 091 String sfmm = ToolingExtensions.readStringExtension(vs, ToolingExtensions.EXT_FMM_LEVEL); 092 if (Utilities.noString(sfmm) || Integer.parseInt(sfmm) < Integer.parseInt(fmm)) 093 ToolingExtensions.setIntegerExtension(vs, ToolingExtensions.EXT_FMM_LEVEL, Integer.parseInt(fmm)); 094 } 095 if (vs.hasUserData("cs")) 096 CodeSystemUtilities.markStatus((CodeSystem) vs.getUserData("cs"), wg, status, fmm); 097 } 098 099 private static int ssval(String status) { 100 if ("Draft".equals("status")) 101 return 1; 102 if ("Informative".equals("status")) 103 return 2; 104 if ("External".equals("status")) 105 return 3; 106 if ("Trial Use".equals("status")) 107 return 3; 108 if ("Normative".equals("status")) 109 return 4; 110 return -1; 111 } 112 113}