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;
040
041import java.net.URISyntaxException;
042import java.util.Map;
043
044public class TerminologyClientR5 implements TerminologyClient {
045
046  private final FHIRToolingClient client;
047  private ClientHeaders clientHeaders;
048
049  public TerminologyClientR5(String address) throws URISyntaxException {
050    this.client = new FHIRToolingClient(address);
051    setClientHeaders(new ClientHeaders());
052  }
053
054  public TerminologyClientR5(String address, ClientHeaders clientHeaders) throws URISyntaxException {
055    this.client = new FHIRToolingClient(address);
056    setClientHeaders(clientHeaders);
057  }
058
059  @Override
060  public TerminologyCapabilities getTerminologyCapabilities() {
061    return client.getTerminologyCapabilities();
062  }
063
064  @Override
065  public String getAddress() {
066    return client.getAddress();
067  }
068
069  @Override
070  public ValueSet expandValueset(ValueSet vs, Parameters p, Map<String, String> params) {
071    return client.expandValueset(vs, p, params);
072  }
073
074  @Override
075  public Parameters validateCS(Parameters pin) {
076    return client.operateType(CodeSystem.class, "validate-code", pin);
077  }
078
079  @Override
080  public Parameters validateVS(Parameters pin) {
081    return client.operateType(ValueSet.class, "validate-code", pin);
082  }
083
084  @Override
085  public TerminologyClient setTimeout(int i) {
086    client.setTimeout(i);
087    return this;
088  }
089
090  @Override
091  public TerminologyClient setLogger(ToolingClientLogger txLog) {
092    client.setLogger(txLog);
093    return this;
094  }
095
096  @Override
097  public CapabilityStatement getCapabilitiesStatementQuick() {
098    return client.getCapabilitiesStatementQuick();
099  }
100
101  @Override
102  public Parameters lookupCode(Map<String, String> params) {
103    return client.lookupCode(params);
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 int getRetryCount() throws FHIRException {
114    return client.getRetryCount();
115  }
116
117  @Override
118  public Bundle validateBatch(Bundle batch) {
119    return client.transaction(batch);
120  }
121
122  @Override
123  public CanonicalResource read(String type, String id) {
124    Class<Resource> t;
125    try {
126      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...
127    } catch (ClassNotFoundException e) {
128      throw new FHIRException("Unable to fetch resources of type " + type + " in R5");
129    }
130    org.hl7.fhir.r5.model.Resource r5 = client.read(t, id);
131    if (r5 != null) {
132      throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 (internal representation)");
133    }
134    if (!(r5 instanceof CanonicalResource)) {
135      throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 canonical resource (internal representation)");
136    }
137    return (CanonicalResource) r5;
138  }
139
140  @Override
141  public ClientHeaders getClientHeaders() {
142    return clientHeaders;
143  }
144
145  @Override
146  public TerminologyClient setClientHeaders(ClientHeaders clientHeaders) {
147    this.clientHeaders = clientHeaders;
148    this.client.setClientHeaders(this.clientHeaders.headers());
149    return this;
150  }
151}