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