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.utils.client.FHIRToolingClient;
041import org.hl7.fhir.exceptions.FHIRException;
042import org.hl7.fhir.r5.model.Bundle;
043import org.hl7.fhir.r5.model.CanonicalResource;
044import org.hl7.fhir.r5.model.CapabilityStatement;
045import org.hl7.fhir.r5.model.Parameters;
046import org.hl7.fhir.r5.model.TerminologyCapabilities;
047import org.hl7.fhir.r5.model.ValueSet;
048import org.hl7.fhir.r5.terminologies.client.ITerminologyClient;
049import org.hl7.fhir.r5.utils.client.network.ClientHeaders;
050import org.hl7.fhir.utilities.FhirPublication;
051import org.hl7.fhir.utilities.ToolingClientLogger;
052import org.hl7.fhir.utilities.Utilities;
053
054public class TerminologyClientR3 implements ITerminologyClient {
055
056  private final FHIRToolingClient client; // todo: use the R2 client
057  private ClientHeaders clientHeaders;
058  private String id;
059
060  public TerminologyClientR3(String id, String address, String userAgent) throws URISyntaxException {
061    client = new FHIRToolingClient(address, userAgent);
062    setClientHeaders(new ClientHeaders());
063    this.id = id;
064  }
065
066  @Override
067  public String getId() {
068    return id;
069  }
070
071  public TerminologyClientR3(String id, String address, String userAgent, ClientHeaders clientHeaders) throws URISyntaxException {
072    client = new FHIRToolingClient(address, userAgent);
073    setClientHeaders(clientHeaders);
074    this.id = id;
075  }
076
077  public EnumSet<FhirPublication> supportableVersions() {
078    return client.supportableVersions();
079  }
080  
081  public void setAllowedVersions(EnumSet<FhirPublication> versions) {
082   client.setAllowedVersions(versions);
083  }
084  
085  public EnumSet<FhirPublication> getAllowedVersions() {
086    return client.getAllowedVersions();
087  }
088  
089  public FhirPublication getActualVersion() {
090    return client.getActualVersion();
091  }
092  
093  @Override
094  public TerminologyCapabilities getTerminologyCapabilities() throws FHIRException {
095    return TerminologyCapabilities30_50.convertTerminologyCapabilities(client.getTerminologyCapabilities(), false);
096  }
097
098  @Override
099  public String getAddress() {
100    return client.getAddress();
101  }
102
103  @Override
104  public ValueSet expandValueset(ValueSet vs, Parameters p, Map<String, String> params) throws FHIRException {
105    org.hl7.fhir.dstu3.model.ValueSet vs2 = (org.hl7.fhir.dstu3.model.ValueSet) VersionConvertorFactory_30_50.convertResource(vs);
106    org.hl7.fhir.dstu3.model.Parameters p2 = (org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(p);
107    vs2 = client.expandValueset(vs2, p2, params); // todo: second parameter
108    return (ValueSet) VersionConvertorFactory_30_50.convertResource(vs2);
109  }
110
111  @Override
112  public Parameters validateCS(Parameters pin) throws FHIRException {
113    org.hl7.fhir.dstu3.model.Parameters p2 = (org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(pin);
114    p2 = client.operateType(org.hl7.fhir.dstu3.model.CodeSystem.class, "validate-code", p2);
115    return (Parameters) VersionConvertorFactory_30_50.convertResource(p2);
116  }
117
118  @Override
119  public Parameters validateVS(Parameters pin) throws FHIRException {
120    org.hl7.fhir.dstu3.model.Parameters p2 = (org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(pin);
121    p2 = client.operateType(org.hl7.fhir.dstu3.model.ValueSet.class, "validate-code", p2);
122    return (Parameters) VersionConvertorFactory_30_50.convertResource(p2);
123  }
124
125  @Override
126  public ITerminologyClient setTimeout(int i) {
127    client.setTimeout(i);
128    return this;
129  }
130
131  @Override
132  public ITerminologyClient setLogger(ToolingClientLogger txLog) {
133    client.setLogger(txLog);
134    return this;
135  }
136
137  @Override
138  public ITerminologyClient setRetryCount(int retryCount) throws FHIRException {
139    client.setRetryCount(retryCount);
140    return this;
141  }
142
143  @Override
144  public CapabilityStatement getCapabilitiesStatementQuick() throws FHIRException {
145    return (CapabilityStatement) VersionConvertorFactory_30_50.convertResource(client.getCapabilitiesStatementQuick());
146  }
147
148  @Override
149  public Parameters lookupCode(Map<String, String> params) throws FHIRException {
150    return (Parameters) VersionConvertorFactory_30_50.convertResource(client.lookupCode(params));
151  }
152
153  @Override
154  public int getRetryCount() throws FHIRException {
155    return client.getRetryCount();
156  }
157
158  @Override
159  public Bundle validateBatch(Bundle batch) {
160    return (Bundle) VersionConvertorFactory_30_50.convertResource(client.transaction((org.hl7.fhir.dstu3.model.Bundle) VersionConvertorFactory_30_50.convertResource(batch)));
161  }
162
163  @Override
164  public CanonicalResource read(String type, String id) {
165    Class<Resource> t;
166    try {
167      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...
168    } catch (ClassNotFoundException e) {
169      throw new FHIRException("Unable to fetch resources of type " + type + " in R2");
170    }
171    org.hl7.fhir.dstu3.model.Resource r3 = client.read(t, id);
172    if (r3 == null) {
173      throw new FHIRException("Unable to fetch resource " + Utilities.pathURL(getAddress(), type, id));
174    }
175    org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_30_50.convertResource(r3);
176    if (r5 == null) {
177      throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 (internal representation)");
178    }
179    if (!(r5 instanceof CanonicalResource)) {
180      throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 canonical resource (internal representation)");
181    }
182    return (CanonicalResource) r5;
183  }
184
185  @Override
186  public ClientHeaders getClientHeaders() {
187    return clientHeaders;
188  }
189
190  @Override
191  public ITerminologyClient setClientHeaders(ClientHeaders clientHeaders) {
192    this.clientHeaders = clientHeaders;
193    if (this.clientHeaders != null) {
194      this.client.setClientHeaders(this.clientHeaders.headers());
195    }
196    return this;
197  }
198
199  @Override
200  public ITerminologyClient setUserAgent(String userAgent) {
201    client.setUserAgent(userAgent);
202    return this;
203 }
204
205  @Override
206  public String getUserAgent() {
207    return client.getUserAgent();
208  }
209
210  @Override
211  public String getServerVersion() {
212    return client.getServerVersion();
213  }
214
215  @Override
216  public ITerminologyClient setLanguage(String lang) {
217    client.setLanguage(lang);
218    return this;
219  }
220}