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