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; 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 ITerminologyClient setTimeoutFactor(int i) { 130 client.setTimeoutFactor(i); 131 return this; 132 } 133 134 @Override 135 public ToolingClientLogger getLogger() { 136 return client.getLogger(); 137 } 138 139 @Override 140 public ITerminologyClient setLogger(ToolingClientLogger txLog) { 141 client.setLogger(txLog); 142 return this; 143 } 144 145 @Override 146 public ITerminologyClient setRetryCount(int retryCount) throws FHIRException { 147 client.setRetryCount(retryCount); 148 return this; 149 } 150 151 @Override 152 public CapabilityStatement getCapabilitiesStatementQuick() throws FHIRException { 153 return (CapabilityStatement) VersionConvertorFactory_30_50.convertResource(client.getCapabilitiesStatementQuick()); 154 } 155 156 @Override 157 public Parameters lookupCode(Map<String, String> params) throws FHIRException { 158 return (Parameters) VersionConvertorFactory_30_50.convertResource(client.lookupCode(params)); 159 } 160 161 @Override 162 public Parameters lookupCode(Parameters params) throws FHIRException { 163 return (Parameters) VersionConvertorFactory_30_50.convertResource(client.lookupCode((org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(params))); 164 } 165 166 @Override 167 public int getRetryCount() throws FHIRException { 168 return client.getRetryCount(); 169 } 170 171 @Override 172 public Bundle validateBatch(Bundle batch) { 173 return (Bundle) VersionConvertorFactory_30_50.convertResource(client.transaction((org.hl7.fhir.dstu3.model.Bundle) VersionConvertorFactory_30_50.convertResource(batch))); 174 } 175 176 @Override 177 public CanonicalResource read(String type, String id) { 178 Class<Resource> t; 179 try { 180 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... 181 } catch (ClassNotFoundException e) { 182 throw new FHIRException("Unable to fetch resources of type " + type + " in R2"); 183 } 184 org.hl7.fhir.dstu3.model.Resource r3 = client.read(t, id); 185 if (r3 == null) { 186 throw new FHIRException("Unable to fetch resource " + Utilities.pathURL(getAddress(), type, id)); 187 } 188 org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_30_50.convertResource(r3); 189 if (r5 == null) { 190 throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 (internal representation)"); 191 } 192 if (!(r5 instanceof CanonicalResource)) { 193 throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 canonical resource (internal representation)"); 194 } 195 return (CanonicalResource) r5; 196 } 197 198 @Override 199 public ClientHeaders getClientHeaders() { 200 return clientHeaders; 201 } 202 203 @Override 204 public ITerminologyClient setClientHeaders(ClientHeaders clientHeaders) { 205 this.clientHeaders = clientHeaders; 206 if (this.clientHeaders != null) { 207 this.client.setClientHeaders(this.clientHeaders.headers()); 208 } 209 this.client.setVersionInMimeTypes(true); 210 return this; 211 } 212 213 @Override 214 public ITerminologyClient setUserAgent(String userAgent) { 215 client.setUserAgent(userAgent); 216 return this; 217 } 218 219 @Override 220 public String getUserAgent() { 221 return client.getUserAgent(); 222 } 223 224 @Override 225 public String getServerVersion() { 226 return client.getServerVersion(); 227 } 228 229 @Override 230 public ITerminologyClient setAcceptLanguage(String lang) { 231 client.setAcceptLanguage(lang); 232 return this; 233 } 234 235 @Override 236 public ITerminologyClient setContentLanguage(String lang) { 237 client.setContentLanguage(lang); 238 return this; 239 } 240 241 @Override 242 public int getUseCount() { 243 return client.getUseCount(); 244 } 245 246 @Override 247 public Bundle search(String type, String criteria) { 248 org.hl7.fhir.dstu3.model.Bundle result = client.search(type, criteria); 249 return result == null ? null : (Bundle) VersionConvertorFactory_30_50.convertResource(result); 250 251 } 252 253 @Override 254 public Parameters subsumes(Parameters pin) throws FHIRException { 255 org.hl7.fhir.dstu3.model.Parameters p2 = (org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(pin); 256 p2 = client.operateType(org.hl7.fhir.dstu3.model.CodeSystem.class, "subsumes", p2); 257 return (Parameters) VersionConvertorFactory_30_50.convertResource(p2); 258 } 259 260 @Override 261 public Parameters translate(Parameters params) throws FHIRException { 262 return (Parameters) VersionConvertorFactory_30_50.convertResource(client.transform((org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(params))); 263 } 264 265}