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 org.hl7.fhir.exceptions.FHIRException;
033import org.hl7.fhir.r4.context.IWorkerContext;
034import org.hl7.fhir.r4.model.CanonicalType;
035import org.hl7.fhir.r4.model.CodeSystem;
036import org.hl7.fhir.r4.model.Enumerations.PublicationStatus;
037import org.hl7.fhir.r4.model.Identifier;
038import org.hl7.fhir.r4.model.Meta;
039import org.hl7.fhir.r4.model.UriType;
040import org.hl7.fhir.r4.model.ValueSet;
041import org.hl7.fhir.r4.model.ValueSet.ConceptSetComponent;
042import org.hl7.fhir.r4.utils.ToolingExtensions;
043import org.hl7.fhir.utilities.StandardsStatus;
044import org.hl7.fhir.utilities.Utilities;
045
046public class ValueSetUtilities {
047
048  public static ValueSet makeShareable(ValueSet vs) {
049    if (!vs.hasMeta())
050      vs.setMeta(new Meta());
051    for (UriType t : vs.getMeta().getProfile())
052      if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablevalueset"))
053        return vs;
054    vs.getMeta().getProfile().add(new CanonicalType("http://hl7.org/fhir/StructureDefinition/shareablevalueset"));
055    return vs;
056  }
057
058  public static void checkShareable(ValueSet vs) {
059    if (!vs.hasMeta())
060      throw new Error("ValueSet " + vs.getUrl() + " is not shareable");
061    for (UriType t : vs.getMeta().getProfile()) {
062      if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablevalueset"))
063        return;
064    }
065    throw new Error("ValueSet " + vs.getUrl() + " is not shareable");
066  }
067
068  public static boolean hasOID(ValueSet vs) {
069    return getOID(vs) != null;
070  }
071
072  public static String getOID(ValueSet vs) {
073    for (Identifier id : vs.getIdentifier()) {
074      if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:"))
075        return id.getValue().substring(8);
076    }
077    return null;
078  }
079
080  public static void setOID(ValueSet vs, String oid) {
081    if (!oid.startsWith("urn:oid:"))
082      oid = "urn:oid:" + oid;
083    for (Identifier id : vs.getIdentifier()) {
084      if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:")) {
085        id.setValue(oid);
086        return;
087      }
088    }
089    vs.addIdentifier().setSystem("urn:ietf:rfc:3986").setValue(oid);
090  }
091
092  public static void markStatus(ValueSet vs, String wg, StandardsStatus status, String pckage, String fmm,
093      IWorkerContext context, String normativeVersion) throws FHIRException {
094    if (vs.hasUserData("external.url"))
095      return;
096
097    if (wg != null) {
098      if (!ToolingExtensions.hasExtension(vs, ToolingExtensions.EXT_WORKGROUP)
099          || (!Utilities.existsInList(ToolingExtensions.readStringExtension(vs, ToolingExtensions.EXT_WORKGROUP),
100              "fhir", "vocab") && Utilities.existsInList(wg, "fhir", "vocab"))) {
101        ToolingExtensions.setCodeExtension(vs, ToolingExtensions.EXT_WORKGROUP, wg);
102      }
103    }
104    if (status != null) {
105      StandardsStatus ss = ToolingExtensions.getStandardsStatus(vs);
106      if (ss == null || ss.isLowerThan(status))
107        ToolingExtensions.setStandardsStatus(vs, status, normativeVersion);
108      if (pckage != null) {
109        if (!vs.hasUserData("ballot.package"))
110          vs.setUserData("ballot.package", pckage);
111        else if (!pckage.equals(vs.getUserString("ballot.package")))
112          if (!"infrastructure".equals(vs.getUserString("ballot.package")))
113            System.out.println("Value Set " + vs.getUrl() + ": ownership clash " + pckage + " vs "
114                + vs.getUserString("ballot.package"));
115      }
116      if (status == StandardsStatus.NORMATIVE) {
117        vs.setExperimental(false);
118        vs.setStatus(PublicationStatus.ACTIVE);
119      }
120    }
121    if (fmm != null) {
122      String sfmm = ToolingExtensions.readStringExtension(vs, ToolingExtensions.EXT_FMM_LEVEL);
123      if (Utilities.noString(sfmm) || Integer.parseInt(sfmm) < Integer.parseInt(fmm))
124        ToolingExtensions.setIntegerExtension(vs, ToolingExtensions.EXT_FMM_LEVEL, Integer.parseInt(fmm));
125    }
126    if (vs.hasUserData("cs"))
127      CodeSystemUtilities.markStatus((CodeSystem) vs.getUserData("cs"), wg, status, pckage, fmm, normativeVersion);
128    else if (status == StandardsStatus.NORMATIVE && context != null) {
129      for (ConceptSetComponent csc : vs.getCompose().getInclude()) {
130        if (csc.hasSystem()) {
131          CodeSystem cs = context.fetchCodeSystem(csc.getSystem());
132          if (cs != null) {
133            CodeSystemUtilities.markStatus(cs, wg, status, pckage, fmm, normativeVersion);
134          }
135        }
136      }
137    }
138  }
139
140  private static int ssval(String status) {
141    if ("Draft".equals("status"))
142      return 1;
143    if ("Informative".equals("status"))
144      return 2;
145    if ("External".equals("status"))
146      return 3;
147    if ("Trial Use".equals("status"))
148      return 3;
149    if ("Normative".equals("status"))
150      return 4;
151    return -1;
152  }
153
154}