001package org.hl7.fhir.r5.terminologies.validation;
002
003import org.hl7.fhir.exceptions.FHIRException;
004import org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent;
005import org.hl7.fhir.utilities.MarkedToMoveToAdjunctPackage;
006
007@MarkedToMoveToAdjunctPackage
008public class VersionInfo {
009  /**
010   * 
011   */
012  private final ValueSetValidator valueSetCheckerSimple;
013
014  /**
015   * @param valueSetCheckerSimple
016   */
017  VersionInfo(ValueSetValidator valueSetCheckerSimple) {
018    this.valueSetCheckerSimple = valueSetCheckerSimple;
019  }
020
021  private String expansionVersion;
022  private String composeVersion;
023
024  public String getExpansionVersion() {
025    return expansionVersion;
026  }
027
028  public void setExpansionVersion(String expansionVersion) {
029    this.expansionVersion = expansionVersion;
030  }
031
032  public String getComposeVersion() {
033    return composeVersion;
034  }
035
036  public void setComposeVersion(String composeVersion) {
037    this.composeVersion = composeVersion;
038  }
039
040  public String getVersion(String system, String version) {
041    String fixedVersion = getVersionParameter("force-system-version", system);
042    if (fixedVersion != null) {
043      return fixedVersion;
044    }
045    String checkVersion = getVersionParameter("check-system-version", system);
046    if (version != null) {
047      if (checkVersion != null && !version.equals(checkVersion)) {
048        throw new FHIRException("Attempt to use version "+version+" of "+system+", when the expansion parameters limit the use to "+checkVersion);
049      }
050      return version;
051    }
052    if (expansionVersion != null) {
053      if (checkVersion != null && !expansionVersion.equals(checkVersion)) {
054        throw new FHIRException("Attempt to use version "+expansionVersion+" of "+system+", when the expansion parameters limit the use to "+checkVersion);
055      }
056      return expansionVersion;
057    }
058    if (composeVersion != null) {
059      if (checkVersion != null && !composeVersion.equals(checkVersion)) {
060        throw new FHIRException("Attempt to use version "+composeVersion+" of "+system+", when the expansion parameters limit the use to "+checkVersion);
061      }
062      return composeVersion;
063    }
064    return getVersionParameter("system-version", system);
065  }
066
067  private String getVersionParameter(String name, String system) {
068    if (this.valueSetCheckerSimple.expansionParameters != null) {
069      for (ParametersParameterComponent pc : this.valueSetCheckerSimple.expansionParameters.getParameter()) {
070        if (name.equals(pc.getName()) && pc.hasValue()) {
071          String v = pc.getValue().primitiveValue();
072          if (v != null && v.startsWith(system+"|")) {
073            return v.substring(system.length()+1);
074          }
075        }
076      }
077    }
078    return null;
079  }
080
081}