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