001package org.hl7.fhir.convertors.txClient; 002 003import java.io.IOException; 004import java.net.URISyntaxException; 005import java.util.EnumSet; 006import java.util.HashMap; 007import java.util.Map; 008 009import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50; 010import org.hl7.fhir.exceptions.FHIRException; 011import org.hl7.fhir.r4.model.Resource; 012import org.hl7.fhir.r4.utils.client.EFhirClientException; 013import org.hl7.fhir.r4.utils.client.FHIRToolingClient; 014import org.hl7.fhir.r5.model.Bundle; 015import org.hl7.fhir.r5.model.CanonicalResource; 016import org.hl7.fhir.r5.model.CapabilityStatement; 017import org.hl7.fhir.r5.model.OperationOutcome; 018import org.hl7.fhir.r5.model.Parameters; 019import org.hl7.fhir.r5.model.TerminologyCapabilities; 020import org.hl7.fhir.r5.model.ValueSet; 021import org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent; 022import org.hl7.fhir.r5.terminologies.client.ITerminologyClient; 023import org.hl7.fhir.r5.utils.client.network.ClientHeaders; 024import org.hl7.fhir.utilities.FhirPublication; 025import org.hl7.fhir.utilities.ToolingClientLogger; 026import org.hl7.fhir.utilities.Utilities; 027 028public class TerminologyClientR4 implements ITerminologyClient { 029 030 private final FHIRToolingClient client; // todo: use the R2 client 031 private ClientHeaders clientHeaders; 032 private String id; 033 034 public TerminologyClientR4(String id, String address, String userAgent) throws URISyntaxException { 035 this.client = new FHIRToolingClient(address, userAgent); 036 setClientHeaders(new ClientHeaders()); 037 this.id = id; 038 } 039 040 public TerminologyClientR4(String id, String address, String userAgent, ClientHeaders clientHeaders) throws URISyntaxException { 041 this.client = new FHIRToolingClient(address, userAgent); 042 setClientHeaders(clientHeaders); 043 this.id = id; 044 } 045 046 @Override 047 public String getId() { 048 return id; 049 } 050 051 052 053 public EnumSet<FhirPublication> supportableVersions() { 054 // todo 055 return EnumSet.range(FhirPublication.STU3, FhirPublication.R5); 056 } 057 058 public void setAllowedVersions(EnumSet<FhirPublication> versions) { 059 // todo 060 } 061 062 public EnumSet<FhirPublication> getAllowedVersions() { 063 return null; // todo 064 } 065 066 public FhirPublication getActualVersion() { 067 return FhirPublication.STU3; 068 } 069 070 071 @Override 072 public TerminologyCapabilities getTerminologyCapabilities() throws FHIRException { 073 return (TerminologyCapabilities) VersionConvertorFactory_40_50.convertResource(client.getTerminologyCapabilities()); 074 } 075 076 @Override 077 public String getAddress() { 078 return client.getAddress(); 079 } 080 081 @Override 082 public ValueSet expandValueset(ValueSet vs, Parameters p) throws FHIRException { 083 org.hl7.fhir.r4.model.ValueSet vs2 = vs == null ? null : (org.hl7.fhir.r4.model.ValueSet) VersionConvertorFactory_40_50.convertResource(vs); 084 org.hl7.fhir.r4.model.Parameters p2 = p == null ? null : (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(p); 085 try { 086 vs2 = client.expandValueset(vs2, p2); // todo: second parameter 087 return (ValueSet) VersionConvertorFactory_40_50.convertResource(vs2); 088 } catch (org.hl7.fhir.r4.utils.client.EFhirClientException e) { 089 if (e.getServerErrors().size() > 0) { 090 throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), (org.hl7.fhir.r5.model.OperationOutcome) VersionConvertorFactory_40_50.convertResource(e.getServerErrors().get(0))); 091 } else { 092 throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage()); 093 } 094 } 095 } 096 097 @Override 098 public Parameters validateCS(Parameters pin) throws FHIRException { 099 try { 100 org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(pin); 101 p2 = client.operateType(org.hl7.fhir.r4.model.CodeSystem.class, "validate-code", p2); 102 return (Parameters) VersionConvertorFactory_40_50.convertResource(p2); 103 } catch (EFhirClientException e) { 104 if (e.getServerErrors().size() == 1) { 105 OperationOutcome op = (OperationOutcome) VersionConvertorFactory_40_50.convertResource(e.getServerErrors().get(0)); 106 throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), op, e); 107 } else { 108 throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), e); 109 } 110 } catch (IOException e) { 111 throw new FHIRException(e); 112 } 113 } 114 115 116 @Override 117 public Parameters subsumes(Parameters pin) throws FHIRException { 118 try { 119 org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(pin); 120 p2 = client.operateType(org.hl7.fhir.r4.model.CodeSystem.class, "subsumes", p2); 121 return (Parameters) VersionConvertorFactory_40_50.convertResource(p2); 122 } catch (EFhirClientException e) { 123 if (e.getServerErrors().size() == 1) { 124 OperationOutcome op = (OperationOutcome) VersionConvertorFactory_40_50.convertResource(e.getServerErrors().get(0)); 125 throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), op, e); 126 } else { 127 throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), e); 128 } 129 } catch (IOException e) { 130 throw new FHIRException(e); 131 } 132 } 133 134 @Override 135 public Parameters validateVS(Parameters pin) throws FHIRException { 136 try { 137 org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(pin); 138 p2 = client.operateType(org.hl7.fhir.r4.model.ValueSet.class, "validate-code", p2); 139 return (Parameters) VersionConvertorFactory_40_50.convertResource(p2); 140 } catch (EFhirClientException e) { 141 if (e.getServerErrors().size() == 1) { 142 OperationOutcome op = (OperationOutcome) VersionConvertorFactory_40_50.convertResource(e.getServerErrors().get(0)); 143 throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), op, e); 144 } else { 145 throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), e); 146 } 147 } catch (IOException e) { 148 throw new FHIRException(e); 149 } 150 } 151 152 @Override 153 public ITerminologyClient setTimeoutFactor(int i) { 154 client.setTimeoutFactor(i); 155 return this; 156 } 157 158 @Override 159 public ToolingClientLogger getLogger() { 160 return client.getLogger(); 161 } 162 163 @Override 164 public ITerminologyClient setLogger(ToolingClientLogger txLog) { 165 client.setLogger(txLog); 166 return this; 167 } 168 169 @Override 170 public ITerminologyClient setRetryCount(int retryCount) throws FHIRException { 171 client.setRetryCount(retryCount); 172 return this; 173 } 174 175 @Override 176 public CapabilityStatement getCapabilitiesStatementQuick() throws FHIRException { 177 return (CapabilityStatement) VersionConvertorFactory_40_50.convertResource(client.getCapabilitiesStatementQuick()); 178 } 179 180 @Override 181 public Parameters lookupCode(Map<String, String> params) throws FHIRException { 182 return (Parameters) VersionConvertorFactory_40_50.convertResource(client.lookupCode(params)); 183 } 184 185 @Override 186 public Parameters lookupCode(Parameters params) throws FHIRException { 187 return (Parameters) VersionConvertorFactory_40_50.convertResource(client.lookupCode((org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(params))); 188 } 189 190 @Override 191 public int getRetryCount() throws FHIRException { 192 return client.getRetryCount(); 193 } 194 195 @Override 196 public Bundle validateBatch(Bundle batch) { 197 org.hl7.fhir.r4.model.Bundle result = client.transaction((org.hl7.fhir.r4.model.Bundle) VersionConvertorFactory_40_50.convertResource(batch)); 198 return result == null ? null : (Bundle) VersionConvertorFactory_40_50.convertResource(result); 199 } 200 201 @Override 202 public CanonicalResource read(String type, String id) { 203 Class<Resource> t; 204 try { 205 t = (Class<Resource>) Class.forName("org.hl7.fhir.r4.model." + type);// todo: do we have to deal with any resource renaming? Use cases are limited... 206 } catch (ClassNotFoundException e) { 207 throw new FHIRException("Unable to fetch resources of type " + type + " in R2"); 208 } 209 org.hl7.fhir.r4.model.Resource r4 = client.read(t, id); 210 if (r4 == null) { 211 throw new FHIRException("Unable to fetch resource " + Utilities.pathURL(getAddress(), type, id)); 212 } 213 org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_40_50.convertResource(r4); 214 if (r5 == null) { 215 throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 (internal representation)"); 216 } 217 if (!(r5 instanceof CanonicalResource)) { 218 throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 canonical resource (internal representation)"); 219 } 220 return (CanonicalResource) r5; 221 } 222 223 @Override 224 public ClientHeaders getClientHeaders() { 225 return clientHeaders; 226 } 227 228 @Override 229 public ITerminologyClient setClientHeaders(ClientHeaders clientHeaders) { 230 this.clientHeaders = clientHeaders; 231 if (this.clientHeaders != null) { 232 this.client.setClientHeaders(this.clientHeaders.headers()); 233 } 234 this.client.setVersionInMimeTypes(true); 235 return this; 236 } 237 238 @Override 239 public ITerminologyClient setUserAgent(String userAgent) { 240 client.setUserAgent(userAgent); 241 return this; 242 } 243 244 @Override 245 public String getUserAgent() { 246 return client.getUserAgent(); 247 } 248 249 @Override 250 public String getServerVersion() { 251 return client.getServerVersion(); 252 } 253 254 255 @Override 256 public ITerminologyClient setAcceptLanguage(String lang) { 257 client.setAcceptLanguage(lang); 258 return this; 259 } 260 261 @Override 262 public ITerminologyClient setContentLanguage(String lang) { 263 client.setContentLanguage(lang); 264 return this; 265 } 266 267 @Override 268 public int getUseCount() { 269 return client.getUseCount(); 270 } 271 272 @Override 273 public Bundle search(String type, String criteria) { 274 org.hl7.fhir.r4.model.Bundle result = client.search(type, criteria); 275 return result == null ? null : (Bundle) VersionConvertorFactory_40_50.convertResource(result); 276 } 277 278 @Override 279 public Parameters translate(Parameters params) throws FHIRException { 280 return (Parameters) VersionConvertorFactory_40_50.convertResource(client.translate((org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(params))); 281 } 282 283}