001package org.hl7.fhir.convertors.txClient;
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
033import org.hl7.fhir.convertors.conv40_50.resources40_50.TerminologyCapabilities40_50;
034import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50;
035import org.hl7.fhir.exceptions.FHIRException;
036import org.hl7.fhir.r4.model.Resource;
037import org.hl7.fhir.r4.utils.client.FHIRToolingClient;
038import org.hl7.fhir.r5.model.*;
039import org.hl7.fhir.r5.terminologies.TerminologyClient;
040import org.hl7.fhir.r5.utils.client.network.ClientHeaders;
041import org.hl7.fhir.utilities.ToolingClientLogger;
042import org.hl7.fhir.utilities.Utilities;
043
044import java.net.URISyntaxException;
045import java.util.Map;
046
047public class TerminologyClientR4 implements TerminologyClient {
048
049  private final FHIRToolingClient client; // todo: use the R2 client
050  private ClientHeaders clientHeaders;
051
052  public TerminologyClientR4(String address) throws URISyntaxException {
053    this.client = new FHIRToolingClient(address);
054    setClientHeaders(new ClientHeaders());
055  }
056
057  public TerminologyClientR4(String address, ClientHeaders clientHeaders) throws URISyntaxException {
058    this.client = new FHIRToolingClient(address);
059    setClientHeaders(clientHeaders);
060  }
061
062  @Override
063  public TerminologyCapabilities getTerminologyCapabilities() throws FHIRException {
064    return TerminologyCapabilities40_50.convertTerminologyCapabilities(client.getTerminologyCapabilities());
065  }
066
067  @Override
068  public String getAddress() {
069    return client.getAddress();
070  }
071
072  @Override
073  public ValueSet expandValueset(ValueSet vs, Parameters p, Map<String, String> params) throws FHIRException {
074    org.hl7.fhir.r4.model.ValueSet vs2 = (org.hl7.fhir.r4.model.ValueSet) VersionConvertorFactory_40_50.convertResource(vs);
075    org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(p);
076    vs2 = client.expandValueset(vs2, p2, params); // todo: second parameter
077    return (ValueSet) VersionConvertorFactory_40_50.convertResource(vs2);
078  }
079
080  @Override
081  public Parameters validateCS(Parameters pin) throws FHIRException {
082    org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(pin);
083    p2 = client.operateType(org.hl7.fhir.r4.model.CodeSystem.class, "validate-code", p2);
084    return (Parameters) VersionConvertorFactory_40_50.convertResource(p2);
085  }
086
087  @Override
088  public Parameters validateVS(Parameters pin) throws FHIRException {
089    org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(pin);
090    p2 = client.operateType(org.hl7.fhir.r4.model.ValueSet.class, "validate-code", p2);
091    return (Parameters) VersionConvertorFactory_40_50.convertResource(p2);
092  }
093
094  @Override
095  public TerminologyClient setTimeout(int i) {
096    client.setTimeout(i);
097    return this;
098  }
099
100  @Override
101  public TerminologyClient setLogger(ToolingClientLogger txLog) {
102    client.setLogger(txLog);
103    return this;
104  }
105
106  @Override
107  public TerminologyClient setRetryCount(int retryCount) throws FHIRException {
108    client.setRetryCount(retryCount);
109    return this;
110  }
111
112  @Override
113  public CapabilityStatement getCapabilitiesStatementQuick() throws FHIRException {
114    return (CapabilityStatement) VersionConvertorFactory_40_50.convertResource(client.getCapabilitiesStatementQuick());
115  }
116
117  @Override
118  public Parameters lookupCode(Map<String, String> params) throws FHIRException {
119    return (Parameters) VersionConvertorFactory_40_50.convertResource(client.lookupCode(params));
120  }
121
122  @Override
123  public int getRetryCount() throws FHIRException {
124    return client.getRetryCount();
125  }
126
127  @Override
128  public Bundle validateBatch(Bundle batch) {
129    return (Bundle) VersionConvertorFactory_40_50.convertResource(client.transaction((org.hl7.fhir.r4.model.Bundle) VersionConvertorFactory_40_50.convertResource(batch)));
130  }
131
132  @Override
133  public CanonicalResource read(String type, String id) {
134    Class<Resource> t;
135    try {
136      t = (Class<Resource>) Class.forName("org.hl7.fhir.r4.model." + type);// todo: do we have to deal with any resource renaming? Use cases are limited...
137    } catch (ClassNotFoundException e) {
138      throw new FHIRException("Unable to fetch resources of type " + type + " in R2");
139    }
140    org.hl7.fhir.r4.model.Resource r4 = client.read(t, id);
141    if (r4 == null) {
142      throw new FHIRException("Unable to fetch resource " + Utilities.pathURL(getAddress(), type, id));
143    }
144    org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_40_50.convertResource(r4);
145    if (r5 != null) {
146      throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 (internal representation)");
147    }
148    if (!(r5 instanceof CanonicalResource)) {
149      throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 canonical resource (internal representation)");
150    }
151    return (CanonicalResource) r5;
152  }
153
154  @Override
155  public ClientHeaders getClientHeaders() {
156    return clientHeaders;
157  }
158
159  @Override
160  public TerminologyClient setClientHeaders(ClientHeaders clientHeaders) {
161    this.clientHeaders = clientHeaders;
162    this.client.setClientHeaders(this.clientHeaders.headers());
163    return this;
164  }
165}