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