001package org.hl7.fhir.dstu3.terminologies;
002
003import org.hl7.fhir.dstu3.model.Identifier;
004import org.hl7.fhir.dstu3.model.Meta;
005import org.hl7.fhir.dstu3.model.UriType;
006import org.hl7.fhir.dstu3.model.ValueSet;
007
008public class ValueSetUtilities {
009
010  public static ValueSet makeShareable(ValueSet vs) {
011    if (!vs.hasMeta())
012      vs.setMeta(new Meta());
013    for (UriType t : vs.getMeta().getProfile()) 
014      if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/valueset-shareable-definition"))
015        return vs;
016    vs.getMeta().getProfile().add(new UriType("http://hl7.org/fhir/StructureDefinition/valueset-shareable-definition"));
017    return vs;
018  }
019
020  public static void checkShareable(ValueSet vs) {
021    if (!vs.hasMeta())
022      throw new Error("ValueSet "+vs.getUrl()+" is not shareable");
023    for (UriType t : vs.getMeta().getProfile()) {
024      if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/valueset-shareable-definition"))
025        return;
026    }
027    throw new Error("ValueSet "+vs.getUrl()+" is not shareable");    
028  }
029
030  public static boolean hasOID(ValueSet vs) {
031    return getOID(vs) != null;
032  }
033
034  public static String getOID(ValueSet vs) {
035    for (Identifier id : vs.getIdentifier()) {
036      if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:"))
037        return id.getValue().substring(8);
038    }
039    return null;
040  }
041
042  public static void setOID(ValueSet vs, String oid) {
043    if (!oid.startsWith("urn:oid:"))
044      oid = "urn:oid:" + oid;
045    for (Identifier id : vs.getIdentifier()) {
046      if ("urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:")) {
047        id.setValue(oid);
048        return;
049      }
050    }
051    vs.addIdentifier().setSystem("urn:ietf:rfc:3986").setValue(oid);
052  }
053
054}