
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.conv10_50.resources10_50.TerminologyCapabilities10_50; 039import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_50; 040import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50; 041import org.hl7.fhir.dstu2.model.Resource; 042import org.hl7.fhir.dstu2.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 TerminologyClientR2 implements ITerminologyClient { 059 060 private final FHIRToolingClient client; // todo: use the R2 client 061 private String id; 062 063 public EnumSet<FhirPublication> supportableVersions() { 064 return EnumSet.of(FhirPublication.DSTU2); 065 } 066 067 public void setAllowedVersions(EnumSet<FhirPublication> versions) { 068 boolean found = false; 069 for (FhirPublication version : versions) { 070 if (version == FhirPublication.DSTU2) { 071 found = true; 072 } else { 073 throw new Error("Unsupported version for DSTU2 client: "+version.toCode()); 074 } 075 } 076 if (!found) { 077 throw new Error("DSTU2 client only supports DSTU2"); 078 } 079 } 080 081 public EnumSet<FhirPublication> getAllowedVersions() { 082 return EnumSet.of(FhirPublication.DSTU2); 083 } 084 085 public FhirPublication getActualVersion() { 086 return FhirPublication.DSTU2; 087 } 088 089 public TerminologyClientR2(String id, String address, String userAgent) throws URISyntaxException { 090 client = new FHIRToolingClient(address, userAgent); 091 this.client.setVersionInMimeTypes(true); 092 this.id = id; 093 } 094 095 @Override 096 public String getId() { 097 return id; 098 } 099 100 @Override 101 public TerminologyCapabilities getTerminologyCapabilities() throws FHIRException { 102 return TerminologyCapabilities10_50.convertTerminologyCapabilities(client.getTerminologyCapabilities()); 103 } 104 105 @Override 106 public String getAddress() { 107 return client.getAddress(); 108 } 109 110 @Override 111 public ValueSet expandValueset(ValueSet vs, Parameters p) throws FHIRException { 112 org.hl7.fhir.dstu2.model.ValueSet vs2 = (org.hl7.fhir.dstu2.model.ValueSet) VersionConvertorFactory_10_50.convertResource(vs); 113 org.hl7.fhir.dstu2.model.Parameters p2 = (org.hl7.fhir.dstu2.model.Parameters) VersionConvertorFactory_10_50.convertResource(p); 114 vs2 = client.expandValueset(vs2, p2); 115 return (ValueSet) VersionConvertorFactory_10_50.convertResource(vs2); 116 } 117 118 @Override 119 public Parameters validateCS(Parameters pin) throws FHIRException { 120 org.hl7.fhir.dstu2.model.Parameters p2 = (org.hl7.fhir.dstu2.model.Parameters) VersionConvertorFactory_10_50.convertResource(pin); 121 p2 = client.operateType(org.hl7.fhir.dstu2.model.ValueSet.class, "validate-code", p2); 122 return (Parameters) VersionConvertorFactory_10_50.convertResource(p2); 123 } 124 125 @Override 126 public Parameters subsumes(Parameters pin) throws FHIRException { 127 org.hl7.fhir.dstu2.model.Parameters p2 = (org.hl7.fhir.dstu2.model.Parameters) VersionConvertorFactory_10_50.convertResource(pin); 128 p2 = client.operateType(org.hl7.fhir.dstu2.model.ValueSet.class, "subsumes", p2); 129 return (Parameters) VersionConvertorFactory_10_50.convertResource(p2); 130 } 131 132 @Override 133 public Parameters validateVS(Parameters pin) throws FHIRException { 134 org.hl7.fhir.dstu2.model.Parameters p2 = (org.hl7.fhir.dstu2.model.Parameters) VersionConvertorFactory_10_50.convertResource(pin); 135 p2 = client.operateType(org.hl7.fhir.dstu2.model.ValueSet.class, "validate-code", p2); 136 return (Parameters) VersionConvertorFactory_10_50.convertResource(p2); 137 } 138 139 @Override 140 public ITerminologyClient setTimeoutFactor(int i) { 141 client.setTimeoutFactor(i); 142 return this; 143 } 144 145 @Override 146 public ToolingClientLogger getLogger() { 147 return client.getLogger(); 148 } 149 150 @Override 151 public ITerminologyClient setLogger(ToolingClientLogger txLog) { 152 client.setLogger(txLog); 153 return this; 154 } 155 156 @Override 157 public ITerminologyClient setRetryCount(int retryCount) throws FHIRException { 158 client.setRetryCount(retryCount); 159 return this; 160 } 161 162 @Override 163 public CapabilityStatement getCapabilitiesStatementQuick() throws FHIRException { 164 return (CapabilityStatement) VersionConvertorFactory_10_50.convertResource(client.getConformanceStatementQuick()); 165 } 166 167 @Override 168 public CapabilityStatement getCapabilitiesStatement() throws FHIRException { 169 return (CapabilityStatement) VersionConvertorFactory_10_50.convertResource(client.getConformanceStatement()); 170 } 171 172 @Override 173 public Parameters lookupCode(Map<String, String> params) throws FHIRException { 174 return (Parameters) VersionConvertorFactory_10_50.convertResource(client.lookupCode(params)); 175 } 176 177 @Override 178 public Parameters lookupCode(Parameters params) throws FHIRException { 179 return (Parameters) VersionConvertorFactory_10_50.convertResource(client.lookupCode((org.hl7.fhir.dstu2.model.Parameters) VersionConvertorFactory_10_50.convertResource(params))); 180 } 181 182 @Override 183 public int getRetryCount() throws FHIRException { 184 return client.getRetryCount(); 185 } 186 187 @Override 188 public Bundle validateBatch(Bundle batch) { 189 return (Bundle) VersionConvertorFactory_10_50.convertResource(client.transaction((org.hl7.fhir.dstu2.model.Bundle) VersionConvertorFactory_10_50.convertResource(batch))); 190 } 191 192 @Override 193 public CanonicalResource read(String type, String id) { 194 Class<Resource> t; 195 try { 196 t = (Class<Resource>) Class.forName("org.hl7.fhir.dstu2.model." + type);// todo: do we have to deal with any resource renaming? Use cases are limited... 197 } catch (ClassNotFoundException e) { 198 throw new FHIRException("Unable to fetch resources of type " + type + " in R2"); 199 } 200 org.hl7.fhir.dstu2.model.Resource r2 = client.read(t, id); 201 if (r2 == null) { 202 throw new FHIRException("Unable to fetch resource " + Utilities.pathURL(getAddress(), type, id)); 203 } 204 org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_10_50.convertResource(r2); 205 if (r5 == null) { 206 throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 (internal representation)"); 207 } 208 if (!(r5 instanceof CanonicalResource)) { 209 throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 canonical resource (internal representation)"); 210 } 211 return (CanonicalResource) r5; 212 } 213 214 @Override 215 public Iterable<HTTPHeader> getClientHeaders() { 216 return null; 217 } 218 219 @Override 220 public ITerminologyClient setClientHeaders(ClientHeaders clientHeaders) { 221 return null; 222 } 223 224 @Override 225 public ITerminologyClient setUserAgent(String userAgent) { 226 client.setUserAgent(userAgent); 227 return this; 228 } 229 230 @Override 231 public String getUserAgent() { 232 return client.getUserAgent(); 233 } 234 235 @Override 236 public String getServerVersion() { 237 return client.getServerVersion(); 238 } 239 240 @Override 241 public ITerminologyClient setAcceptLanguage(String lang) { 242 client.setAcceptLanguage(lang); 243 return this; 244 } 245 246 @Override 247 public ITerminologyClient setContentLanguage(String lang) { 248 client.setContentLanguage(lang); 249 return this; 250 } 251 252 @Override 253 public int getUseCount() { 254 return client.getUseCount(); 255 } 256 257 @Override 258 public Bundle search(String type, String criteria) { 259 org.hl7.fhir.dstu2.model.Bundle result = client.search(type, criteria); 260 return result == null ? null : (Bundle) VersionConvertorFactory_10_50.convertResource(result); 261 } 262 263 @Override 264 public Parameters translate(Parameters params) throws FHIRException { 265 return (Parameters) VersionConvertorFactory_10_50.convertResource(client.translate((org.hl7.fhir.dstu2.model.Parameters) VersionConvertorFactory_10_50.convertResource(params))); 266 } 267 268 @Override 269 public void setConversionLogger(ITerminologyConversionLogger logger) { 270 // TODO Auto-generated method stub 271 272 } 273 274}