001package org.hl7.fhir.r5.terminologies;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r5
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
023import org.hl7.fhir.exceptions.FHIRException;
024import org.hl7.fhir.r5.context.IWorkerContext;
025import org.hl7.fhir.r5.model.CanonicalType;
026import org.hl7.fhir.r5.model.CodeSystem;
027import org.hl7.fhir.r5.model.Enumerations.PublicationStatus;
028import org.hl7.fhir.r5.model.Identifier;
029import org.hl7.fhir.r5.model.Meta;
030import org.hl7.fhir.r5.model.UriType;
031import org.hl7.fhir.r5.model.ValueSet;
032import org.hl7.fhir.r5.model.ValueSet.ConceptSetComponent;
033import org.hl7.fhir.r5.model.Enumerations.FilterOperator;
034import org.hl7.fhir.r5.utils.ToolingExtensions;
035import org.hl7.fhir.utilities.StandardsStatus;
036import org.hl7.fhir.utilities.Utilities;
037
038public class ValueSetUtilities {
039
040  public static ValueSet makeShareable(ValueSet vs) {
041    if (!vs.hasMeta())
042      vs.setMeta(new Meta());
043    for (UriType t : vs.getMeta().getProfile()) 
044      if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablevalueset"))
045        return vs;
046    vs.getMeta().getProfile().add(new CanonicalType("http://hl7.org/fhir/StructureDefinition/shareablevalueset"));
047    return vs;
048  }
049
050  public static void checkShareable(ValueSet vs) {
051    if (!vs.hasMeta())
052      throw new Error("ValueSet "+vs.getUrl()+" is not shareable");
053    for (UriType t : vs.getMeta().getProfile()) {
054      if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablevalueset"))
055        return;
056    }
057    throw new Error("ValueSet "+vs.getUrl()+" is not shareable");    
058  }
059
060  public static boolean hasOID(ValueSet vs) {
061    return getOID(vs) != null;
062  }
063
064  public static String getOID(ValueSet vs) {
065    for (Identifier id : vs.getIdentifier()) {
066      if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:"))
067        return id.getValue().substring(8);
068    }
069    return null;
070  }
071
072  public static void setOID(ValueSet vs, String oid) {
073    if (!oid.startsWith("urn:oid:"))
074      oid = "urn:oid:" + oid;
075    for (Identifier id : vs.getIdentifier()) {
076      if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:")) {
077        id.setValue(oid);
078        return;
079      }
080    }
081    vs.addIdentifier().setSystem("urn:ietf:rfc:3986").setValue(oid);
082  }
083
084  public static void markStatus(ValueSet vs, String wg, StandardsStatus status, String pckage, String fmm, IWorkerContext context, String normativeVersion) throws FHIRException {
085    if (vs.hasUserData("external.url"))
086      return;
087    
088    if (wg != null) {
089      if (!ToolingExtensions.hasExtension(vs, ToolingExtensions.EXT_WORKGROUP) || 
090          (!Utilities.existsInList(ToolingExtensions.readStringExtension(vs, ToolingExtensions.EXT_WORKGROUP), "fhir", "vocab") && Utilities.existsInList(wg, "fhir", "vocab"))) {
091        ToolingExtensions.setCodeExtension(vs, ToolingExtensions.EXT_WORKGROUP, wg);
092      }
093    }
094    if (status != null) {
095      StandardsStatus ss = ToolingExtensions.getStandardsStatus(vs);
096      if (ss == null || ss.isLowerThan(status)) 
097        ToolingExtensions.setStandardsStatus(vs, status, normativeVersion);
098      if (pckage != null) {
099        if (!vs.hasUserData("ballot.package"))        
100          vs.setUserData("ballot.package", pckage);
101        else if (!pckage.equals(vs.getUserString("ballot.package")))
102          if (!"infrastructure".equals(vs.getUserString("ballot.package")))
103          System.out.println("Value Set "+vs.getUrl()+": ownership clash "+pckage+" vs "+vs.getUserString("ballot.package"));
104      }
105      if (status == StandardsStatus.NORMATIVE) {
106        vs.setExperimental(false);
107        vs.setStatus(PublicationStatus.ACTIVE);
108      }
109    }
110    if (fmm != null) {
111      String sfmm = ToolingExtensions.readStringExtension(vs, ToolingExtensions.EXT_FMM_LEVEL);
112      if (Utilities.noString(sfmm) || Integer.parseInt(sfmm) < Integer.parseInt(fmm)) 
113        ToolingExtensions.setIntegerExtension(vs, ToolingExtensions.EXT_FMM_LEVEL, Integer.parseInt(fmm));
114    }
115    if (vs.hasUserData("cs"))
116      CodeSystemUtilities.markStatus((CodeSystem) vs.getUserData("cs"), wg, status, pckage, fmm, normativeVersion);
117    else if (status == StandardsStatus.NORMATIVE && context != null) {
118      for (ConceptSetComponent csc : vs.getCompose().getInclude()) {
119        if (csc.hasSystem()) {
120          CodeSystem cs = context.fetchCodeSystem(csc.getSystem());
121          if (cs != null) {
122            CodeSystemUtilities.markStatus(cs, wg, status, pckage, fmm, normativeVersion);
123          }
124        }
125      }
126    }
127  }
128
129  private static int ssval(String status) {
130    if ("Draft".equals("status")) 
131      return 1;
132    if ("Informative".equals("status")) 
133      return 2;
134    if ("External".equals("status")) 
135      return 3;
136    if ("Trial Use".equals("status")) 
137      return 3;
138    if ("Normative".equals("status")) 
139      return 4;
140    return -1;
141  }
142
143  public static ValueSet generateImplicitValueSet(String uri) {
144    if (uri.startsWith("http://snomed.info/sct"))
145      return generateImplicitSnomedValueSet(uri);
146    if (uri.startsWith("http://loinc.org/vs"))
147      return generateImplicitLoincValueSet(uri);
148    if (uri.equals("http://hl7.org/fhir/ValueSet/mimetypes")) {
149      return generateImplicitMimetypesValueSet(uri);
150    }
151    return null;
152  }
153
154  private static ValueSet generateImplicitMimetypesValueSet(String theUri) {
155    ValueSet valueSet = new ValueSet();
156    valueSet.setUrl(theUri);
157    valueSet.setDescription("This value set includes all possible codes from BCP-13 (http://tools.ietf.org/html/bcp13)");
158    valueSet.getCompose()
159      .addInclude().setSystem("urn:ietf:bcp:13");
160    return valueSet;
161  }
162
163  private static ValueSet generateImplicitLoincValueSet(String uri) {
164    if ("http://loinc.org/vs".equals(uri))
165      return makeLoincValueSet();
166    if (uri.startsWith("http://loinc.org/vs/LL"))
167      return makeAnswerList(makeLoincValueSet(), uri);
168    return null;
169  }
170
171  private static ValueSet makeAnswerList(ValueSet vs, String uri) {
172    vs.setUrl(uri);
173    String c = uri.substring(20);
174    vs.setName("LOINCAnswers"+c);
175    vs.setTitle("LOINC Answer Codes for "+c);
176    vs.getCompose().getIncludeFirstRep().addFilter().setProperty("LIST").setOp(FilterOperator.EQUAL).setValue(c);
177    return vs;
178  }
179
180  private static ValueSet makeLoincValueSet() {
181    ValueSet vs = new ValueSet();
182    vs.setUrl("http://loinc.org/vs");
183    vs.setName("LOINCCodes");
184    vs.setTitle("All LOINC codes");
185    vs.setCopyright("This content LOINC® is copyright © 1995 Regenstrief Institute, Inc. and the LOINC Committee, and available at no cost under the license at http://loinc.org/terms-of-use");
186    vs.setStatus(PublicationStatus.ACTIVE);
187    vs.getCompose().addInclude().setSystem("http://loinc.org");
188    return vs;
189  }
190
191  private static ValueSet generateImplicitSnomedValueSet(String uri) {
192    if ("http://snomed.info/sct?fhir_vs".equals(uri))
193      return makeImplicitSnomedValueSet(uri);
194    return null;
195  }
196
197  private static ValueSet makeImplicitSnomedValueSet(String uri) {
198    ValueSet vs = new ValueSet();
199    vs.setUrl(uri);
200    vs.setName("SCTValueSet");
201    vs.setTitle("SCT ValueSet");
202    vs.setDescription("All SNOMED CT Concepts");
203    vs.setCopyright("This value set includes content from SNOMED CT, which is copyright © 2002+ International Health Terminology Standards Development Organisation (SNOMED International), and distributed by agreement between SNOMED International and HL7. Implementer use of SNOMED CT is not covered by this agreement");
204    vs.setStatus(PublicationStatus.ACTIVE);
205    vs.getCompose().addInclude().setSystem("http://snomed.info/sct");
206    return vs;
207  }
208
209}