001package org.hl7.fhir.dstu3.utils;
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
033
034import org.hl7.fhir.dstu3.model.CodeSystem;
035import org.hl7.fhir.dstu3.model.Identifier;
036import org.hl7.fhir.dstu3.model.Meta;
037import org.hl7.fhir.dstu3.model.UriType;
038import org.hl7.fhir.dstu3.model.ValueSet;
039import org.hl7.fhir.utilities.Utilities;
040
041public class ValueSetUtilities {
042
043  public static ValueSet makeShareable(ValueSet vs) {
044    if (!vs.hasMeta())
045      vs.setMeta(new Meta());
046    for (UriType t : vs.getMeta().getProfile()) 
047      if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablevalueset"))
048        return vs;
049    vs.getMeta().getProfile().add(new UriType("http://hl7.org/fhir/StructureDefinition/shareablevalueset"));
050    return vs;
051  }
052
053  public static void checkShareable(ValueSet vs) {
054    if (!vs.hasMeta())
055      throw new Error("ValueSet "+vs.getUrl()+" is not shareable");
056    for (UriType t : vs.getMeta().getProfile()) {
057      if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablevalueset"))
058        return;
059    }
060    throw new Error("ValueSet "+vs.getUrl()+" is not shareable");    
061  }
062
063  public static boolean hasOID(ValueSet vs) {
064    return getOID(vs) != null;
065  }
066
067  public static String getOID(ValueSet vs) {
068    for (Identifier id : vs.getIdentifier()) {
069      if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:"))
070        return id.getValue().substring(8);
071    }
072    return null;
073  }
074
075  public static void setOID(ValueSet vs, String oid) {
076    if (!oid.startsWith("urn:oid:"))
077      oid = "urn:oid:" + oid;
078    for (Identifier id : vs.getIdentifier()) {
079      if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:")) {
080        id.setValue(oid);
081        return;
082      }
083    }
084    vs.addIdentifier().setSystem("urn:ietf:rfc:3986").setValue(oid);
085  }
086
087  public static void markStatus(ValueSet vs, String wg, String status, String fmm) {
088    if (wg != null) {
089      if (!ExtensionUtilities.hasExtension(vs, ExtensionUtilities.EXT_WORKGROUP) ||
090          (Utilities.existsInList(ExtensionUtilities.readStringExtension(vs, ExtensionUtilities.EXT_WORKGROUP), "fhir", "vocab") && !Utilities.existsInList(wg, "fhir", "vocab"))) {
091        ExtensionUtilities.setCodeExtension(vs, ExtensionUtilities.EXT_WORKGROUP, wg);
092      }
093    }
094    if (status != null) {
095      String ss = ExtensionUtilities.readStringExtension(vs, ExtensionUtilities.EXT_BALLOT_STATUS);
096      if (Utilities.noString(ss) || ssval(ss) < ssval(status)) 
097        ExtensionUtilities.setStringExtension(vs, ExtensionUtilities.EXT_BALLOT_STATUS, status);
098    }
099    if (fmm != null) {
100      String sfmm = ExtensionUtilities.readStringExtension(vs, ExtensionUtilities.EXT_FMM_LEVEL);
101      if (Utilities.noString(sfmm) || Integer.parseInt(sfmm) < Integer.parseInt(fmm)) 
102        ExtensionUtilities.setIntegerExtension(vs, ExtensionUtilities.EXT_FMM_LEVEL, Integer.parseInt(fmm));
103    }
104    if (vs.hasUserData("cs"))
105      CodeSystemUtilities.markStatus((CodeSystem) vs.getUserData("cs"), wg, status, fmm);
106  }
107
108  private static int ssval(String status) {
109    if ("Draft".equals("status")) 
110      return 1;
111    if ("Informative".equals("status")) 
112      return 2;
113    if ("External".equals("status")) 
114      return 3;
115    if ("Trial Use".equals("status")) 
116      return 3;
117    if ("Normative".equals("status")) 
118      return 4;
119    return -1;
120  }
121
122}