001package org.hl7.fhir.convertors.txClient;
002
003import java.net.URISyntaxException;
004
005import org.hl7.fhir.r5.terminologies.client.ITerminologyClient;
006import org.hl7.fhir.utilities.FhirPublication;
007import org.hl7.fhir.utilities.Utilities;
008import org.hl7.fhir.utilities.VersionUtilities;
009
010public class TerminologyClientFactory {
011
012  public static ITerminologyClient makeClient(String id, String url, String userAgent, FhirPublication v) throws URISyntaxException {
013    if (v == null)
014      return new TerminologyClientR5(id, checkEndsWith("/r4", url), userAgent);
015    switch (v) {
016      case DSTU2016May:
017        return new TerminologyClientR3(id, checkEndsWith("/r3", url), userAgent); // r3 is the least worst match
018      case DSTU1:
019        throw new Error("The version " + v + " is not currently supported");
020      case DSTU2:
021        return new TerminologyClientR2(id, checkEndsWith("/r2", url), userAgent);
022      case R4:
023        return new TerminologyClientR4(id, checkEndsWith("/r4", url), userAgent);
024      case R4B:
025        return new TerminologyClientR4(id, checkEndsWith("/r4", url), userAgent);
026      case R5:
027        return new TerminologyClientR5(id, checkEndsWith("/r4", url), userAgent); // r4 for now, since the terminology is currently the same
028      case STU3:
029        return new TerminologyClientR3(id, checkEndsWith("/r3", url), userAgent);
030      default:
031        throw new Error("The version " + v + " is not currently supported");
032    }
033  }
034
035  public static ITerminologyClient makeClient(String id, String url, String userAgent, String v) throws URISyntaxException {
036    if (v == null)
037      return new TerminologyClientR5(id, checkEndsWith("/r4", url), userAgent);
038    v = VersionUtilities.getMajMin(v);
039    if (VersionUtilities.isR2Ver(v)) {
040      return new TerminologyClientR2(id, checkEndsWith("/r2", url), userAgent);      
041    }
042    if (VersionUtilities.isR2BVer(v)) {
043      return new TerminologyClientR3(id, checkEndsWith("/r3", url), userAgent); // r3 is the least worst match      
044    }
045    if (VersionUtilities.isR3Ver(v)) {
046      return new TerminologyClientR3(id, checkEndsWith("/r3", url), userAgent); // r3 is the least worst match      
047    }
048    if (VersionUtilities.isR4Ver(v)) {
049      return new TerminologyClientR4(id, checkEndsWith("/r4", url), userAgent);      
050    }
051    if (VersionUtilities.isR4BVer(v)) {
052      return new TerminologyClientR4(id, checkEndsWith("/r4", url), userAgent);
053    }
054    if (VersionUtilities.isR5Plus(v)) {
055      return new TerminologyClientR5(id, checkEndsWith("/r4", url), userAgent); // r4 for now, since the terminology is currently the same      
056    }
057    throw new Error("The version " + v + " is not currently supported");
058  }
059
060  private static String checkEndsWith(String term, String url) {
061    if (url.endsWith(term))
062      return url;
063    if (Utilities.isTxFhirOrgServer(url)) {
064      return Utilities.pathURL(url, term);
065    }
066    return url;
067  }
068
069}