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