001package org.hl7.fhir.convertors.txClient;
002
003import java.net.URISyntaxException;
004import java.util.EnumSet;
005import java.util.HashMap;
006import java.util.Map;
007
008/*
009  Copyright (c) 2011+, HL7, Inc.
010  All rights reserved.
011  
012  Redistribution and use in source and binary forms, with or without modification, 
013  are permitted provided that the following conditions are met:
014    
015   * Redistributions of source code must retain the above copyright notice, this 
016     list of conditions and the following disclaimer.
017   * Redistributions in binary form must reproduce the above copyright notice, 
018     this list of conditions and the following disclaimer in the documentation 
019     and/or other materials provided with the distribution.
020   * Neither the name of HL7 nor the names of its contributors may be used to 
021     endorse or promote products derived from this software without specific 
022     prior written permission.
023  
024  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
025  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
026  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
027  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
028  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
029  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
030  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
031  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
032  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
033  POSSIBILITY OF SUCH DAMAGE.
034  
035 */
036
037
038import org.hl7.fhir.convertors.conv30_50.resources30_50.TerminologyCapabilities30_50;
039import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_50;
040import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50;
041import org.hl7.fhir.dstu3.model.Resource;
042import org.hl7.fhir.dstu3.utils.client.FHIRToolingClient;
043import org.hl7.fhir.exceptions.FHIRException;
044import org.hl7.fhir.r5.model.Bundle;
045import org.hl7.fhir.r5.model.CanonicalResource;
046import org.hl7.fhir.r5.model.CapabilityStatement;
047import org.hl7.fhir.r5.model.Parameters;
048import org.hl7.fhir.r5.model.TerminologyCapabilities;
049import org.hl7.fhir.r5.model.ValueSet;
050import org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent;
051import org.hl7.fhir.r5.terminologies.client.ITerminologyClient;
052import org.hl7.fhir.r5.utils.client.network.ClientHeaders;
053import org.hl7.fhir.utilities.FhirPublication;
054import org.hl7.fhir.utilities.ToolingClientLogger;
055import org.hl7.fhir.utilities.Utilities;
056import org.hl7.fhir.utilities.http.HTTPHeader;
057
058public class TerminologyClientR3 implements ITerminologyClient {
059
060  private final FHIRToolingClient client; // todo: use the R2 client
061  private ClientHeaders clientHeaders;
062  private String id;
063
064  public TerminologyClientR3(String id, String address, String userAgent) throws URISyntaxException {
065    client = new FHIRToolingClient(address, userAgent);
066    setClientHeaders(new ClientHeaders());
067    this.id = id;
068  }
069
070  @Override
071  public String getId() {
072    return id;
073  }
074
075  public TerminologyClientR3(String id, String address, String userAgent, ClientHeaders clientHeaders) throws URISyntaxException {
076    client = new FHIRToolingClient(address, userAgent);
077    setClientHeaders(clientHeaders);
078    this.id = id;
079  }
080
081  public EnumSet<FhirPublication> supportableVersions() {
082    return client.supportableVersions();
083  }
084  
085  public void setAllowedVersions(EnumSet<FhirPublication> versions) {
086   client.setAllowedVersions(versions);
087  }
088  
089  public EnumSet<FhirPublication> getAllowedVersions() {
090    return client.getAllowedVersions();
091  }
092  
093  public FhirPublication getActualVersion() {
094    return client.getActualVersion();
095  }
096  
097  @Override
098  public TerminologyCapabilities getTerminologyCapabilities() throws FHIRException {
099    return TerminologyCapabilities30_50.convertTerminologyCapabilities(client.getTerminologyCapabilities(), false);
100  }
101
102  @Override
103  public String getAddress() {
104    return client.getAddress();
105  }
106
107  @Override
108  public ValueSet expandValueset(ValueSet vs, Parameters p) throws FHIRException {
109    org.hl7.fhir.dstu3.model.ValueSet vs2 = (org.hl7.fhir.dstu3.model.ValueSet) VersionConvertorFactory_30_50.convertResource(vs);
110    org.hl7.fhir.dstu3.model.Parameters p2 = (org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(p);
111    vs2 = client.expandValueset(vs2, p2); // todo: second parameter
112    return (ValueSet) VersionConvertorFactory_30_50.convertResource(vs2);
113  }
114
115  @Override
116  public Parameters validateCS(Parameters pin) throws FHIRException {
117    org.hl7.fhir.dstu3.model.Parameters p2 = (org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(pin);
118    p2 = client.operateType(org.hl7.fhir.dstu3.model.CodeSystem.class, "validate-code", p2);
119    return (Parameters) VersionConvertorFactory_30_50.convertResource(p2);
120  }
121
122  @Override
123  public Parameters validateVS(Parameters pin) throws FHIRException {
124    org.hl7.fhir.dstu3.model.Parameters p2 = (org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(pin);
125    p2 = client.operateType(org.hl7.fhir.dstu3.model.ValueSet.class, "validate-code", p2);
126    return (Parameters) VersionConvertorFactory_30_50.convertResource(p2);
127  }
128
129  @Override
130  public ITerminologyClient setTimeoutFactor(int i) {
131    client.setTimeoutFactor(i);
132    return this;
133  }
134  
135  @Override
136  public ToolingClientLogger getLogger() {
137    return client.getLogger();
138  }
139
140  @Override
141  public ITerminologyClient setLogger(ToolingClientLogger txLog) {
142    client.setLogger(txLog);
143    return this;
144  }
145
146  @Override
147  public ITerminologyClient setRetryCount(int retryCount) throws FHIRException {
148    client.setRetryCount(retryCount);
149    return this;
150  }
151
152  @Override
153  public CapabilityStatement getCapabilitiesStatementQuick() throws FHIRException {
154    return (CapabilityStatement) VersionConvertorFactory_30_50.convertResource(client.getCapabilitiesStatementQuick());
155  }
156
157  @Override
158  public CapabilityStatement getCapabilitiesStatement() throws FHIRException {
159    return (CapabilityStatement) VersionConvertorFactory_30_50.convertResource(client.getCapabilitiesStatement());
160  }
161
162  @Override
163  public Parameters lookupCode(Map<String, String> params) throws FHIRException {
164    return (Parameters) VersionConvertorFactory_30_50.convertResource(client.lookupCode(params));
165  }
166
167  @Override
168  public Parameters lookupCode(Parameters params) throws FHIRException {
169    return (Parameters) VersionConvertorFactory_30_50.convertResource(client.lookupCode((org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(params)));
170  }
171
172  @Override
173  public int getRetryCount() throws FHIRException {
174    return client.getRetryCount();
175  }
176
177  @Override
178  public Bundle validateBatch(Bundle batch) {
179    return (Bundle) VersionConvertorFactory_30_50.convertResource(client.transaction((org.hl7.fhir.dstu3.model.Bundle) VersionConvertorFactory_30_50.convertResource(batch)));
180  }
181
182  @Override
183  public CanonicalResource read(String type, String id) {
184    Class<Resource> t;
185    try {
186      t = (Class<Resource>) Class.forName("org.hl7.fhir.dstu3.model." + type);// todo: do we have to deal with any resource renaming? Use cases are limited...
187    } catch (ClassNotFoundException e) {
188      throw new FHIRException("Unable to fetch resources of type " + type + " in R2");
189    }
190    org.hl7.fhir.dstu3.model.Resource r3 = client.read(t, id);
191    if (r3 == null) {
192      throw new FHIRException("Unable to fetch resource " + Utilities.pathURL(getAddress(), type, id));
193    }
194    org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_30_50.convertResource(r3);
195    if (r5 == null) {
196      throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 (internal representation)");
197    }
198    if (!(r5 instanceof CanonicalResource)) {
199      throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 canonical resource (internal representation)");
200    }
201    return (CanonicalResource) r5;
202  }
203
204  @Override
205  public Iterable<HTTPHeader> getClientHeaders() {
206    return clientHeaders.headers();
207  }
208
209  @Override
210  public ITerminologyClient setClientHeaders(ClientHeaders clientHeaders) {
211    this.clientHeaders = clientHeaders;
212    if (this.clientHeaders != null) {
213      this.client.setClientHeaders(this.clientHeaders.headers());
214    }
215    this.client.setVersionInMimeTypes(true);
216    return this;
217  }
218
219  @Override
220  public ITerminologyClient setUserAgent(String userAgent) {
221    client.setUserAgent(userAgent);
222    return this;
223 }
224
225  @Override
226  public String getUserAgent() {
227    return client.getUserAgent();
228  }
229
230  @Override
231  public String getServerVersion() {
232    return client.getServerVersion();
233  }
234
235  @Override
236  public ITerminologyClient setAcceptLanguage(String lang) {
237    client.setAcceptLanguage(lang);
238    return this;
239  }
240  
241  @Override
242  public ITerminologyClient setContentLanguage(String lang) {
243    client.setContentLanguage(lang);
244    return this;
245  }
246  
247  @Override
248  public int getUseCount() {
249    return client.getUseCount();
250  }
251
252  @Override
253  public Bundle search(String type, String criteria) {   
254    org.hl7.fhir.dstu3.model.Bundle result = client.search(type, criteria);
255    return result == null ? null : (Bundle) VersionConvertorFactory_30_50.convertResource(result);
256
257  }
258
259  @Override
260  public Parameters subsumes(Parameters pin) throws FHIRException { 
261    org.hl7.fhir.dstu3.model.Parameters p2 = (org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(pin);
262    p2 = client.operateType(org.hl7.fhir.dstu3.model.CodeSystem.class, "subsumes", p2);
263    return (Parameters) VersionConvertorFactory_30_50.convertResource(p2);
264  }
265
266  @Override
267  public Parameters translate(Parameters params) throws FHIRException {
268    return (Parameters) VersionConvertorFactory_30_50.convertResource(client.transform((org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(params)));
269  }
270
271  @Override
272  public void setConversionLogger(ITerminologyConversionLogger logger) {
273    // TODO Auto-generated method stub
274    
275  }
276
277}